Skip to content

Commit 721bbb7

Browse files
committed
add tests in Test_MockGetVersion
1 parent ba9ac46 commit 721bbb7

File tree

3 files changed

+139
-3
lines changed

3 files changed

+139
-3
lines changed

internal/internal_workflow_testsuite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,7 @@ func (env *testWorkflowEnvironmentImpl) GetVersion(changeID string, minSupported
19751975
validateVersion(changeID, version, minSupported, maxSupported)
19761976

19771977
// If the version is not the DefaultVersion, update search attributes
1978-
// Keeping the DefaultVersion as a special case where no version marker is recorded
1978+
// Keeping the DefaultVersion as a special case where no search attributes are updated
19791979
if version != DefaultVersion {
19801980
env.UpsertSearchAttributes(createSearchAttributesForChangeVersion(changeID, version, env.changeVersions))
19811981
}

internal/internal_workflow_testsuite_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,142 @@ func (s *WorkflowTestSuiteUnitTest) Test_GetVersion_ExecuteWithVersion() {
13601360
env.AssertExpectations(s.T())
13611361
}
13621362

1363+
func (s *WorkflowTestSuiteUnitTest) Test_MockGetVersion_ExecuteWithVersion() {
1364+
oldActivity := func(ctx context.Context, msg string) (string, error) {
1365+
return "hello" + "_" + msg, nil
1366+
}
1367+
newActivity := func(ctx context.Context, msg string) (string, error) {
1368+
return "hello" + "_" + msg, nil
1369+
}
1370+
workflowFn := func(ctx Context) (string, error) {
1371+
ctx = WithActivityOptions(ctx, s.activityOptions)
1372+
var f Future
1373+
v1 := GetVersion(ctx, "change_1", DefaultVersion, 2, ExecuteWithVersion(1))
1374+
if v1 == DefaultVersion {
1375+
f = ExecuteActivity(ctx, oldActivity, "old1")
1376+
} else {
1377+
f = ExecuteActivity(ctx, newActivity, "new1")
1378+
}
1379+
var ret1 string
1380+
err := f.Get(ctx, &ret1) // wait for result
1381+
if err != nil {
1382+
return "", err
1383+
}
1384+
1385+
v2 := GetVersion(ctx, "change_2", DefaultVersion, 2)
1386+
if v2 == DefaultVersion {
1387+
f = ExecuteActivity(ctx, oldActivity, "old2")
1388+
} else {
1389+
f = ExecuteActivity(ctx, newActivity, "new2")
1390+
}
1391+
var ret2 string
1392+
err = f.Get(ctx, &ret2) // wait for result
1393+
if err != nil {
1394+
return "", err
1395+
}
1396+
1397+
// test searchable change version
1398+
wfInfo := GetWorkflowInfo(ctx)
1399+
s.NotNil(wfInfo.SearchAttributes)
1400+
changeVersionsBytes, ok := wfInfo.SearchAttributes.IndexedFields[CadenceChangeVersion]
1401+
s.True(ok)
1402+
var changeVersions []string
1403+
err = json.Unmarshal(changeVersionsBytes, &changeVersions)
1404+
s.NoError(err)
1405+
s.Equal(2, len(changeVersions))
1406+
s.Equal("change_2-2", changeVersions[0])
1407+
s.Equal("change_1--1", changeVersions[1])
1408+
1409+
return ret1 + ret2, err
1410+
}
1411+
1412+
env := s.NewTestWorkflowEnvironment()
1413+
env.RegisterWorkflow(workflowFn)
1414+
env.RegisterActivity(oldActivity)
1415+
env.RegisterActivity(newActivity)
1416+
1417+
env.OnGetVersion("change_1", DefaultVersion, 2).Return(func(string, Version, Version) Version {
1418+
return DefaultVersion
1419+
})
1420+
env.OnGetVersion(mock.Anything, DefaultVersion, 2).Return(Version(2))
1421+
env.ExecuteWorkflow(workflowFn)
1422+
1423+
s.True(env.IsWorkflowCompleted())
1424+
s.Nil(env.GetWorkflowError())
1425+
var ret string
1426+
s.NoError(env.GetWorkflowResult(&ret))
1427+
s.Equal("hello_old1hello_new2", ret)
1428+
env.AssertExpectations(s.T())
1429+
}
1430+
1431+
func (s *WorkflowTestSuiteUnitTest) Test_MockGetVersion_ExecuteWithMinVersion() {
1432+
oldActivity := func(ctx context.Context, msg string) (string, error) {
1433+
return "hello" + "_" + msg, nil
1434+
}
1435+
newActivity := func(ctx context.Context, msg string) (string, error) {
1436+
return "hello" + "_" + msg, nil
1437+
}
1438+
workflowFn := func(ctx Context) (string, error) {
1439+
ctx = WithActivityOptions(ctx, s.activityOptions)
1440+
var f Future
1441+
v1 := GetVersion(ctx, "change_1", DefaultVersion, 2)
1442+
if v1 == DefaultVersion {
1443+
f = ExecuteActivity(ctx, oldActivity, "old1")
1444+
} else {
1445+
f = ExecuteActivity(ctx, newActivity, "new1")
1446+
}
1447+
var ret1 string
1448+
err := f.Get(ctx, &ret1) // wait for result
1449+
if err != nil {
1450+
return "", err
1451+
}
1452+
1453+
v2 := GetVersion(ctx, "change_2", DefaultVersion, 2, ExecuteWithMinVersion())
1454+
if v2 == DefaultVersion {
1455+
f = ExecuteActivity(ctx, oldActivity, "old2")
1456+
} else {
1457+
f = ExecuteActivity(ctx, newActivity, "new2")
1458+
}
1459+
var ret2 string
1460+
err = f.Get(ctx, &ret2) // wait for result
1461+
if err != nil {
1462+
return "", err
1463+
}
1464+
1465+
// test searchable change version
1466+
wfInfo := GetWorkflowInfo(ctx)
1467+
s.NotNil(wfInfo.SearchAttributes)
1468+
changeVersionsBytes, ok := wfInfo.SearchAttributes.IndexedFields[CadenceChangeVersion]
1469+
s.True(ok)
1470+
var changeVersions []string
1471+
err = json.Unmarshal(changeVersionsBytes, &changeVersions)
1472+
s.NoError(err)
1473+
s.Equal(2, len(changeVersions))
1474+
s.Equal("change_2-2", changeVersions[0])
1475+
s.Equal("change_1--1", changeVersions[1])
1476+
1477+
return ret1 + ret2, err
1478+
}
1479+
1480+
env := s.NewTestWorkflowEnvironment()
1481+
env.RegisterWorkflow(workflowFn)
1482+
env.RegisterActivity(oldActivity)
1483+
env.RegisterActivity(newActivity)
1484+
1485+
env.OnGetVersion("change_1", DefaultVersion, 2).Return(func(string, Version, Version) Version {
1486+
return DefaultVersion
1487+
})
1488+
env.OnGetVersion(mock.Anything, DefaultVersion, 2).Return(Version(2))
1489+
env.ExecuteWorkflow(workflowFn)
1490+
1491+
s.True(env.IsWorkflowCompleted())
1492+
s.Nil(env.GetWorkflowError())
1493+
var ret string
1494+
s.NoError(env.GetWorkflowResult(&ret))
1495+
s.Equal("hello_old1hello_new2", ret)
1496+
env.AssertExpectations(s.T())
1497+
}
1498+
13631499
func (s *WorkflowTestSuiteUnitTest) Test_MockGetVersion() {
13641500
oldActivity := func(ctx context.Context, msg string) (string, error) {
13651501
return "hello" + "_" + msg, nil

internal/workflow_testsuite.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ func (t *TestWorkflowEnvironment) OnRequestCancelExternalWorkflow(domainName, wo
365365
}
366366

367367
// OnGetVersion setup a mock for workflow.GetVersion() call. By default, if mock is not setup, the GetVersion call from
368-
// workflow code will always return the maxSupported version. Make it not possible to test old version branch. With this
369-
// mock support, it is possible to test code branch for different versions.
368+
// workflow code will always return the maxSupported version or version specified by GetVersionOption.
369+
// Make it not possible to test old version branch. With this mock support, it is possible to test code branch for different versions.
370370
//
371371
// Note: mock can be setup for a specific changeID. Or if mock.Anything is used as changeID then all calls to GetVersion
372372
// will be mocked. Mock for a specific changeID has higher priority over mock.Anything.

0 commit comments

Comments
 (0)