|
| 1 | +package recordworkflowtaskstarted |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + commonpb "go.temporal.io/api/common/v1" |
| 10 | + enumspb "go.temporal.io/api/enums/v1" |
| 11 | + "go.temporal.io/api/serviceerror" |
| 12 | + taskqueuepb "go.temporal.io/api/taskqueue/v1" |
| 13 | + "go.temporal.io/api/workflowservice/v1" |
| 14 | + "go.temporal.io/server/api/historyservice/v1" |
| 15 | + persistencespb "go.temporal.io/server/api/persistence/v1" |
| 16 | + "go.temporal.io/server/common/cluster" |
| 17 | + "go.temporal.io/server/common/namespace" |
| 18 | + serviceerrors "go.temporal.io/server/common/serviceerror" |
| 19 | + "go.temporal.io/server/service/history/api" |
| 20 | + "go.temporal.io/server/service/history/configs" |
| 21 | + "go.temporal.io/server/service/history/consts" |
| 22 | + historyi "go.temporal.io/server/service/history/interfaces" |
| 23 | + "go.temporal.io/server/service/history/tests" |
| 24 | + "go.uber.org/mock/gomock" |
| 25 | +) |
| 26 | + |
| 27 | +type mutableStateModifier func(*historyi.MockMutableState) |
| 28 | + |
| 29 | +func TestRecordWorkflowTaskStarted_Errors(t *testing.T) { |
| 30 | + t.Run("Stamp Mismatch", func(t *testing.T) { |
| 31 | + resp, err := invoke(t, func(mutableState *historyi.MockMutableState) { |
| 32 | + executionInfo := &persistencespb.WorkflowExecutionInfo{WorkflowTaskStamp: 1} |
| 33 | + mutableState.EXPECT().GetExecutionInfo().Return(executionInfo).Times(1) |
| 34 | + })() |
| 35 | + |
| 36 | + require.Nil(t, resp) |
| 37 | + require.Error(t, err) |
| 38 | + var obsoleteErr *serviceerrors.ObsoleteMatchingTask |
| 39 | + require.ErrorAs(t, err, &obsoleteErr) |
| 40 | + require.Contains(t, err.Error(), "Workflow task stamp mismatch") |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("Task Not Found", func(t *testing.T) { |
| 44 | + resp, err := invoke(t, func(mutableState *historyi.MockMutableState) { |
| 45 | + mutableState.EXPECT().GetWorkflowTaskByID(gomock.Any()).Return(nil).Times(1) |
| 46 | + })() |
| 47 | + |
| 48 | + require.Nil(t, resp) |
| 49 | + require.Error(t, err) |
| 50 | + var notFoundErr *serviceerror.NotFound |
| 51 | + require.ErrorAs(t, err, ¬FoundErr) |
| 52 | + require.Contains(t, err.Error(), "Workflow task not found") |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("Workflow Completed", func(t *testing.T) { |
| 56 | + resp, err := invoke(t, func(mutableState *historyi.MockMutableState) { |
| 57 | + mutableState.EXPECT().IsWorkflowExecutionRunning().Return(false).Times(1) |
| 58 | + })() |
| 59 | + |
| 60 | + require.Nil(t, resp) |
| 61 | + require.Error(t, err) |
| 62 | + require.Equal(t, consts.ErrWorkflowCompleted, err) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func invoke(t *testing.T, modifyMutableState mutableStateModifier) func() (*historyservice.RecordWorkflowTaskStartedResponseWithRawHistory, error) { |
| 67 | + ctx := context.Background() |
| 68 | + ctrl := gomock.NewController(t) |
| 69 | + t.Cleanup(ctrl.Finish) |
| 70 | + |
| 71 | + testNamespaceID := tests.NamespaceID |
| 72 | + workflowID := uuid.NewString() |
| 73 | + runID := uuid.NewString() |
| 74 | + scheduledEventID := int64(42) |
| 75 | + |
| 76 | + request := &historyservice.RecordWorkflowTaskStartedRequest{ |
| 77 | + NamespaceId: testNamespaceID.String(), |
| 78 | + WorkflowExecution: &commonpb.WorkflowExecution{ |
| 79 | + WorkflowId: workflowID, |
| 80 | + RunId: runID, |
| 81 | + }, |
| 82 | + ScheduledEventId: scheduledEventID, |
| 83 | + RequestId: "test-request", |
| 84 | + PollRequest: &workflowservice.PollWorkflowTaskQueueRequest{ |
| 85 | + Identity: "test-worker", |
| 86 | + TaskQueue: &taskqueuepb.TaskQueue{ |
| 87 | + Name: "test-tq", |
| 88 | + Kind: enumspb.TASK_QUEUE_KIND_NORMAL, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + mockNamespaceRegistry := namespace.NewMockRegistry(ctrl) |
| 94 | + mockNamespaceRegistry.EXPECT().GetNamespaceByID(testNamespaceID).Return(tests.GlobalNamespaceEntry, nil).AnyTimes() |
| 95 | + mockClusterMetadata := cluster.NewMockMetadata(ctrl) |
| 96 | + mockClusterMetadata.EXPECT().GetCurrentClusterName().Return("active").AnyTimes() |
| 97 | + shardContext := historyi.NewMockShardContext(ctrl) |
| 98 | + shardContext.EXPECT().GetNamespaceRegistry().Return(mockNamespaceRegistry).AnyTimes() |
| 99 | + shardContext.EXPECT().GetClusterMetadata().Return(mockClusterMetadata).AnyTimes() |
| 100 | + |
| 101 | + mutableState := historyi.NewMockMutableState(ctrl) |
| 102 | + modifyMutableState(mutableState) // must come before setting defaults |
| 103 | + mutableState.EXPECT().GetWorkflowTaskByID(scheduledEventID).Return(&historyi.WorkflowTaskInfo{ |
| 104 | + ScheduledEventID: scheduledEventID, |
| 105 | + Stamp: 0, |
| 106 | + }).AnyTimes() |
| 107 | + mutableState.EXPECT().IsWorkflowExecutionRunning().Return(true).AnyTimes() |
| 108 | + mutableState.EXPECT().GetExecutionInfo().Return(&persistencespb.WorkflowExecutionInfo{}).AnyTimes() |
| 109 | + |
| 110 | + mockWorkflowContext := historyi.NewMockWorkflowContext(ctrl) |
| 111 | + workflowLease := api.NewWorkflowLease(mockWorkflowContext, func(_ error) {}, mutableState) |
| 112 | + |
| 113 | + consistencyChecker := api.NewMockWorkflowConsistencyChecker(ctrl) |
| 114 | + consistencyChecker.EXPECT().GetWorkflowLease( |
| 115 | + gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), |
| 116 | + ).Return(workflowLease, nil) |
| 117 | + |
| 118 | + config := &configs.Config{} |
| 119 | + |
| 120 | + return func() (*historyservice.RecordWorkflowTaskStartedResponseWithRawHistory, error) { |
| 121 | + return Invoke(ctx, request, shardContext, config, nil, nil, consistencyChecker) |
| 122 | + } |
| 123 | +} |
0 commit comments