From 745e9484e2a7d125c24ab637df157cd22cb2e316 Mon Sep 17 00:00:00 2001 From: Daniel Joos Date: Mon, 27 Nov 2023 18:57:37 +0100 Subject: [PATCH] Add `NotBefore` function to `testsuite.MockCallWrapper` (#1301) --- internal/workflow_testsuite.go | 11 +++++++++++ internal/workflow_testsuite_test.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/internal/workflow_testsuite.go b/internal/workflow_testsuite.go index 3dc57f1a3..7d0f6f125 100644 --- a/internal/workflow_testsuite.go +++ b/internal/workflow_testsuite.go @@ -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{}) { diff --git a/internal/workflow_testsuite_test.go b/internal/workflow_testsuite_test.go index 4496797d8..203db5461 100644 --- a/internal/workflow_testsuite_test.go +++ b/internal/workflow_testsuite_test.go @@ -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") +}