Skip to content

Commit a01ec47

Browse files
authored
fix unit test framework (#233)
1 parent 614a82a commit a01ec47

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

internal_workflow_testsuite.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,13 @@ func (w *workflowExecutorWrapper) Execute(ctx Context, input []byte) (result []b
770770
}, false)
771771
}
772772

773+
if !env.isChildWorkflow() {
774+
// This is to prevent auto-forwarding mock clock before main workflow starts. For child workflow, we increase
775+
// the counter in env.ExecuteChildWorkflow(). We cannot do it here for child workflow, because we need to make
776+
// sure the counter is increased before returning from ExecuteChildWorkflow().
777+
env.runningCount.Inc()
778+
}
779+
773780
m := &mockWrapper{env: env, name: w.name, fn: w.fn, isWorkflow: true}
774781
// This method is called by workflow's dispatcher. In this test suite, it is run in the main loop. We cannot block
775782
// the main loop, but the mock could block if it is configured to wait. So we need to use a separate goroutinue to
@@ -790,12 +797,9 @@ func (w *workflowExecutorWrapper) Execute(ctx Context, input []byte) (result []b
790797
// ExecuteUntilAllBlocked() so the main loop is not blocked. The dispatcher will unblock when getMockReturn() returns.
791798
mockReadyChannel.Receive(ctx, &mockRet)
792799

793-
if env.isChildWorkflow() {
794-
env.postCallback(func() {
795-
// reduce runningCount after current workflow dispatcher run is blocked (aka ExecuteUntilAllBlocked() returns).
796-
env.runningCount.Dec()
797-
}, false)
798-
}
800+
// reduce runningCount to allow auto-forwarding mock clock after current workflow dispatcher run is blocked (aka
801+
// ExecuteUntilAllBlocked() returns).
802+
env.runningCount.Dec()
799803

800804
if mockRet != nil {
801805
return m.executeMock(ctx, input, mockRet)

internal_workflow_testsuite_test.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,36 +1045,29 @@ func (s *WorkflowTestSuiteUnitTest) Test_WorkflowFullyQualifiedName() {
10451045

10461046
func (s *WorkflowTestSuiteUnitTest) Test_QueryWorkflow() {
10471047
queryType := "state"
1048-
stateWaitTimer, stateWaitActivity, stateDone := "wait for timer", "wait for activity", "done"
1048+
stateWaitSignal, stateWaitActivity, stateDone := "wait for signal", "wait for activity", "done"
10491049
workflowFn := func(ctx Context) error {
10501050
var state string
10511051
err := SetQueryHandler(ctx, queryType, func() (string, error) {
10521052
return state, nil
10531053
})
10541054
if err != nil {
1055-
state = err.Error()
10561055
return err
10571056
}
10581057

1059-
state = stateWaitTimer
1060-
err = NewTimer(ctx, time.Hour).Get(ctx, nil)
1061-
if err != nil {
1062-
state = err.Error()
1063-
return err
1064-
}
1058+
state = stateWaitSignal
1059+
var signalData string
1060+
GetSignalChannel(ctx, "query-signal").Receive(ctx, &signalData)
10651061

10661062
state = stateWaitActivity
10671063
ctx = WithActivityOptions(ctx, s.activityOptions)
10681064
err = ExecuteActivity(ctx, testActivityHello, "mock_delay").Get(ctx, nil)
10691065
if err != nil {
1070-
state = err.Error()
10711066
return err
10721067
}
1073-
10741068
state = stateDone
10751069
return err
10761070
}
1077-
10781071
RegisterWorkflow(workflowFn)
10791072

10801073
env := s.NewTestWorkflowEnvironment()
@@ -1086,9 +1079,10 @@ func (s *WorkflowTestSuiteUnitTest) Test_QueryWorkflow() {
10861079
s.NoError(err)
10871080
s.Equal(expected, state)
10881081
}
1089-
env.SetOnTimerScheduledListener(func(timerID string, duration time.Duration) {
1090-
verifyStateWithQuery(stateWaitTimer)
1091-
})
1082+
env.RegisterDelayedCallback(func() {
1083+
verifyStateWithQuery(stateWaitSignal)
1084+
env.SignalWorkflow("query-signal", "hello-query")
1085+
}, time.Hour)
10921086
env.OnActivity(testActivityHello, mock.Anything, mock.Anything).After(time.Hour).Return("hello_mock", nil)
10931087
env.SetOnActivityStartedListener(func(activityInfo *ActivityInfo, ctx context.Context, args EncodedValues) {
10941088
verifyStateWithQuery(stateWaitActivity)
@@ -1097,5 +1091,6 @@ func (s *WorkflowTestSuiteUnitTest) Test_QueryWorkflow() {
10971091

10981092
s.True(env.IsWorkflowCompleted())
10991093
s.NoError(env.GetWorkflowError())
1094+
env.AssertExpectations(s.T())
11001095
verifyStateWithQuery(stateDone)
11011096
}

0 commit comments

Comments
 (0)