Files
tasks/service/history/ndc/mutable_state_mapper_test.go
Roey BermanandGitHub 243effc6e1 Forbid the use of pborman/uuid and remove uses (#8541)
## What

Used the `depguard` linter to prevent importing the package and modified
all uses of it.

Also fixed a bug in the membership code where we were not updating the
next page token:

https://github.com/temporalio/temporal/pull/8541/files#diff-985dd64ffa4c6bfe0248b982dc47439b725862baa3e1f2c559b7ff522bf21969R350


## Why?

From the package README in GitHub:

> This package now leverages the github.com/google/uuid package (which
is based off an earlier version of this package).

Also, there's no point in having two uuid implementations in the
codebase.

## Test

Added the invalid import and confirmed it works:

```
../service/frontend/admin_handler_test.go:13:2: import 'github.com/pborman/uuid' is not allowed from list 'main': Importing github.com/pborman/uuid is disallowed; use github.com/google/uuid instead (depguard)
        _ "github.com/pborman/uuid"

```
2025-11-26 01:33:02 +00:00

199 lines
4.3 KiB
Go

package ndc
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/log"
historyi "go.temporal.io/server/service/history/interfaces"
"go.uber.org/mock/gomock"
)
type (
mutableStateMapperSuite struct {
suite.Suite
*require.Assertions
controller *gomock.Controller
logger log.Logger
shardContext historyi.MockShardContext
branchMgrProvider branchMgrProvider
mockBranchMgr *MockBranchMgr
mutableStateMapper *MutableStateMapperImpl
clusterMetadata *cluster.MockMetadata
}
)
func TestMutableStateMapperSuite(t *testing.T) {
s := new(mutableStateMapperSuite)
suite.Run(t, s)
}
func (s *mutableStateMapperSuite) SetupSuite() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
s.mockBranchMgr = NewMockBranchMgr(s.controller)
s.branchMgrProvider = func(
wfContext historyi.WorkflowContext,
mutableState historyi.MutableState,
logger log.Logger) BranchMgr {
return s.mockBranchMgr
}
s.clusterMetadata = cluster.NewMockMetadata(s.controller)
s.clusterMetadata.EXPECT().ClusterNameForFailoverVersion(gomock.Any(), gomock.Any()).Return("some random cluster name").AnyTimes()
s.mutableStateMapper = NewMutableStateMapping(
nil,
nil,
s.branchMgrProvider,
nil,
nil)
}
func (s *mutableStateMapperSuite) TearDownSuite() {
}
func (s *mutableStateMapperSuite) TestGetOrCreateHistoryBranch_ValidEventBatch_NoDedupe() {
workflowKey := definition.WorkflowKey{
WorkflowID: uuid.NewString(),
RunID: uuid.NewString(),
}
eventSlices := [][]*historypb.HistoryEvent{
{
{
EventId: 11,
},
{
EventId: 12,
},
},
{
{
EventId: 13,
},
{
EventId: 14,
},
},
}
task, _ := newReplicationTask(
s.clusterMetadata,
nil,
workflowKey,
nil,
nil,
eventSlices,
nil,
"",
nil,
false,
)
s.mockBranchMgr.EXPECT().
GetOrCreate(gomock.Any(), gomock.Any(), int64(11), gomock.Any()).Return(true, int32(0), nil).Times(1)
_, out, err := s.mutableStateMapper.GetOrCreateHistoryBranch(context.Background(), nil, nil, task)
s.NoError(err)
s.Equal(int32(0), out.BranchIndex)
s.Equal(0, out.EventsApplyIndex)
s.True(out.DoContinue)
}
func (s *mutableStateMapperSuite) TestGetOrCreateHistoryBranch_ValidEventBatch_FirstBatchDedupe() {
workflowKey := definition.WorkflowKey{
WorkflowID: uuid.NewString(),
RunID: uuid.NewString(),
}
eventSlices := [][]*historypb.HistoryEvent{
{
{
EventId: 11,
},
{
EventId: 12,
},
},
{
{
EventId: 13,
},
{
EventId: 14,
},
},
}
task, _ := newReplicationTask(
s.clusterMetadata,
nil,
workflowKey,
nil,
nil,
eventSlices,
nil,
"",
nil,
false,
)
s.mockBranchMgr.EXPECT().
GetOrCreate(gomock.Any(), gomock.Any(), int64(11), gomock.Any()).Return(false, int32(0), nil).Times(1)
s.mockBranchMgr.EXPECT().
GetOrCreate(gomock.Any(), gomock.Any(), int64(13), gomock.Any()).Return(true, int32(0), nil).Times(1)
_, out, err := s.mutableStateMapper.GetOrCreateHistoryBranch(context.Background(), nil, nil, task)
s.NoError(err)
s.Equal(int32(0), out.BranchIndex)
s.Equal(1, out.EventsApplyIndex)
s.True(out.DoContinue)
}
func (s *mutableStateMapperSuite) TestGetOrCreateHistoryBranch_ValidEventBatch_AllBatchDedupe() {
workflowKey := definition.WorkflowKey{
WorkflowID: uuid.NewString(),
RunID: uuid.NewString(),
}
eventSlices := [][]*historypb.HistoryEvent{
{
{
EventId: 11,
},
{
EventId: 12,
},
},
{
{
EventId: 13,
},
{
EventId: 14,
},
},
}
task, _ := newReplicationTask(
s.clusterMetadata,
nil,
workflowKey,
nil,
nil,
eventSlices,
nil,
"",
nil,
false,
)
s.mockBranchMgr.EXPECT().
GetOrCreate(gomock.Any(), gomock.Any(), int64(11), gomock.Any()).Return(false, int32(0), nil).Times(1)
s.mockBranchMgr.EXPECT().
GetOrCreate(gomock.Any(), gomock.Any(), int64(13), gomock.Any()).Return(false, int32(0), nil).Times(1)
_, out, err := s.mutableStateMapper.GetOrCreateHistoryBranch(context.Background(), nil, nil, task)
s.NoError(err)
s.Equal(int32(0), out.BranchIndex)
s.Equal(0, out.EventsApplyIndex)
s.False(out.DoContinue)
}