From 5eb913da791d39453e364e11a9f1323e43c183a1 Mon Sep 17 00:00:00 2001 From: agautam478 <72432016+agautam478@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:24:38 -0700 Subject: [PATCH] Tests for the stray lines needing coverage. (#6098) * Covering the stray lines in various files * One more test case * One more test case --- .../frontend/wrappers/metered/metered_test.go | 8 + .../start_workflow_execution_test.go | 37 ++- .../terminate_workflow_execution_test.go | 215 ++++++++++++++++++ 3 files changed, 258 insertions(+), 2 deletions(-) diff --git a/service/frontend/wrappers/metered/metered_test.go b/service/frontend/wrappers/metered/metered_test.go index 59d70e2a1ee..7f6307d46d6 100644 --- a/service/frontend/wrappers/metered/metered_test.go +++ b/service/frontend/wrappers/metered/metered_test.go @@ -30,6 +30,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/uber-go/tally" + "go.uber.org/yarpc/yarpcerrors" "github.com/uber/cadence/common/cache" "github.com/uber/cadence/common/dynamicconfig" @@ -714,6 +715,13 @@ func TestHandleErr(t *testing.T) { expectedErrMsg: "client version not supported", expectedCounter: "test.cadence_err_client_version_not_supported_counter+", }, + { + name: "YARPCDeadlineExceededError", + err: yarpcerrors.Newf(yarpcerrors.CodeDeadlineExceeded, "deadline exceeded"), + expectedErrType: &yarpcerrors.Status{}, + expectedErrMsg: "deadline exceeded", + expectedCounter: "test.cadence_err_context_timeout_counter+", + }, { name: "ContextDeadlineExceeded", err: context.DeadlineExceeded, diff --git a/service/history/engine/engineimpl/start_workflow_execution_test.go b/service/history/engine/engineimpl/start_workflow_execution_test.go index 7764e2db29f..036d7d30f74 100644 --- a/service/history/engine/engineimpl/start_workflow_execution_test.go +++ b/service/history/engine/engineimpl/start_workflow_execution_test.go @@ -144,7 +144,7 @@ func TestStartWorkflowExecution(t *testing.T) { Identity: "workflow-starter", RequestID: "request-id-for-start", WorkflowType: &types.WorkflowType{Name: "workflow-type"}, - WorkflowIDReusePolicy: (*types.WorkflowIDReusePolicy)(common.Int32Ptr(1)), + WorkflowIDReusePolicy: types.WorkflowIDReusePolicyAllowDuplicate.Ptr(), }, }, setupMocks: func(t *testing.T, eft *testdata.EngineForTest) { @@ -179,7 +179,7 @@ func TestStartWorkflowExecution(t *testing.T) { Identity: "workflow-starter", RequestID: "request-id-for-start", WorkflowType: &types.WorkflowType{Name: "workflow-type"}, - WorkflowIDReusePolicy: (*types.WorkflowIDReusePolicy)(common.Int32Ptr(3)), + WorkflowIDReusePolicy: types.WorkflowIDReusePolicyTerminateIfRunning.Ptr(), }, }, setupMocks: func(t *testing.T, eft *testdata.EngineForTest) { @@ -197,6 +197,39 @@ func TestStartWorkflowExecution(t *testing.T) { }, wantErr: false, }, + { + name: "workflow ID reuse policy - reject duplicate", + request: &types.HistoryStartWorkflowExecutionRequest{ + DomainUUID: constants.TestDomainID, + StartRequest: &types.StartWorkflowExecutionRequest{ + Domain: constants.TestDomainName, + WorkflowID: "workflow-id", + WorkflowType: &types.WorkflowType{Name: "workflow-type"}, + TaskList: &types.TaskList{Name: "default-task-list"}, + ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(3600), // 1 hour + TaskStartToCloseTimeoutSeconds: common.Int32Ptr(10), // 10 seconds + Identity: "workflow-starter", + RequestID: "request-id-for-start", + WorkflowIDReusePolicy: types.WorkflowIDReusePolicyRejectDuplicate.Ptr(), + }, + }, + setupMocks: func(t *testing.T, eft *testdata.EngineForTest) { + domainEntry := &cache.DomainCacheEntry{} + eft.ShardCtx.Resource.DomainCache.EXPECT().GetDomainByID(constants.TestDomainID).Return(domainEntry, nil).AnyTimes() + + eft.ShardCtx.Resource.ExecutionMgr.On("CreateWorkflowExecution", mock.Anything, mock.Anything).Return(nil, &persistence.WorkflowExecutionAlreadyStartedError{ + StartRequestID: "existing-request-id", + RunID: "existing-run-id", + }).Once() + eft.ShardCtx.Resource.ShardMgr. + On("UpdateShard", mock.Anything, mock.Anything). + Return(nil) + historyV2Mgr := eft.ShardCtx.Resource.HistoryMgr + historyV2Mgr.On("AppendHistoryNodes", mock.Anything, mock.AnythingOfType("*persistence.AppendHistoryNodesRequest")). + Return(&persistence.AppendHistoryNodesResponse{}, nil).Once() + }, + wantErr: true, + }, } for _, tc := range tests { diff --git a/service/history/engine/engineimpl/terminate_workflow_execution_test.go b/service/history/engine/engineimpl/terminate_workflow_execution_test.go index fa75a3598bb..33b6ed44131 100644 --- a/service/history/engine/engineimpl/terminate_workflow_execution_test.go +++ b/service/history/engine/engineimpl/terminate_workflow_execution_test.go @@ -326,6 +326,221 @@ func TestTerminateWorkflowExecution(t *testing.T) { }, wantErr: false, }, + { + name: "first execution run ID matches", + terminationRequest: types.HistoryTerminateWorkflowExecutionRequest{ + DomainUUID: constants.TestDomainID, + TerminateRequest: &types.TerminateWorkflowExecutionRequest{ + Domain: constants.TestDomainName, + WorkflowExecution: &types.WorkflowExecution{WorkflowID: constants.TestWorkflowID, RunID: constants.TestRunID}, + Reason: "Test termination", + Identity: "testRunner", + FirstExecutionRunID: "matching-first-run-id", + }, + }, + setupMocks: func(t *testing.T, eft *testdata.EngineForTest) { + getExecReq := &persistence.GetWorkflowExecutionRequest{ + DomainID: constants.TestDomainID, + Execution: types.WorkflowExecution{WorkflowID: constants.TestWorkflowID, RunID: constants.TestRunID}, + DomainName: constants.TestDomainName, + RangeID: 1, + } + getExecResp := &persistence.GetWorkflowExecutionResponse{ + State: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + DomainID: constants.TestDomainID, + WorkflowID: constants.TestWorkflowID, + RunID: constants.TestRunID, + FirstExecutionRunID: "matching-first-run-id", + }, + ExecutionStats: &persistence.ExecutionStats{}, + }, + MutableStateStats: &persistence.MutableStateStats{}, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("GetWorkflowExecution", mock.Anything, getExecReq). + Return(getExecResp, nil).Once() + + getCurrentExecReq := &persistence.GetCurrentExecutionRequest{ + DomainID: constants.TestDomainID, + WorkflowID: constants.TestWorkflowID, + DomainName: constants.TestDomainName, + } + getCurrentExecResp := &persistence.GetCurrentExecutionResponse{ + RunID: constants.TestRunID, + State: persistence.WorkflowStateRunning, + CloseStatus: persistence.WorkflowCloseStatusNone, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("GetCurrentExecution", mock.Anything, getCurrentExecReq). + Return(getCurrentExecResp, nil).Once() + + historyMgr := eft.ShardCtx.Resource.HistoryMgr + historyMgr. + On("ReadHistoryBranch", mock.Anything, mock.Anything). + Return(&persistence.ReadHistoryBranchResponse{}, nil). + Once() + + updateExecResp := &persistence.UpdateWorkflowExecutionResponse{ + MutableStateUpdateSessionStats: &persistence.MutableStateUpdateSessionStats{}, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("UpdateWorkflowExecution", mock.Anything, mock.Anything). + Return(updateExecResp, nil).Once() + + eft.ShardCtx.Resource.ShardMgr. + On("UpdateShard", mock.Anything, mock.Anything). + Return(nil) + + historyV2Mgr := eft.ShardCtx.Resource.HistoryMgr + historyV2Mgr.On("AppendHistoryNodes", mock.Anything, mock.AnythingOfType("*persistence.AppendHistoryNodesRequest")). + Return(&persistence.AppendHistoryNodesResponse{}, nil).Once() + }, + wantErr: false, + }, + { + name: "first execution run ID does not match", + terminationRequest: types.HistoryTerminateWorkflowExecutionRequest{ + DomainUUID: constants.TestDomainID, + TerminateRequest: &types.TerminateWorkflowExecutionRequest{ + Domain: constants.TestDomainName, + WorkflowExecution: &types.WorkflowExecution{WorkflowID: constants.TestWorkflowID, RunID: constants.TestRunID}, + Reason: "Test termination", + Identity: "testRunner", + FirstExecutionRunID: "non-matching-first-run-id", + }, + }, + setupMocks: func(t *testing.T, eft *testdata.EngineForTest) { + getExecReq := &persistence.GetWorkflowExecutionRequest{ + DomainID: constants.TestDomainID, + Execution: types.WorkflowExecution{WorkflowID: constants.TestWorkflowID, RunID: constants.TestRunID}, + DomainName: constants.TestDomainName, + RangeID: 1, + } + getExecResp := &persistence.GetWorkflowExecutionResponse{ + State: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + DomainID: constants.TestDomainID, + WorkflowID: constants.TestWorkflowID, + RunID: constants.TestRunID, + FirstExecutionRunID: "matching-first-run-id", + }, + ExecutionStats: &persistence.ExecutionStats{}, + }, + MutableStateStats: &persistence.MutableStateStats{}, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("GetWorkflowExecution", mock.Anything, getExecReq). + Return(getExecResp, nil).Once() + + getCurrentExecReq := &persistence.GetCurrentExecutionRequest{ + DomainID: constants.TestDomainID, + WorkflowID: constants.TestWorkflowID, + DomainName: constants.TestDomainName, + } + getCurrentExecResp := &persistence.GetCurrentExecutionResponse{ + RunID: constants.TestRunID, + State: persistence.WorkflowStateRunning, + CloseStatus: persistence.WorkflowCloseStatusNone, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("GetCurrentExecution", mock.Anything, getCurrentExecReq). + Return(getCurrentExecResp, nil).Once() + + historyMgr := eft.ShardCtx.Resource.HistoryMgr + historyMgr. + On("ReadHistoryBranch", mock.Anything, mock.Anything). + Return(&persistence.ReadHistoryBranchResponse{}, nil). + Once() + + eft.ShardCtx.Resource.ExecutionMgr. + On("UpdateWorkflowExecution", mock.Anything, mock.Anything). + Return(&persistence.UpdateWorkflowExecutionResponse{}, nil).Once() + }, + wantErr: true, + }, + { + name: "load first execution run ID from start event", + terminationRequest: types.HistoryTerminateWorkflowExecutionRequest{ + DomainUUID: constants.TestDomainID, + TerminateRequest: &types.TerminateWorkflowExecutionRequest{ + Domain: constants.TestDomainName, + WorkflowExecution: &types.WorkflowExecution{WorkflowID: constants.TestWorkflowID, RunID: constants.TestRunID}, + Reason: "Test termination", + Identity: "testRunner", + FirstExecutionRunID: "fetched-first-run-id", + }, + }, + setupMocks: func(t *testing.T, eft *testdata.EngineForTest) { + getExecReq := &persistence.GetWorkflowExecutionRequest{ + DomainID: constants.TestDomainID, + Execution: types.WorkflowExecution{WorkflowID: constants.TestWorkflowID, RunID: constants.TestRunID}, + DomainName: constants.TestDomainName, + RangeID: 1, + } + getExecResp := &persistence.GetWorkflowExecutionResponse{ + State: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + DomainID: constants.TestDomainID, + WorkflowID: constants.TestWorkflowID, + RunID: constants.TestRunID, + FirstExecutionRunID: "", + }, + ExecutionStats: &persistence.ExecutionStats{}, + }, + MutableStateStats: &persistence.MutableStateStats{}, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("GetWorkflowExecution", mock.Anything, getExecReq). + Return(getExecResp, nil).Once() + + getCurrentExecReq := &persistence.GetCurrentExecutionRequest{ + DomainID: constants.TestDomainID, + WorkflowID: constants.TestWorkflowID, + DomainName: constants.TestDomainName, + } + getCurrentExecResp := &persistence.GetCurrentExecutionResponse{ + RunID: constants.TestRunID, + State: persistence.WorkflowStateRunning, + CloseStatus: persistence.WorkflowCloseStatusNone, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("GetCurrentExecution", mock.Anything, getCurrentExecReq). + Return(getCurrentExecResp, nil).Once() + + historyBranchResp := &persistence.ReadHistoryBranchResponse{ + HistoryEvents: []*types.HistoryEvent{ + { + ID: 1, + WorkflowExecutionStartedEventAttributes: &types.WorkflowExecutionStartedEventAttributes{ + FirstExecutionRunID: "fetched-first-run-id", + }, + }, + }, + } + historyMgr := eft.ShardCtx.Resource.HistoryMgr + historyMgr. + On("ReadHistoryBranch", mock.Anything, mock.Anything). + Return(historyBranchResp, nil). + Once() + + updateExecResp := &persistence.UpdateWorkflowExecutionResponse{ + MutableStateUpdateSessionStats: &persistence.MutableStateUpdateSessionStats{}, + } + eft.ShardCtx.Resource.ExecutionMgr. + On("UpdateWorkflowExecution", mock.Anything, mock.Anything). + Return(updateExecResp, nil).Once() + + eft.ShardCtx.Resource.ShardMgr. + On("UpdateShard", mock.Anything, mock.Anything). + Return(nil) + + historyV2Mgr := eft.ShardCtx.Resource.HistoryMgr + historyV2Mgr.On("AppendHistoryNodes", mock.Anything, mock.AnythingOfType("*persistence.AppendHistoryNodesRequest")). + Return(&persistence.AppendHistoryNodesResponse{}, nil).Once() + }, + wantErr: false, + }, } for _, tc := range tests {