Skip to content

Commit

Permalink
Add unit tests for SignalWithStartWorkflowExecution, ResetWorkflowExe…
Browse files Browse the repository at this point in the history
…cution and TerminateWorkflowExecution methods in frontend api handler
  • Loading branch information
timl3136 committed Jun 7, 2024
1 parent b0ae76a commit 0ca3c74
Showing 1 changed file with 346 additions and 0 deletions.
346 changes: 346 additions & 0 deletions service/frontend/api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3817,3 +3817,349 @@ func TestDescribeWorkflowExecution(t *testing.T) {
})
}
}

func (s *workflowHandlerSuite) TestSignalWithStartWorkflowExecution() {
config := s.newConfig(dc.NewInMemoryClient())
config.EnableClientVersionCheck = dc.GetBoolPropertyFn(true)
wh := NewWorkflowHandler(s.mockResource, config, s.mockVersionChecker, nil)
wh.tokenSerializer = s.mockTokenSerializer

validRequest := &types.SignalWithStartWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowID: testWorkflowID,
Identity: "identity",
SignalName: "signal",
Input: nil,
WorkflowType: &types.WorkflowType{Name: "wType"},
TaskList: &types.TaskList{Name: "taskList"},
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(10),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(10),
}

testInput := map[string]struct {
request *types.SignalWithStartWorkflowExecutionRequest
expectError bool
mockFn func()
}{
"shutting down": {
request: validRequest,
mockFn: func() {
wh.shuttingDown = int32(1)
},
expectError: true,
},
"nil request": {
request: nil,
mockFn: func() {},
expectError: true,
},
"empty domain": {
request: &types.SignalWithStartWorkflowExecutionRequest{
Domain: "",
},
mockFn: func() {},
expectError: true,
},
"empty workflow ID": {
request: &types.SignalWithStartWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowID: "",
},
mockFn: func() {},
expectError: true,
},
"empty workflow type": {
request: &types.SignalWithStartWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowID: testWorkflowID,
WorkflowType: nil,
},
mockFn: func() {},
expectError: true,
},
"cannot get domain ID": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil)
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return("", errors.New("error getting domain ID"))
},
expectError: true,
},
"history client error": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil).Times(2)
s.mockHistoryClient.EXPECT().SignalWithStartWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, errors.New("error"))
},
expectError: true,
},
"success": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil).Times(2)
s.mockHistoryClient.EXPECT().SignalWithStartWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, nil)
},
expectError: false,
},
}

for name, input := range testInput {
s.Run(name, func() {
input.mockFn()
_, err := wh.SignalWithStartWorkflowExecution(context.Background(), input.request)
if input.expectError {
s.Error(err)
} else {
s.NoError(err)
}
wh.shuttingDown = int32(0)
})
}
}

func (s *workflowHandlerSuite) TestResetWorkflowExecution() {
config := s.newConfig(dc.NewInMemoryClient())
config.EnableClientVersionCheck = dc.GetBoolPropertyFn(true)
wh := NewWorkflowHandler(s.mockResource, config, s.mockVersionChecker, nil)
wh.tokenSerializer = s.mockTokenSerializer

validRequest := &types.ResetWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: testWorkflowID,
RunID: testRunID,
},
Reason: "reason",
}

testInput := map[string]struct {
request *types.ResetWorkflowExecutionRequest
expectError bool
mockFn func()
expectErrorType error
}{
"shutting down": {
request: validRequest,
mockFn: func() {
wh.shuttingDown = int32(1)
},
expectError: true,
expectErrorType: validate.ErrShuttingDown,
},
"nil request": {
request: nil,
mockFn: func() {},
expectError: true,
expectErrorType: validate.ErrRequestNotSet,
},
"empty domain": {
request: &types.ResetWorkflowExecutionRequest{
Domain: "",
},
mockFn: func() {},
expectError: true,
expectErrorType: validate.ErrDomainNotSet,
},
"empty workflow ID": {
request: &types.ResetWorkflowExecutionRequest{
Domain: s.testDomain,
},
mockFn: func() {},
expectError: true,
},
"empty workflow execution": {
request: &types.ResetWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowExecution: nil,
},
mockFn: func() {},
expectError: true,
},
"cannot get domain ID": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return("", errors.New("error getting domain ID"))
},
expectError: true,
},
"history client error": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil)
s.mockHistoryClient.EXPECT().ResetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, errors.New("error"))
},
expectError: true,
},
"success": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil)
s.mockHistoryClient.EXPECT().ResetWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, nil)
},
expectError: false,
},
}

for name, input := range testInput {
s.Run(name, func() {
input.mockFn()
_, err := wh.ResetWorkflowExecution(context.Background(), input.request)
if input.expectError {
s.Error(err)
if input.expectErrorType != nil {
s.ErrorIs(err, input.expectErrorType)
}
} else {
s.NoError(err)
}
wh.shuttingDown = int32(0)
})
}

// test version checker
s.Run("version checker", func() {
mockCtrl := gomock.NewController(s.T())
mockResource := resource.NewTest(s.T(), mockCtrl, metrics.Frontend)
mockVersionChecker := client.NewMockVersionChecker(mockCtrl)

cfg := frontendcfg.NewConfig(
dc.NewCollection(
dc.NewInMemoryClient(),
mockResource.GetLogger(),
),
numHistoryShards,
false,
"hostname",
)
cfg.EnableClientVersionCheck = dc.GetBoolPropertyFn(true)
wh := NewWorkflowHandler(mockResource, cfg, mockVersionChecker, nil)
mockVersionChecker.EXPECT().ClientSupported(gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
_, err := wh.ResetWorkflowExecution(context.Background(), validRequest)
s.Error(err)
})
}

func (s *workflowHandlerSuite) TestTerminateWorkflowExecution() {
config := s.newConfig(dc.NewInMemoryClient())
config.EnableClientVersionCheck = dc.GetBoolPropertyFn(true)
wh := NewWorkflowHandler(s.mockResource, config, s.mockVersionChecker, nil)
wh.tokenSerializer = s.mockTokenSerializer

validRequest := &types.TerminateWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowExecution: &types.WorkflowExecution{
WorkflowID: testWorkflowID,
RunID: testRunID,
},
Reason: "reason",
Details: nil,
Identity: "identity",
}

testInput := map[string]struct {
request *types.TerminateWorkflowExecutionRequest
expectError bool
mockFn func()
expectErrorType error
}{
"shutting down": {
request: validRequest,
mockFn: func() {
wh.shuttingDown = int32(1)
},
expectError: true,
expectErrorType: validate.ErrShuttingDown,
},
"nil request": {
request: nil,
mockFn: func() {},
expectError: true,
expectErrorType: validate.ErrRequestNotSet,
},
"empty domain": {
request: &types.TerminateWorkflowExecutionRequest{
Domain: "",
},
mockFn: func() {},
expectError: true,
expectErrorType: validate.ErrDomainNotSet,
},
"empty workflow ID": {
request: &types.TerminateWorkflowExecutionRequest{
Domain: s.testDomain,
},
mockFn: func() {},
expectError: true,
},
"empty workflow execution": {
request: &types.TerminateWorkflowExecutionRequest{
Domain: s.testDomain,
WorkflowExecution: nil,
},
mockFn: func() {},
expectError: true,
},
"cannot get domain ID": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return("", errors.New("error getting domain ID"))
},
expectError: true,
},
"history client error": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil)
s.mockHistoryClient.EXPECT().TerminateWorkflowExecution(gomock.Any(), gomock.Any()).Return(errors.New("error"))
},
expectError: true,
},
"success": {
request: validRequest,
mockFn: func() {
s.mockDomainCache.EXPECT().GetDomainID(s.testDomain).Return(s.testDomainID, nil)
s.mockHistoryClient.EXPECT().TerminateWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil)
},
expectError: false,
},
}

for name, input := range testInput {
s.Run(name, func() {
input.mockFn()
err := wh.TerminateWorkflowExecution(context.Background(), input.request)
if input.expectError {
s.Error(err)
if input.expectErrorType != nil {
s.ErrorIs(err, input.expectErrorType)
}
} else {
s.NoError(err)
}
wh.shuttingDown = int32(0)
})
}

// test version checker
s.Run("version checker", func() {
mockCtrl := gomock.NewController(s.T())
mockResource := resource.NewTest(s.T(), mockCtrl, metrics.Frontend)
mockVersionChecker := client.NewMockVersionChecker(mockCtrl)

cfg := frontendcfg.NewConfig(
dc.NewCollection(
dc.NewInMemoryClient(),
mockResource.GetLogger(),
),
numHistoryShards,
false,
"hostname",
)
cfg.EnableClientVersionCheck = dc.GetBoolPropertyFn(true)
wh := NewWorkflowHandler(mockResource, cfg, mockVersionChecker, nil)
mockVersionChecker.EXPECT().ClientSupported(gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
err := wh.TerminateWorkflowExecution(context.Background(), validRequest)
s.Error(err)
})

}

0 comments on commit 0ca3c74

Please sign in to comment.