mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
* Use low priority lock for VerifyFirstWorkflowTaskScheduled API * Use low priority lock for VerifyChildExecutionCompletionRecorded API * Rename caller type to lock priority since some internal API use "low priority lock"
214 lines
7.3 KiB
Go
214 lines
7.3 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 history
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"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"
|
|
|
|
enumsspb "go.temporal.io/server/api/enums/v1"
|
|
persistencespb "go.temporal.io/server/api/persistence/v1"
|
|
"go.temporal.io/server/common/cluster"
|
|
"go.temporal.io/server/common/definition"
|
|
"go.temporal.io/server/common/metrics"
|
|
"go.temporal.io/server/service/history/deletemanager"
|
|
"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 (
|
|
timerQueueTaskExecutorBaseSuite struct {
|
|
suite.Suite
|
|
*require.Assertions
|
|
|
|
controller *gomock.Controller
|
|
mockDeleteManager *deletemanager.MockDeleteManager
|
|
mockCache *wcache.MockCache
|
|
|
|
testShardContext *shard.ContextTest
|
|
timerQueueTaskExecutorBase *timerQueueTaskExecutorBase
|
|
}
|
|
)
|
|
|
|
func TestTimerQueueTaskExecutorBaseSuite(t *testing.T) {
|
|
s := new(timerQueueTaskExecutorBaseSuite)
|
|
suite.Run(t, s)
|
|
}
|
|
|
|
func (s *timerQueueTaskExecutorBaseSuite) SetupSuite() {
|
|
}
|
|
|
|
func (s *timerQueueTaskExecutorBaseSuite) TearDownSuite() {
|
|
}
|
|
|
|
func (s *timerQueueTaskExecutorBaseSuite) SetupTest() {
|
|
s.Assertions = require.New(s.T())
|
|
|
|
s.controller = gomock.NewController(s.T())
|
|
s.mockDeleteManager = deletemanager.NewMockDeleteManager(s.controller)
|
|
s.mockCache = wcache.NewMockCache(s.controller)
|
|
|
|
config := tests.NewDynamicConfig()
|
|
s.testShardContext = shard.NewTestContext(
|
|
s.controller,
|
|
&persistencespb.ShardInfo{
|
|
ShardId: 0,
|
|
RangeId: 1,
|
|
},
|
|
config,
|
|
)
|
|
s.testShardContext.Resource.ClusterMetadata.EXPECT().GetCurrentClusterName().Return(cluster.TestCurrentClusterName).AnyTimes()
|
|
|
|
s.timerQueueTaskExecutorBase = newTimerQueueTaskExecutorBase(
|
|
s.testShardContext,
|
|
s.mockCache,
|
|
s.mockDeleteManager,
|
|
nil,
|
|
s.testShardContext.GetLogger(),
|
|
metrics.NoopMetricsHandler,
|
|
config,
|
|
)
|
|
}
|
|
|
|
func (s *timerQueueTaskExecutorBaseSuite) TearDownTest() {
|
|
s.controller.Finish()
|
|
}
|
|
|
|
func (s *timerQueueTaskExecutorBaseSuite) Test_executeDeleteHistoryEventTask_NoErr() {
|
|
for _, alreadyArchived := range []bool{false, true} {
|
|
s.Run(fmt.Sprintf("AlreadyArchived=%v", alreadyArchived), func() {
|
|
task := &tasks.DeleteHistoryEventTask{
|
|
WorkflowKey: definition.NewWorkflowKey(
|
|
tests.NamespaceID.String(),
|
|
tests.WorkflowID,
|
|
tests.RunID,
|
|
),
|
|
Version: 123,
|
|
TaskID: 12345,
|
|
VisibilityTimestamp: time.Now().UTC(),
|
|
WorkflowDataAlreadyArchived: alreadyArchived,
|
|
}
|
|
we := commonpb.WorkflowExecution{
|
|
WorkflowId: tests.WorkflowID,
|
|
RunId: tests.RunID,
|
|
}
|
|
|
|
mockWeCtx := workflow.NewMockContext(s.controller)
|
|
mockMutableState := workflow.NewMockMutableState(s.controller)
|
|
|
|
s.mockCache.EXPECT().GetOrCreateWorkflowExecution(gomock.Any(), tests.NamespaceID, we, workflow.LockPriorityLow).Return(mockWeCtx, wcache.NoopReleaseFn, nil)
|
|
|
|
mockWeCtx.EXPECT().LoadMutableState(gomock.Any()).Return(mockMutableState, nil)
|
|
mockMutableState.EXPECT().GetLastWriteVersion().Return(int64(1), nil)
|
|
mockMutableState.EXPECT().GetExecutionInfo().Return(&persistencespb.WorkflowExecutionInfo{})
|
|
mockMutableState.EXPECT().GetNextEventID().Return(int64(2))
|
|
mockMutableState.EXPECT().GetNamespaceEntry().Return(tests.LocalNamespaceEntry)
|
|
s.testShardContext.Resource.ClusterMetadata.EXPECT().IsGlobalNamespaceEnabled().Return(false)
|
|
mockMutableState.EXPECT().GetExecutionState().Return(&persistencespb.WorkflowExecutionState{State: enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED})
|
|
|
|
archiveIfEnabled := !alreadyArchived
|
|
stage := tasks.DeleteWorkflowExecutionStageNone
|
|
s.mockDeleteManager.EXPECT().DeleteWorkflowExecutionByRetention(
|
|
gomock.Any(),
|
|
tests.NamespaceID,
|
|
we,
|
|
mockWeCtx,
|
|
mockMutableState,
|
|
archiveIfEnabled,
|
|
&stage,
|
|
).Return(nil)
|
|
|
|
err := s.timerQueueTaskExecutorBase.executeDeleteHistoryEventTask(
|
|
context.Background(),
|
|
task)
|
|
s.NoError(err)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *timerQueueTaskExecutorBaseSuite) TestArchiveHistory_DeleteFailed() {
|
|
for _, alreadyArchived := range []bool{false, true} {
|
|
s.Run(fmt.Sprintf("AlreadyArchived=%v", alreadyArchived), func() {
|
|
task := &tasks.DeleteHistoryEventTask{
|
|
WorkflowKey: definition.NewWorkflowKey(
|
|
tests.NamespaceID.String(),
|
|
tests.WorkflowID,
|
|
tests.RunID,
|
|
),
|
|
Version: 123,
|
|
TaskID: 12345,
|
|
VisibilityTimestamp: time.Now().UTC(),
|
|
WorkflowDataAlreadyArchived: alreadyArchived,
|
|
}
|
|
we := commonpb.WorkflowExecution{
|
|
WorkflowId: tests.WorkflowID,
|
|
RunId: tests.RunID,
|
|
}
|
|
|
|
mockWeCtx := workflow.NewMockContext(s.controller)
|
|
mockMutableState := workflow.NewMockMutableState(s.controller)
|
|
|
|
s.mockCache.EXPECT().GetOrCreateWorkflowExecution(gomock.Any(), tests.NamespaceID, we, workflow.LockPriorityLow).Return(mockWeCtx, wcache.NoopReleaseFn, nil)
|
|
|
|
mockWeCtx.EXPECT().LoadMutableState(gomock.Any()).Return(mockMutableState, nil)
|
|
mockMutableState.EXPECT().GetLastWriteVersion().Return(int64(1), nil)
|
|
mockMutableState.EXPECT().GetExecutionInfo().Return(&persistencespb.WorkflowExecutionInfo{})
|
|
mockMutableState.EXPECT().GetNextEventID().Return(int64(2))
|
|
mockMutableState.EXPECT().GetNamespaceEntry().Return(tests.LocalNamespaceEntry)
|
|
s.testShardContext.Resource.ClusterMetadata.EXPECT().IsGlobalNamespaceEnabled().Return(false)
|
|
mockMutableState.EXPECT().GetExecutionState().Return(&persistencespb.WorkflowExecutionState{State: enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED})
|
|
|
|
archiveIfEnabled := !alreadyArchived
|
|
stage := tasks.DeleteWorkflowExecutionStageNone
|
|
s.mockDeleteManager.EXPECT().DeleteWorkflowExecutionByRetention(
|
|
gomock.Any(),
|
|
tests.NamespaceID,
|
|
we,
|
|
mockWeCtx,
|
|
mockMutableState,
|
|
archiveIfEnabled,
|
|
&stage,
|
|
).Return(serviceerror.NewInternal("test error"))
|
|
|
|
err := s.timerQueueTaskExecutorBase.executeDeleteHistoryEventTask(
|
|
context.Background(),
|
|
task)
|
|
s.Error(err)
|
|
})
|
|
}
|
|
}
|