Files
tasks/service/history/deletemanager/delete_manager_test.go

225 lines
6.9 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package deletemanager
import (
"context"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence/visibility/manager"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/history/tests"
"go.temporal.io/server/service/history/workflow"
wcache "go.temporal.io/server/service/history/workflow/cache"
)
type (
deleteManagerWorkflowSuite struct {
suite.Suite
*require.Assertions
controller *gomock.Controller
mockCache *wcache.MockCache
mockShardContext *shard.MockContext
mockClock *clock.EventTimeSource
mockNamespaceRegistry *namespace.MockRegistry
mockMetadata *cluster.MockMetadata
mockVisibilityManager *manager.MockVisibilityManager
deleteManager DeleteManager
}
)
func TestDeleteManagerSuite(t *testing.T) {
s := &deleteManagerWorkflowSuite{}
suite.Run(t, s)
}
func (s *deleteManagerWorkflowSuite) SetupSuite() {
}
func (s *deleteManagerWorkflowSuite) TearDownSuite() {
}
func (s *deleteManagerWorkflowSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
s.mockCache = wcache.NewMockCache(s.controller)
s.mockClock = clock.NewEventTimeSource()
s.mockNamespaceRegistry = namespace.NewMockRegistry(s.controller)
s.mockMetadata = cluster.NewMockMetadata(s.controller)
s.mockVisibilityManager = manager.NewMockVisibilityManager(s.controller)
s.mockVisibilityManager.EXPECT().GetIndexName().Return("").AnyTimes()
config := tests.NewDynamicConfig()
s.mockShardContext = shard.NewMockContext(s.controller)
s.mockShardContext.EXPECT().GetMetricsHandler().Return(metrics.NoopMetricsHandler).AnyTimes()
s.mockShardContext.EXPECT().GetNamespaceRegistry().Return(s.mockNamespaceRegistry).AnyTimes()
s.mockShardContext.EXPECT().GetClusterMetadata().Return(s.mockMetadata).AnyTimes()
s.deleteManager = NewDeleteManager(
s.mockShardContext,
s.mockCache,
config,
s.mockClock,
s.mockVisibilityManager,
)
}
func (s *deleteManagerWorkflowSuite) TestDeleteDeletedWorkflowExecution() {
we := commonpb.WorkflowExecution{
WorkflowId: tests.WorkflowID,
RunId: tests.RunID,
}
mockWeCtx := workflow.NewMockContext(s.controller)
mockMutableState := workflow.NewMockMutableState(s.controller)
mockMutableState.EXPECT().GetCurrentBranchToken().Return([]byte{22, 8, 78}, nil)
closeExecutionVisibilityTaskID := int64(39)
mockMutableState.EXPECT().GetExecutionInfo().Return(&persistencespb.WorkflowExecutionInfo{
CloseVisibilityTaskId: closeExecutionVisibilityTaskID,
})
stage := tasks.DeleteWorkflowExecutionStageNone
s.mockShardContext.EXPECT().DeleteWorkflowExecution(
gomock.Any(),
definition.WorkflowKey{
NamespaceID: tests.NamespaceID.String(),
WorkflowID: tests.WorkflowID,
RunID: tests.RunID,
},
[]byte{22, 8, 78},
closeExecutionVisibilityTaskID,
&stage,
).Return(nil)
mockWeCtx.EXPECT().Clear()
err := s.deleteManager.DeleteWorkflowExecution(
context.Background(),
tests.NamespaceID,
&we,
mockWeCtx,
mockMutableState,
false,
&stage,
)
s.NoError(err)
}
func (s *deleteManagerWorkflowSuite) TestDeleteDeletedWorkflowExecution_Error() {
we := commonpb.WorkflowExecution{
WorkflowId: tests.WorkflowID,
RunId: tests.RunID,
}
mockWeCtx := workflow.NewMockContext(s.controller)
mockMutableState := workflow.NewMockMutableState(s.controller)
mockMutableState.EXPECT().GetCurrentBranchToken().Return([]byte{22, 8, 78}, nil)
closeExecutionVisibilityTaskID := int64(39)
mockMutableState.EXPECT().GetExecutionInfo().MinTimes(1).Return(&persistencespb.WorkflowExecutionInfo{
CloseVisibilityTaskId: closeExecutionVisibilityTaskID,
})
stage := tasks.DeleteWorkflowExecutionStageNone
s.mockShardContext.EXPECT().DeleteWorkflowExecution(
gomock.Any(),
definition.WorkflowKey{
NamespaceID: tests.NamespaceID.String(),
WorkflowID: tests.WorkflowID,
RunID: tests.RunID,
},
[]byte{22, 8, 78},
closeExecutionVisibilityTaskID,
&stage,
).Return(serviceerror.NewInternal("test error"))
err := s.deleteManager.DeleteWorkflowExecution(
context.Background(),
tests.NamespaceID,
&we,
mockWeCtx,
mockMutableState,
false,
&stage,
)
s.Error(err)
}
func (s *deleteManagerWorkflowSuite) TestDeleteWorkflowExecution_OpenWorkflow() {
we := commonpb.WorkflowExecution{
WorkflowId: tests.WorkflowID,
RunId: tests.RunID,
}
mockWeCtx := workflow.NewMockContext(s.controller)
mockMutableState := workflow.NewMockMutableState(s.controller)
closeExecutionVisibilityTaskID := int64(39)
mockMutableState.EXPECT().GetCurrentBranchToken().Return([]byte{22, 8, 78}, nil)
mockMutableState.EXPECT().GetExecutionInfo().MinTimes(1).Return(&persistencespb.WorkflowExecutionInfo{
CloseVisibilityTaskId: closeExecutionVisibilityTaskID,
})
stage := tasks.DeleteWorkflowExecutionStageNone
s.mockShardContext.EXPECT().DeleteWorkflowExecution(
gomock.Any(),
definition.WorkflowKey{
NamespaceID: tests.NamespaceID.String(),
WorkflowID: tests.WorkflowID,
RunID: tests.RunID,
},
[]byte{22, 8, 78},
closeExecutionVisibilityTaskID,
&stage,
).Return(nil)
mockWeCtx.EXPECT().Clear()
err := s.deleteManager.DeleteWorkflowExecution(
context.Background(),
tests.NamespaceID,
&we,
mockWeCtx,
mockMutableState,
true,
&stage,
)
s.NoError(err)
}