From b258e62dc6dd514f829e3c565e5a883ffbd5a8d5 Mon Sep 17 00:00:00 2001 From: Ketsia <115650494+ketsiambaku@users.noreply.github.com> Date: Fri, 12 Apr 2024 19:59:19 +0200 Subject: [PATCH] [code-coverage] Add tests for HandleDecisionTaskStarted (#5906) * Add tests for HandleDecisionTaskFailed in service/history/decision * run linter * Add tests for HandleDecisionTaskStarted --- service/history/decision/handler_test.go | 121 +++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/service/history/decision/handler_test.go b/service/history/decision/handler_test.go index cc8e5936700..7eebd295941 100644 --- a/service/history/decision/handler_test.go +++ b/service/history/decision/handler_test.go @@ -48,6 +48,7 @@ import ( "github.com/uber/cadence/service/history/execution" "github.com/uber/cadence/service/history/query" "github.com/uber/cadence/service/history/shard" + "github.com/uber/cadence/service/history/workflow" ) const ( @@ -382,6 +383,126 @@ func (s *DecisionHandlerSuite) TestHandleDecisionTaskFailed() { } } +func (s *DecisionHandlerSuite) TestHandleDecisionTaskStarted() { + tests := []struct { + name string + domainID string + mutablestate *persistence.WorkflowMutableState + expectCalls func(h *handlerImpl) + expectErr error + }{ + { + name: "fail to retrieve domain From ID", + domainID: _testInvalidDomainUUID, + expectCalls: func(h *handlerImpl) {}, + expectErr: &types.BadRequestError{Message: "Invalid domain UUID."}, + mutablestate: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{}, + }, + }, + { + name: "failure - decision task already started", + domainID: _testDomainUUID, + expectCalls: func(h *handlerImpl) { + h.shard.(*shard.MockContext).EXPECT().GetEventsCache().Times(1).Return(events.NewMockCache(s.controller)) + }, + expectErr: &types.EventAlreadyStartedError{Message: "Decision task already started."}, + mutablestate: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{}, + }, + }, + { + name: "failure - workflow completed", + domainID: _testDomainUUID, + expectCalls: func(h *handlerImpl) { + h.shard.(*shard.MockContext).EXPECT().GetEventsCache().Times(1).Return(events.NewMockCache(s.controller)) + }, + expectErr: workflow.ErrNotExists, + mutablestate: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + State: 2, //2 == WorkflowStateCompleted + }, + }, + }, + { + name: "failure - decision task already completed", + domainID: _testDomainUUID, + expectCalls: func(h *handlerImpl) { + h.shard.(*shard.MockContext).EXPECT().GetEventsCache().Times(1).Return(events.NewMockCache(s.controller)) + }, + expectErr: &types.EntityNotExistsError{Message: "Decision task not found."}, + mutablestate: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + DecisionScheduleID: 1, + NextEventID: 2, + }, + }, + }, + { + name: "failure - cached mutable state is stale", + domainID: _testDomainUUID, + expectCalls: func(h *handlerImpl) { + // handler will attempt reloading mutable state at most 5 times + // this test will fail all retries + h.shard.(*shard.MockContext).EXPECT().GetEventsCache().Times(5).Return(events.NewMockCache(s.controller)) + }, + expectErr: workflow.ErrMaxAttemptsExceeded, + mutablestate: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + DecisionScheduleID: 1, + }, + }, + }, + { + name: "success", + domainID: _testDomainUUID, + expectCalls: func(h *handlerImpl) { + h.shard.(*shard.MockContext).EXPECT().GetEventsCache().Times(1).Return(events.NewMockCache(s.controller)) + }, + expectErr: nil, + mutablestate: &persistence.WorkflowMutableState{ + ExecutionInfo: &persistence.WorkflowExecutionInfo{ + DecisionScheduleID: 0, + NextEventID: 3, + DecisionRequestID: "test-request-id", + }, + }, + }, + } + + for _, test := range tests { + s.Run(test.name, func() { + request := &types.RecordDecisionTaskStartedRequest{ + DomainUUID: test.domainID, + WorkflowExecution: &types.WorkflowExecution{ + WorkflowID: _testWorkflowID, + RunID: _testRunID, + }, + ScheduleID: 0, + TaskID: 0, + RequestID: "test-request-id", + PollRequest: &types.PollForDecisionTaskRequest{ + Domain: test.domainID, + TaskList: nil, + Identity: "", + BinaryChecksum: "", + }, + } + shardContext := shard.NewMockContext(s.controller) + s.decisionHandler.shard = shardContext + s.expectCommonCalls(test.domainID, test.mutablestate) + s.decisionHandler.executionCache = execution.NewCache(shardContext) + test.expectCalls(s.decisionHandler) + + resp, err := s.decisionHandler.HandleDecisionTaskStarted(context.Background(), request) + s.Equal(test.expectErr, err) + if err == nil { + s.NotNil(resp) + } + }) + } +} + func (s *DecisionHandlerSuite) TestHandleBufferedQueries_ClientNotSupports() { s.mockMutableState.EXPECT().GetQueryRegistry().Return(s.queryRegistry) s.assertQueryCounts(s.queryRegistry, 10, 0, 0, 0)