Skip to content

Commit

Permalink
Add NotBefore function to testsuite.MockCallWrapper (#1301)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljoos authored Nov 27, 2023
1 parent f5a0127 commit 745e948
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/workflow_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,17 @@ func (c *MockCallWrapper) Panic(msg string) *MockCallWrapper {
return c
}

// NotBefore indicates that a call to this mock must not happen before the given calls have happened as expected.
// It calls `NotBefore` on the wrapped mock call.
func (c *MockCallWrapper) NotBefore(calls ...*MockCallWrapper) *MockCallWrapper {
wrappedCalls := make([]*mock.Call, 0, len(calls))
for _, call := range calls {
wrappedCalls = append(wrappedCalls, call.call)
}
c.call.NotBefore(wrappedCalls...)
return c
}

// ExecuteWorkflow executes a workflow, wait until workflow complete. It will fail the test if workflow is blocked and
// cannot complete within TestTimeout (set by SetTestTimeout()).
func (e *TestWorkflowEnvironment) ExecuteWorkflow(workflowFn interface{}, args ...interface{}) {
Expand Down
21 changes: 21 additions & 0 deletions internal/workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,24 @@ func TestActivityMockingByNameWithoutRegistrationFails(t *testing.T) {
env := testSuite.NewTestWorkflowEnvironment()
assert.Panics(t, func() { env.OnActivity("SayHello", mock.Anything, mock.Anything) }, "The code did not panic")
}

func TestMockCallWrapperNotBefore(t *testing.T) {
testSuite := &WorkflowTestSuite{}
env := testSuite.NewTestWorkflowEnvironment()
env.RegisterActivity(namedActivity)

c1 := env.OnActivity(namedActivity, mock.Anything, "call1").Return("result1", nil)
env.OnActivity(namedActivity, mock.Anything, "call2").Return("result2", nil).NotBefore(c1)

env.ExecuteWorkflow(func(ctx Context) error {
ctx = WithLocalActivityOptions(ctx, LocalActivityOptions{
ScheduleToCloseTimeout: time.Hour,
StartToCloseTimeout: time.Hour,
})
var result string
return ExecuteLocalActivity(ctx, "namedActivity", "call2").Get(ctx, &result)
})
var expectedErr *PanicError
require.ErrorAs(t, env.GetWorkflowError(), &expectedErr)
require.ErrorContains(t, expectedErr, "Must not be called before")
}

0 comments on commit 745e948

Please sign in to comment.