Skip to content

Commit 34d273c

Browse files
committed
add support of ExecuteWithMinVersion and ExecuteWithVersion to testWorkflowEnvironmentImpl
1 parent f63ed38 commit 34d273c

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

internal/internal_event_handlers.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,6 @@ func (wc *workflowEnvironmentImpl) GetVersion(changeID string, minSupported, max
655655
// Store the version in the changeVersions
656656
// ensuring that it can be retrieved later
657657
wc.changeVersions[changeID] = version
658-
659658
return version
660659
}
661660

internal/internal_workflow_testsuite.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,9 +1947,43 @@ func (env *testWorkflowEnvironmentImpl) GetVersion(changeID string, minSupported
19471947
validateVersion(changeID, version, minSupported, maxSupported)
19481948
return version
19491949
}
1950-
env.UpsertSearchAttributes(createSearchAttributesForChangeVersion(changeID, maxSupported, env.changeVersions))
1951-
env.changeVersions[changeID] = maxSupported
1952-
return maxSupported
1950+
1951+
// Apply options to determine which version to use
1952+
options := &GetVersionOptions{}
1953+
for _, opt := range opts {
1954+
opt(options)
1955+
}
1956+
1957+
// Determine the version to use based on the options provided
1958+
var version Version
1959+
switch {
1960+
// If ExecuteWithVersion option is used, use the custom version provided
1961+
case options.CustomVersion != nil:
1962+
version = *options.CustomVersion
1963+
1964+
// If ExecuteWithMinVersion option is set, use the minimum supported version
1965+
case options.UseMinVersion:
1966+
version = minSupported
1967+
1968+
// Otherwise, use the maximum supported version
1969+
default:
1970+
version = maxSupported
1971+
}
1972+
1973+
// Validate the version against the min and max supported versions
1974+
// ensuring it is within the acceptable range
1975+
validateVersion(changeID, version, minSupported, maxSupported)
1976+
1977+
// If the version is not the DefaultVersion, update search attributes
1978+
// Keeping the DefaultVersion as a special case where no version marker is recorded
1979+
if version != DefaultVersion {
1980+
env.UpsertSearchAttributes(createSearchAttributesForChangeVersion(changeID, version, env.changeVersions))
1981+
}
1982+
1983+
// Store the version in the changeVersions
1984+
// ensuring that it can be retrieved later
1985+
env.changeVersions[changeID] = version
1986+
return version
19531987
}
19541988

19551989
func (env *testWorkflowEnvironmentImpl) getMockedVersion(mockedChangeID, changeID string, minSupported, maxSupported Version) (Version, bool) {

internal/internal_workflow_testsuite_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,92 @@ func (s *WorkflowTestSuiteUnitTest) Test_GetVersion() {
12741274
env.AssertExpectations(s.T())
12751275
}
12761276

1277+
func (s *WorkflowTestSuiteUnitTest) Test_GetVersion_ExecuteWithMinVersion() {
1278+
oldActivity := func(ctx context.Context, msg string) (string, error) {
1279+
return "hello" + "_" + msg, nil
1280+
}
1281+
newActivity := func(ctx context.Context, msg string) (string, error) {
1282+
return "hello" + "_" + msg, nil
1283+
}
1284+
workflowFn := func(ctx Context) error {
1285+
ctx = WithActivityOptions(ctx, s.activityOptions)
1286+
var f Future
1287+
v := GetVersion(ctx, "test_change_id", DefaultVersion, 2, ExecuteWithMinVersion())
1288+
if v == DefaultVersion {
1289+
f = ExecuteActivity(ctx, oldActivity, "ols_msg")
1290+
} else {
1291+
f = ExecuteActivity(ctx, newActivity, "new_msg")
1292+
}
1293+
err := f.Get(ctx, nil) // wait for result
1294+
if err != nil {
1295+
return err
1296+
}
1297+
1298+
// test no search attributes
1299+
wfInfo := GetWorkflowInfo(ctx)
1300+
s.Nil(wfInfo.SearchAttributes)
1301+
return err
1302+
}
1303+
1304+
env := s.NewTestWorkflowEnvironment()
1305+
env.RegisterWorkflow(workflowFn)
1306+
env.RegisterActivity(oldActivity)
1307+
env.RegisterActivity(newActivity)
1308+
env.OnActivity(oldActivity, mock.Anything, "ols_msg").Return("hello_ols_msg", nil).Once()
1309+
env.ExecuteWorkflow(workflowFn)
1310+
1311+
s.True(env.IsWorkflowCompleted())
1312+
s.Nil(env.GetWorkflowError())
1313+
env.AssertExpectations(s.T())
1314+
}
1315+
1316+
func (s *WorkflowTestSuiteUnitTest) Test_GetVersion_ExecuteWithVersion() {
1317+
oldActivity := func(ctx context.Context, msg string) (string, error) {
1318+
return "hello" + "_" + msg, nil
1319+
}
1320+
newActivity := func(ctx context.Context, msg string) (string, error) {
1321+
return "hello" + "_" + msg, nil
1322+
}
1323+
workflowFn := func(ctx Context) error {
1324+
ctx = WithActivityOptions(ctx, s.activityOptions)
1325+
var f Future
1326+
v := GetVersion(ctx, "test_change_id", DefaultVersion, 2, ExecuteWithVersion(1))
1327+
if v == DefaultVersion {
1328+
f = ExecuteActivity(ctx, oldActivity, "ols_msg")
1329+
} else {
1330+
f = ExecuteActivity(ctx, newActivity, "new_msg")
1331+
}
1332+
err := f.Get(ctx, nil) // wait for result
1333+
if err != nil {
1334+
return err
1335+
}
1336+
1337+
// test searchable change version
1338+
wfInfo := GetWorkflowInfo(ctx)
1339+
s.NotNil(wfInfo.SearchAttributes)
1340+
changeVersionsBytes, ok := wfInfo.SearchAttributes.IndexedFields[CadenceChangeVersion]
1341+
s.True(ok)
1342+
var changeVersions []string
1343+
err = json.Unmarshal(changeVersionsBytes, &changeVersions)
1344+
s.NoError(err)
1345+
s.Equal(1, len(changeVersions))
1346+
s.Equal("test_change_id-1", changeVersions[0])
1347+
1348+
return err
1349+
}
1350+
1351+
env := s.NewTestWorkflowEnvironment()
1352+
env.RegisterWorkflow(workflowFn)
1353+
env.RegisterActivity(oldActivity)
1354+
env.RegisterActivity(newActivity)
1355+
env.OnActivity(newActivity, mock.Anything, "new_msg").Return("hello new_mock_msg", nil).Once()
1356+
env.ExecuteWorkflow(workflowFn)
1357+
1358+
s.True(env.IsWorkflowCompleted())
1359+
s.Nil(env.GetWorkflowError())
1360+
env.AssertExpectations(s.T())
1361+
}
1362+
12771363
func (s *WorkflowTestSuiteUnitTest) Test_MockGetVersion() {
12781364
oldActivity := func(ctx context.Context, msg string) (string, error) {
12791365
return "hello" + "_" + msg, nil

0 commit comments

Comments
 (0)