Files
tasks/service/history/ndc/resetter_test.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

261 lines
7.6 KiB
Go

package ndc
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
historyspb "github.com/hanzoai/tasks/api/history/v1"
persistencespb "github.com/hanzoai/tasks/api/persistence/v1"
"github.com/hanzoai/tasks/chasm"
"github.com/hanzoai/tasks/common"
"github.com/hanzoai/tasks/common/definition"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/namespace"
"github.com/hanzoai/tasks/common/persistence"
"github.com/hanzoai/tasks/common/persistence/versionhistory"
serviceerrors "github.com/hanzoai/tasks/common/serviceerror"
"github.com/hanzoai/tasks/common/util"
historyi "github.com/hanzoai/tasks/service/history/interfaces"
"github.com/hanzoai/tasks/service/history/shard"
"github.com/hanzoai/tasks/service/history/tests"
"github.com/hanzoai/tasks/service/history/workflow"
"go.uber.org/mock/gomock"
)
type (
resetterSuite struct {
suite.Suite
*require.Assertions
controller *gomock.Controller
mockShard *shard.ContextTest
mockBaseMutableState *historyi.MockMutableState
mockRebuiltMutableState *historyi.MockMutableState
mockTransactionMgr *MockTransactionManager
mockStateBuilder *MockStateRebuilder
logger log.Logger
mockExecManager *persistence.MockExecutionManager
namespaceID namespace.ID
namespace namespace.Name
workflowID string
baseRunID string
newContext historyi.WorkflowContext
newRunID string
workflowResetter *resetterImpl
}
)
func TestResetterSuite(t *testing.T) {
s := new(resetterSuite)
suite.Run(t, s)
}
func (s *resetterSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
s.mockBaseMutableState = historyi.NewMockMutableState(s.controller)
s.mockRebuiltMutableState = historyi.NewMockMutableState(s.controller)
s.mockTransactionMgr = NewMockTransactionManager(s.controller)
s.mockStateBuilder = NewMockStateRebuilder(s.controller)
s.mockShard = shard.NewTestContext(
s.controller,
&persistencespb.ShardInfo{
ShardId: 10,
RangeId: 1,
},
tests.NewDynamicConfig(),
)
s.mockExecManager = s.mockShard.Resource.ExecutionMgr
s.logger = s.mockShard.GetLogger()
s.namespaceID = namespace.ID(uuid.NewString())
s.namespace = "some random namespace name"
s.workflowID = "some random workflow ID"
s.baseRunID = uuid.NewString()
s.newContext = workflow.NewContext(
s.mockShard.GetConfig(),
definition.NewWorkflowKey(
s.namespaceID.String(),
s.workflowID,
s.newRunID,
),
chasm.WorkflowArchetypeID,
s.logger,
s.mockShard.GetThrottledLogger(),
s.mockShard.GetMetricsHandler(),
)
s.newRunID = uuid.NewString()
s.workflowResetter = NewResetter(
s.mockShard, s.mockTransactionMgr, s.namespaceID, s.workflowID, s.baseRunID, s.newContext, s.newRunID, s.logger,
)
s.workflowResetter.stateRebuilder = s.mockStateBuilder
}
func (s *resetterSuite) TearDownTest() {
s.controller.Finish()
s.mockShard.StopForTest()
}
func (s *resetterSuite) TestResetWorkflow_NoError() {
ctx := context.Background()
now := time.Now().UTC()
branchToken := []byte("some random branch token")
lastEventID := int64(500)
version := int64(123)
versionHistory := versionhistory.NewVersionHistory(
branchToken,
[]*historyspb.VersionHistoryItem{versionhistory.NewVersionHistoryItem(lastEventID, version)},
)
versionHistories := versionhistory.NewVersionHistories(versionHistory)
baseEventID := lastEventID - 100
baseVersion := version
incomingFirstEventID := baseEventID + 12
incomingVersion := baseVersion + 3
rebuildStats := RebuildStats{
HistorySize: 9999,
ExternalPayloadSize: 1234,
ExternalPayloadCount: 56,
}
newBranchToken := []byte("other random branch token")
s.mockBaseMutableState.EXPECT().GetExecutionInfo().Return(&persistencespb.WorkflowExecutionInfo{VersionHistories: versionHistories}).AnyTimes()
mockBaseWorkflowReleaseFnCalled := false
mockBaseWorkflowReleaseFn := func(err error) {
mockBaseWorkflowReleaseFnCalled = true
}
mockBaseWorkflow := NewMockWorkflow(s.controller)
mockBaseWorkflow.EXPECT().GetMutableState().Return(s.mockBaseMutableState).AnyTimes()
mockBaseWorkflow.EXPECT().GetReleaseFn().Return(mockBaseWorkflowReleaseFn)
s.mockTransactionMgr.EXPECT().LoadWorkflow(
ctx,
s.namespaceID,
s.workflowID,
s.baseRunID,
chasm.WorkflowArchetypeID,
).Return(mockBaseWorkflow, nil)
s.mockStateBuilder.EXPECT().Rebuild(
ctx,
now,
definition.NewWorkflowKey(
s.namespaceID.String(),
s.workflowID,
s.baseRunID,
),
branchToken,
baseEventID,
util.Ptr(baseVersion),
definition.NewWorkflowKey(
s.namespaceID.String(),
s.workflowID,
s.newRunID,
),
newBranchToken,
gomock.Any(),
).Return(s.mockRebuiltMutableState, rebuildStats, nil)
s.mockRebuiltMutableState.EXPECT().AddHistorySize(rebuildStats.HistorySize)
s.mockRebuiltMutableState.EXPECT().AddExternalPayloadSize(rebuildStats.ExternalPayloadSize)
s.mockRebuiltMutableState.EXPECT().AddExternalPayloadCount(rebuildStats.ExternalPayloadCount)
shardID := s.mockShard.GetShardID()
s.mockExecManager.EXPECT().ForkHistoryBranch(gomock.Any(), &persistence.ForkHistoryBranchRequest{
ForkBranchToken: branchToken,
ForkNodeID: baseEventID + 1,
Info: persistence.BuildHistoryGarbageCleanupInfo(s.namespaceID.String(), s.workflowID, s.newRunID),
ShardID: shardID,
NamespaceID: s.namespaceID.String(),
NewRunID: s.newRunID,
}).Return(&persistence.ForkHistoryBranchResponse{NewBranchToken: newBranchToken}, nil)
s.mockRebuiltMutableState.EXPECT().RefreshExpirationTimeoutTask(gomock.Any()).Return(nil)
rebuiltMutableState, err := s.workflowResetter.resetWorkflow(
ctx,
now,
baseEventID,
baseVersion,
incomingFirstEventID,
incomingVersion,
)
s.NoError(err)
s.Equal(s.mockRebuiltMutableState, rebuiltMutableState)
s.True(mockBaseWorkflowReleaseFnCalled)
}
func (s *resetterSuite) TestResetWorkflow_Error() {
ctx := context.Background()
now := time.Now().UTC()
branchToken := []byte("some random branch token")
lastEventID := int64(500)
version := int64(123)
versionHistory := versionhistory.NewVersionHistory(
branchToken,
[]*historyspb.VersionHistoryItem{versionhistory.NewVersionHistoryItem(lastEventID, version)},
)
versionHistories := versionhistory.NewVersionHistories(versionHistory)
baseEventID := lastEventID + 100
baseVersion := version
incomingFirstEventID := baseEventID + 12
incomingFirstEventVersion := baseVersion + 3
s.mockBaseMutableState.EXPECT().GetExecutionInfo().Return(&persistencespb.WorkflowExecutionInfo{VersionHistories: versionHistories}).AnyTimes()
mockBaseWorkflowReleaseFn := func(err error) {
}
mockBaseWorkflow := NewMockWorkflow(s.controller)
mockBaseWorkflow.EXPECT().GetMutableState().Return(s.mockBaseMutableState).AnyTimes()
mockBaseWorkflow.EXPECT().GetReleaseFn().Return(mockBaseWorkflowReleaseFn)
s.mockTransactionMgr.EXPECT().LoadWorkflow(
ctx,
s.namespaceID,
s.workflowID,
s.baseRunID,
chasm.WorkflowArchetypeID,
).Return(mockBaseWorkflow, nil)
rebuiltMutableState, err := s.workflowResetter.resetWorkflow(
ctx,
now,
baseEventID,
baseVersion,
incomingFirstEventID,
incomingFirstEventVersion,
)
s.Error(err)
s.IsType(&serviceerrors.RetryReplication{}, err)
s.Nil(rebuiltMutableState)
retryErr, isRetryError := err.(*serviceerrors.RetryReplication)
s.True(isRetryError)
expectedErr := serviceerrors.NewRetryReplication(
resendOnResetWorkflowMessage,
s.namespaceID.String(),
s.workflowID,
s.newRunID,
common.EmptyEventID,
common.EmptyVersion,
incomingFirstEventID,
incomingFirstEventVersion,
)
s.Equal(retryErr, expectedErr)
}