Skip to content

Commit

Permalink
Add unit tests for GetVersion (#1389)
Browse files Browse the repository at this point in the history
* Add unit tests for GetVersion
  • Loading branch information
3vilhamster authored Nov 6, 2024
1 parent 17b9b1a commit aa18e1d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/internal_event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,10 @@ func (wc *workflowEnvironmentImpl) GetVersion(changeID string, minSupported, max
// Also upsert search attributes to enable ability to search by changeVersion.
version = maxSupported
wc.decisionsHelper.recordVersionMarker(changeID, version, wc.GetDataConverter())
wc.UpsertSearchAttributes(createSearchAttributesForChangeVersion(changeID, version, wc.changeVersions))
err := wc.UpsertSearchAttributes(createSearchAttributesForChangeVersion(changeID, version, wc.changeVersions))
if err != nil {
wc.logger.Warn("UpsertSearchAttributes failed", zap.Error(err))
}
}

validateVersion(changeID, version, minSupported, maxSupported)
Expand Down
43 changes: 43 additions & 0 deletions internal/internal_event_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,49 @@ func TestSideEffect(t *testing.T) {
})
}

func TestGetVersion_validation(t *testing.T) {
t.Run("version < minSupported", func(t *testing.T) {
assert.PanicsWithValue(t, `Workflow code removed support of version 1. for "test" changeID. The oldest supported version is 2`, func() {
validateVersion("test", 1, 2, 3)
})
})
t.Run("version > maxSupported", func(t *testing.T) {
assert.PanicsWithValue(t, `Workflow code is too old to support version 3 for "test" changeID. The maximum supported version is 2`, func() {
validateVersion("test", 3, 1, 2)
})
})
t.Run("success", func(t *testing.T) {
validateVersion("test", 2, 1, 3)
})
}

func TestGetVersion(t *testing.T) {
t.Run("version exists", func(t *testing.T) {
weh := testWorkflowExecutionEventHandler(t, newRegistry())
weh.changeVersions = map[string]Version{
"test": 2,
}
res := weh.GetVersion("test", 1, 3)
assert.Equal(t, Version(2), res)
})
t.Run("version doesn't exist in replay", func(t *testing.T) {
weh := testWorkflowExecutionEventHandler(t, newRegistry())
weh.isReplay = true
res := weh.GetVersion("test", DefaultVersion, 3)
assert.Equal(t, DefaultVersion, res)
require.Contains(t, weh.changeVersions, "test")
assert.Equal(t, DefaultVersion, weh.changeVersions["test"])
})
t.Run("version doesn't exist without replay", func(t *testing.T) {
weh := testWorkflowExecutionEventHandler(t, newRegistry())
res := weh.GetVersion("test", DefaultVersion, 3)
assert.Equal(t, Version(3), res)
require.Contains(t, weh.changeVersions, "test")
assert.Equal(t, Version(3), weh.changeVersions["test"])
assert.Equal(t, []byte(`["test-3"]`), weh.workflowInfo.SearchAttributes.IndexedFields[CadenceChangeVersion], "ensure search attributes are updated")
})
}

func testWorkflowExecutionEventHandler(t *testing.T, registry *registry) *workflowExecutionEventHandlerImpl {
return newWorkflowExecutionEventHandler(
testWorkflowInfo,
Expand Down

0 comments on commit aa18e1d

Please sign in to comment.