From 6903e610733e2786bdb1e967a728c99c9f5c110d Mon Sep 17 00:00:00 2001 From: taylan isikdemir Date: Mon, 3 Jun 2024 14:35:48 -0700 Subject: [PATCH 1/3] Write tests for shard context --- service/history/shard/contextTest.go | 121 ------------- service/history/shard/context_test.go | 233 ++++++++++++++++++++++++++ 2 files changed, 233 insertions(+), 121 deletions(-) delete mode 100644 service/history/shard/contextTest.go diff --git a/service/history/shard/contextTest.go b/service/history/shard/contextTest.go deleted file mode 100644 index b9d84dc746b..00000000000 --- a/service/history/shard/contextTest.go +++ /dev/null @@ -1,121 +0,0 @@ -// 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 shard - -import ( - "testing" - "time" - - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/mock" - - "github.com/uber/cadence/common/metrics" - "github.com/uber/cadence/common/persistence" - "github.com/uber/cadence/common/types" - "github.com/uber/cadence/service/history/config" - "github.com/uber/cadence/service/history/events" - "github.com/uber/cadence/service/history/resource" -) - -// TestContext is a test implementation for shard Context interface -type TestContext struct { - *contextImpl - - Resource *resource.Test - MockEventsCache *events.MockCache - MockAddingPendingFailoverMarker func(*types.FailoverMarkerAttributes) error -} - -var _ Context = (*TestContext)(nil) - -// NewTestContext create a new shardContext for test -func NewTestContext( - t *testing.T, - ctrl *gomock.Controller, - shardInfo *persistence.ShardInfo, - config *config.Config, -) *TestContext { - resource := resource.NewTest(t, ctrl, metrics.History) - eventsCache := events.NewMockCache(ctrl) - if shardInfo.TransferProcessingQueueStates == nil { - shardInfo.TransferProcessingQueueStates = &types.ProcessingQueueStates{ - StatesByCluster: make(map[string][]*types.ProcessingQueueState), - } - } - if shardInfo.TimerProcessingQueueStates == nil { - shardInfo.TimerProcessingQueueStates = &types.ProcessingQueueStates{ - StatesByCluster: make(map[string][]*types.ProcessingQueueState), - } - } - if shardInfo.CrossClusterProcessingQueueStates == nil { - shardInfo.CrossClusterProcessingQueueStates = &types.ProcessingQueueStates{ - StatesByCluster: make(map[string][]*types.ProcessingQueueState), - } - } - shard := &contextImpl{ - Resource: resource, - shardID: shardInfo.ShardID, - rangeID: shardInfo.RangeID, - shardInfo: shardInfo, - executionManager: resource.ExecutionMgr, - config: config, - logger: resource.GetLogger(), - throttledLogger: resource.GetThrottledLogger(), - transferSequenceNumber: 1, - transferMaxReadLevel: 0, - maxTransferSequenceNumber: 100000, - timerMaxReadLevelMap: make(map[string]time.Time), - remoteClusterCurrentTime: make(map[string]time.Time), - eventsCache: eventsCache, - } - return &TestContext{ - contextImpl: shard, - Resource: resource, - MockEventsCache: eventsCache, - } -} - -// ShardInfo is a test hook for getting shard info -func (s *TestContext) ShardInfo() *persistence.ShardInfo { - return s.shardInfo -} - -// SetEventsCache is a test hook for setting events cache -func (s *TestContext) SetEventsCache( - eventsCache events.Cache, -) { - s.eventsCache = eventsCache - s.MockEventsCache = nil -} - -// Finish checks whether expectations are met -func (s *TestContext) Finish( - t mock.TestingT, -) { - s.Resource.Finish(t) -} - -func (s *TestContext) AddingPendingFailoverMarker(marker *types.FailoverMarkerAttributes) error { - if s.MockAddingPendingFailoverMarker != nil { - return s.MockAddingPendingFailoverMarker(marker) - } - return s.contextImpl.AddingPendingFailoverMarker(marker) -} diff --git a/service/history/shard/context_test.go b/service/history/shard/context_test.go index e84f5e00fa7..c14dc95de6b 100644 --- a/service/history/shard/context_test.go +++ b/service/history/shard/context_test.go @@ -50,6 +50,91 @@ import ( "github.com/uber/cadence/service/history/resource" ) +// TestContext is a test implementation for shard Context interface +type TestContext struct { + *contextImpl + + Resource *resource.Test + MockEventsCache *events.MockCache + MockAddingPendingFailoverMarker func(*types.FailoverMarkerAttributes) error +} + +var _ Context = (*TestContext)(nil) + +// NewTestContext create a new shardContext for test +func NewTestContext( + t *testing.T, + ctrl *gomock.Controller, + shardInfo *persistence.ShardInfo, + config *config.Config, +) *TestContext { + resource := resource.NewTest(t, ctrl, metrics.History) + eventsCache := events.NewMockCache(ctrl) + if shardInfo.TransferProcessingQueueStates == nil { + shardInfo.TransferProcessingQueueStates = &types.ProcessingQueueStates{ + StatesByCluster: make(map[string][]*types.ProcessingQueueState), + } + } + if shardInfo.TimerProcessingQueueStates == nil { + shardInfo.TimerProcessingQueueStates = &types.ProcessingQueueStates{ + StatesByCluster: make(map[string][]*types.ProcessingQueueState), + } + } + if shardInfo.CrossClusterProcessingQueueStates == nil { + shardInfo.CrossClusterProcessingQueueStates = &types.ProcessingQueueStates{ + StatesByCluster: make(map[string][]*types.ProcessingQueueState), + } + } + shard := &contextImpl{ + Resource: resource, + shardID: shardInfo.ShardID, + rangeID: shardInfo.RangeID, + shardInfo: shardInfo, + executionManager: resource.ExecutionMgr, + config: config, + logger: resource.GetLogger(), + throttledLogger: resource.GetThrottledLogger(), + transferSequenceNumber: 1, + transferMaxReadLevel: 0, + maxTransferSequenceNumber: 100000, + timerMaxReadLevelMap: make(map[string]time.Time), + remoteClusterCurrentTime: make(map[string]time.Time), + eventsCache: eventsCache, + } + return &TestContext{ + contextImpl: shard, + Resource: resource, + MockEventsCache: eventsCache, + } +} + +// ShardInfo is a test hook for getting shard info +func (s *TestContext) ShardInfo() *persistence.ShardInfo { + return s.shardInfo +} + +// SetEventsCache is a test hook for setting events cache +func (s *TestContext) SetEventsCache( + eventsCache events.Cache, +) { + s.eventsCache = eventsCache + s.MockEventsCache = nil +} + +// Finish checks whether expectations are met +func (s *TestContext) Finish( + t mock.TestingT, +) { + s.Resource.Finish(t) +} + +func (s *TestContext) AddingPendingFailoverMarker(marker *types.FailoverMarkerAttributes) error { + if s.MockAddingPendingFailoverMarker != nil { + return s.MockAddingPendingFailoverMarker(marker) + } + return s.contextImpl.AddingPendingFailoverMarker(marker) +} + const ( testShardID = 123 testRangeID = 1 @@ -125,6 +210,8 @@ func (s *contextTestSuite) newContext() *contextImpl { maxTransferSequenceNumber: testMaxTransferSequenceNumber, timerMaxReadLevelMap: make(map[string]time.Time), remoteClusterCurrentTime: make(map[string]time.Time), + transferFailoverLevels: make(map[string]TransferFailoverLevel), + timerFailoverLevels: make(map[string]TimerFailoverLevel), eventsCache: eventsCache, } @@ -151,6 +238,152 @@ func (s *contextTestSuite) TestAccessorMethods() { s.Assert().Equal(mockEngine, s.context.GetEngine()) } +func (s *contextTestSuite) TestTransferAckLevel() { + // validate default value returned + s.context.shardInfo.TransferAckLevel = 5 + s.Assert().EqualValues(5, s.context.GetTransferAckLevel()) + + // update and validate it's returned + s.mockShardManager.On("UpdateShard", mock.Anything, mock.Anything).Once().Return(nil) + s.context.UpdateTransferAckLevel(20) + s.Assert().EqualValues(20, s.context.GetTransferAckLevel()) + s.Assert().Equal(0, s.context.shardInfo.StolenSinceRenew) +} + +func (s *contextTestSuite) TestClusterTransferAckLevel() { + // update and validate cluster transfer ack level + s.mockShardManager.On("UpdateShard", mock.Anything, mock.Anything).Once().Return(nil) + s.context.UpdateTransferClusterAckLevel(cluster.TestCurrentClusterName, 5) + s.Assert().EqualValues(5, s.context.GetTransferClusterAckLevel(cluster.TestCurrentClusterName)) + s.Assert().Equal(0, s.context.shardInfo.StolenSinceRenew) + + // get cluster transfer ack level for non existing cluster + s.context.shardInfo.TransferAckLevel = 10 + s.Assert().EqualValues(10, s.context.GetTransferClusterAckLevel("non-existing-cluster")) +} + +func (s *contextTestSuite) TestTimerAckLevel() { + // validate default value returned + now := time.Now() + s.context.shardInfo.TimerAckLevel = now + s.Assert().Equal(now.UnixNano(), s.context.GetTimerAckLevel().UnixNano()) + + // update and validate it's returned + newTime := time.Now() + s.mockShardManager.On("UpdateShard", mock.Anything, mock.Anything).Once().Return(nil) + s.context.UpdateTimerAckLevel(newTime) + s.Assert().EqualValues(newTime.UnixNano(), s.context.GetTimerAckLevel().UnixNano()) + s.Assert().Equal(0, s.context.shardInfo.StolenSinceRenew) +} + +func (s *contextTestSuite) TestClusterTimerAckLevel() { + // update and validate cluster timer ack level + now := time.Now() + s.mockShardManager.On("UpdateShard", mock.Anything, mock.Anything).Once().Return(nil) + s.context.UpdateTimerClusterAckLevel(cluster.TestCurrentClusterName, now) + s.Assert().EqualValues(now.UnixNano(), s.context.GetTimerClusterAckLevel(cluster.TestCurrentClusterName).UnixNano()) + s.Assert().Equal(0, s.context.shardInfo.StolenSinceRenew) + + // get cluster timer ack level for non existing cluster + s.context.shardInfo.TimerAckLevel = now + s.Assert().EqualValues(now.UnixNano(), s.context.GetTimerClusterAckLevel("non-existing-cluster").UnixNano()) +} + +func (s *contextTestSuite) TestUpdateTransferFailoverLevel() { + failoverLevel1 := TransferFailoverLevel{ + StartTime: time.Now(), + MinLevel: 1, + CurrentLevel: 10, + MaxLevel: 100, + DomainIDs: map[string]struct{}{"testDomainID": {}}, + } + failoverLevel2 := TransferFailoverLevel{ + StartTime: time.Now(), + MinLevel: 2, + CurrentLevel: 20, + MaxLevel: 200, + DomainIDs: map[string]struct{}{"testDomainID2": {}}, + } + + err := s.context.UpdateTransferFailoverLevel("id1", failoverLevel1) + s.NoError(err) + err = s.context.UpdateTransferFailoverLevel("id2", failoverLevel2) + s.NoError(err) + + gotLevels := s.context.GetAllTransferFailoverLevels() + s.Len(gotLevels, 2) + assert.Equal(s.T(), failoverLevel1, gotLevels["id1"]) + assert.Equal(s.T(), failoverLevel2, gotLevels["id2"]) + + err = s.context.DeleteTransferFailoverLevel("id1") + s.NoError(err) + gotLevels = s.context.GetAllTransferFailoverLevels() + s.Len(gotLevels, 1) + assert.Equal(s.T(), failoverLevel2, gotLevels["id2"]) +} + +func (s *contextTestSuite) TestUpdateTimerFailoverLevel() { + t := time.Now() + failoverLevel1 := TimerFailoverLevel{ + StartTime: t, + MinLevel: t.Add(time.Minute), + CurrentLevel: t.Add(time.Minute * 2), + MaxLevel: t.Add(time.Minute * 3), + DomainIDs: map[string]struct{}{"testDomainID": {}}, + } + failoverLevel2 := TimerFailoverLevel{ + StartTime: t, + MinLevel: t.Add(time.Minute * 2), + CurrentLevel: t.Add(time.Minute * 4), + MaxLevel: t.Add(time.Minute * 6), + DomainIDs: map[string]struct{}{"testDomainID2": {}}, + } + + err := s.context.UpdateTimerFailoverLevel("id1", failoverLevel1) + s.NoError(err) + err = s.context.UpdateTimerFailoverLevel("id2", failoverLevel2) + s.NoError(err) + + gotLevels := s.context.GetAllTimerFailoverLevels() + s.Len(gotLevels, 2) + assert.Equal(s.T(), failoverLevel1, gotLevels["id1"]) + assert.Equal(s.T(), failoverLevel2, gotLevels["id2"]) + + err = s.context.DeleteTimerFailoverLevel("id1") + s.NoError(err) + gotLevels = s.context.GetAllTimerFailoverLevels() + s.Len(gotLevels, 1) + assert.Equal(s.T(), failoverLevel2, gotLevels["id2"]) +} + +func (s *contextTestSuite) TestDomainNotificationVersion() { + // test initial value + s.EqualValues(0, s.context.GetDomainNotificationVersion()) + + // test updated value + s.mockShardManager.On("UpdateShard", mock.Anything, mock.Anything).Once().Return(nil) + err := s.context.UpdateDomainNotificationVersion(10) + s.NoError(err) + s.EqualValues(10, s.context.GetDomainNotificationVersion()) +} + +func (s *contextTestSuite) TestTimerMaxReadLevel() { + // get current cluster's level + gotLevel := s.context.UpdateTimerMaxReadLevel(cluster.TestCurrentClusterName) + wantLevel := s.mockResource.TimeSource.Now().Add(s.context.config.TimerProcessorMaxTimeShift()).Truncate(time.Millisecond) + s.Equal(wantLevel, gotLevel) + s.Equal(wantLevel, s.context.GetTimerMaxReadLevel(cluster.TestCurrentClusterName)) + + // get remote cluster's level + remoteCluster := "remote-cluster" + now := time.Now() + s.context.SetCurrentTime(remoteCluster, now) + gotLevel = s.context.UpdateTimerMaxReadLevel(remoteCluster) + wantLevel = now.Add(s.context.config.TimerProcessorMaxTimeShift()).Truncate(time.Millisecond) + s.Equal(wantLevel, gotLevel) + s.Equal(wantLevel, s.context.GetTimerMaxReadLevel(remoteCluster)) +} + func (s *contextTestSuite) TestGenerateTransferTaskID() { taskID, err := s.context.GenerateTransferTaskID() s.Require().NoError(err) From 01d0b6c6e2e7956ec7496701aa5b3e88538b70cb Mon Sep 17 00:00:00 2001 From: taylan isikdemir Date: Tue, 4 Jun 2024 09:45:59 -0700 Subject: [PATCH 2/3] revert context test change because it breaks build --- codecov.yml | 3 +- .../shard/{context_test.go => contextTest.go} | 85 ------------ service/history/shard/test_context.go | 123 ++++++++++++++++++ 3 files changed, 124 insertions(+), 87 deletions(-) rename service/history/shard/{context_test.go => contextTest.go} (88%) create mode 100644 service/history/shard/test_context.go diff --git a/codecov.yml b/codecov.yml index cd2e2ee39c9..0b48fd16b21 100644 --- a/codecov.yml +++ b/codecov.yml @@ -19,8 +19,7 @@ coverage: if_ci_failed: ignore # require the CI to pass before setting the status patch: default: - target: 0% # Todo (David.porter) to immediately revert this after landing a refactor - # specify the target coverage for each commit status + target: 80% # specify the target coverage for each commit status # option: "auto" (compare against parent commit or pull request base) # option: "X%" a static target percentage to hit threshold: 0% # allow the coverage drop by x% before marking as failure diff --git a/service/history/shard/context_test.go b/service/history/shard/contextTest.go similarity index 88% rename from service/history/shard/context_test.go rename to service/history/shard/contextTest.go index c14dc95de6b..f75d262b8b5 100644 --- a/service/history/shard/context_test.go +++ b/service/history/shard/contextTest.go @@ -50,91 +50,6 @@ import ( "github.com/uber/cadence/service/history/resource" ) -// TestContext is a test implementation for shard Context interface -type TestContext struct { - *contextImpl - - Resource *resource.Test - MockEventsCache *events.MockCache - MockAddingPendingFailoverMarker func(*types.FailoverMarkerAttributes) error -} - -var _ Context = (*TestContext)(nil) - -// NewTestContext create a new shardContext for test -func NewTestContext( - t *testing.T, - ctrl *gomock.Controller, - shardInfo *persistence.ShardInfo, - config *config.Config, -) *TestContext { - resource := resource.NewTest(t, ctrl, metrics.History) - eventsCache := events.NewMockCache(ctrl) - if shardInfo.TransferProcessingQueueStates == nil { - shardInfo.TransferProcessingQueueStates = &types.ProcessingQueueStates{ - StatesByCluster: make(map[string][]*types.ProcessingQueueState), - } - } - if shardInfo.TimerProcessingQueueStates == nil { - shardInfo.TimerProcessingQueueStates = &types.ProcessingQueueStates{ - StatesByCluster: make(map[string][]*types.ProcessingQueueState), - } - } - if shardInfo.CrossClusterProcessingQueueStates == nil { - shardInfo.CrossClusterProcessingQueueStates = &types.ProcessingQueueStates{ - StatesByCluster: make(map[string][]*types.ProcessingQueueState), - } - } - shard := &contextImpl{ - Resource: resource, - shardID: shardInfo.ShardID, - rangeID: shardInfo.RangeID, - shardInfo: shardInfo, - executionManager: resource.ExecutionMgr, - config: config, - logger: resource.GetLogger(), - throttledLogger: resource.GetThrottledLogger(), - transferSequenceNumber: 1, - transferMaxReadLevel: 0, - maxTransferSequenceNumber: 100000, - timerMaxReadLevelMap: make(map[string]time.Time), - remoteClusterCurrentTime: make(map[string]time.Time), - eventsCache: eventsCache, - } - return &TestContext{ - contextImpl: shard, - Resource: resource, - MockEventsCache: eventsCache, - } -} - -// ShardInfo is a test hook for getting shard info -func (s *TestContext) ShardInfo() *persistence.ShardInfo { - return s.shardInfo -} - -// SetEventsCache is a test hook for setting events cache -func (s *TestContext) SetEventsCache( - eventsCache events.Cache, -) { - s.eventsCache = eventsCache - s.MockEventsCache = nil -} - -// Finish checks whether expectations are met -func (s *TestContext) Finish( - t mock.TestingT, -) { - s.Resource.Finish(t) -} - -func (s *TestContext) AddingPendingFailoverMarker(marker *types.FailoverMarkerAttributes) error { - if s.MockAddingPendingFailoverMarker != nil { - return s.MockAddingPendingFailoverMarker(marker) - } - return s.contextImpl.AddingPendingFailoverMarker(marker) -} - const ( testShardID = 123 testRangeID = 1 diff --git a/service/history/shard/test_context.go b/service/history/shard/test_context.go new file mode 100644 index 00000000000..ae77f436fed --- /dev/null +++ b/service/history/shard/test_context.go @@ -0,0 +1,123 @@ +// The MIT License (MIT) +// +// Copyright (c) 2017-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 shard + +import ( + "testing" + "time" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/mock" + + "github.com/uber/cadence/common/metrics" + "github.com/uber/cadence/common/persistence" + "github.com/uber/cadence/common/types" + "github.com/uber/cadence/service/history/config" + "github.com/uber/cadence/service/history/events" + "github.com/uber/cadence/service/history/resource" +) + +// TestContext is a test implementation for shard Context interface +type TestContext struct { + *contextImpl + + Resource *resource.Test + MockEventsCache *events.MockCache + MockAddingPendingFailoverMarker func(*types.FailoverMarkerAttributes) error +} + +var _ Context = (*TestContext)(nil) + +// NewTestContext create a new shardContext for test +func NewTestContext( + t *testing.T, + ctrl *gomock.Controller, + shardInfo *persistence.ShardInfo, + config *config.Config, +) *TestContext { + resource := resource.NewTest(t, ctrl, metrics.History) + eventsCache := events.NewMockCache(ctrl) + if shardInfo.TransferProcessingQueueStates == nil { + shardInfo.TransferProcessingQueueStates = &types.ProcessingQueueStates{ + StatesByCluster: make(map[string][]*types.ProcessingQueueState), + } + } + if shardInfo.TimerProcessingQueueStates == nil { + shardInfo.TimerProcessingQueueStates = &types.ProcessingQueueStates{ + StatesByCluster: make(map[string][]*types.ProcessingQueueState), + } + } + if shardInfo.CrossClusterProcessingQueueStates == nil { + shardInfo.CrossClusterProcessingQueueStates = &types.ProcessingQueueStates{ + StatesByCluster: make(map[string][]*types.ProcessingQueueState), + } + } + shard := &contextImpl{ + Resource: resource, + shardID: shardInfo.ShardID, + rangeID: shardInfo.RangeID, + shardInfo: shardInfo, + executionManager: resource.ExecutionMgr, + config: config, + logger: resource.GetLogger(), + throttledLogger: resource.GetThrottledLogger(), + transferSequenceNumber: 1, + transferMaxReadLevel: 0, + maxTransferSequenceNumber: 100000, + timerMaxReadLevelMap: make(map[string]time.Time), + remoteClusterCurrentTime: make(map[string]time.Time), + eventsCache: eventsCache, + } + return &TestContext{ + contextImpl: shard, + Resource: resource, + MockEventsCache: eventsCache, + } +} + +// ShardInfo is a test hook for getting shard info +func (s *TestContext) ShardInfo() *persistence.ShardInfo { + return s.shardInfo +} + +// SetEventsCache is a test hook for setting events cache +func (s *TestContext) SetEventsCache( + eventsCache events.Cache, +) { + s.eventsCache = eventsCache + s.MockEventsCache = nil +} + +// Finish checks whether expectations are met +func (s *TestContext) Finish( + t mock.TestingT, +) { + s.Resource.Finish(t) +} + +func (s *TestContext) AddingPendingFailoverMarker(marker *types.FailoverMarkerAttributes) error { + if s.MockAddingPendingFailoverMarker != nil { + return s.MockAddingPendingFailoverMarker(marker) + } + return s.contextImpl.AddingPendingFailoverMarker(marker) +} From ad3e37aea45dd425dcb72fdb8724b9208f8ef897 Mon Sep 17 00:00:00 2001 From: taylan isikdemir Date: Tue, 4 Jun 2024 12:31:03 -0700 Subject: [PATCH 3/3] fix --- service/history/shard/contextTest.go | 609 +++----------------------- service/history/shard/context_test.go | 608 +++++++++++++++++++++++++ service/history/shard/test_context.go | 123 ------ 3 files changed, 669 insertions(+), 671 deletions(-) create mode 100644 service/history/shard/context_test.go delete mode 100644 service/history/shard/test_context.go diff --git a/service/history/shard/contextTest.go b/service/history/shard/contextTest.go index f75d262b8b5..b9d84dc746b 100644 --- a/service/history/shard/contextTest.go +++ b/service/history/shard/contextTest.go @@ -1,6 +1,4 @@ -// The MIT License (MIT) -// -// Copyright (c) 2017-2020 Uber Technologies Inc. +// 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 @@ -9,600 +7,115 @@ // 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 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. +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. package shard import ( - "context" - "errors" - "math" "testing" "time" "github.com/golang/mock/gomock" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - "github.com/uber-go/tally" - "github.com/uber/cadence/common" - "github.com/uber/cadence/common/cluster" - "github.com/uber/cadence/common/log" - "github.com/uber/cadence/common/log/testlogger" "github.com/uber/cadence/common/metrics" - "github.com/uber/cadence/common/mocks" "github.com/uber/cadence/common/persistence" "github.com/uber/cadence/common/types" "github.com/uber/cadence/service/history/config" - "github.com/uber/cadence/service/history/engine" "github.com/uber/cadence/service/history/events" "github.com/uber/cadence/service/history/resource" ) -const ( - testShardID = 123 - testRangeID = 1 - testTransferMaxReadLevel = 10 - testMaxTransferSequenceNumber = 100 -) - -type ( - contextTestSuite struct { - suite.Suite - *require.Assertions - - controller *gomock.Controller - mockResource *resource.Test - mockShardManager *mocks.ShardManager +// TestContext is a test implementation for shard Context interface +type TestContext struct { + *contextImpl - metricsClient metrics.Client - logger log.Logger - - context *contextImpl - } -) - -func TestContextSuite(t *testing.T) { - s := new(contextTestSuite) - suite.Run(t, s) + Resource *resource.Test + MockEventsCache *events.MockCache + MockAddingPendingFailoverMarker func(*types.FailoverMarkerAttributes) error } -func (s *contextTestSuite) SetupTest() { - s.Assertions = require.New(s.T()) - - s.controller = gomock.NewController(s.T()) - s.mockResource = resource.NewTest(s.T(), s.controller, metrics.History) - s.mockShardManager = s.mockResource.ShardMgr - - s.metricsClient = metrics.NewClient(tally.NoopScope, metrics.History) - s.logger = testlogger.New(s.T()) +var _ Context = (*TestContext)(nil) - s.context = s.newContext() -} - -func (s *contextTestSuite) newContext() *contextImpl { - eventsCache := events.NewMockCache(s.controller) - config := config.NewForTest() - shardInfo := &persistence.ShardInfo{ - ShardID: testShardID, - RangeID: testRangeID, - // the following fields will be initialized - // when acquiring the shard if they are nil - ClusterTransferAckLevel: make(map[string]int64), - ClusterTimerAckLevel: make(map[string]time.Time), - TransferProcessingQueueStates: &types.ProcessingQueueStates{ +// NewTestContext create a new shardContext for test +func NewTestContext( + t *testing.T, + ctrl *gomock.Controller, + shardInfo *persistence.ShardInfo, + config *config.Config, +) *TestContext { + resource := resource.NewTest(t, ctrl, metrics.History) + eventsCache := events.NewMockCache(ctrl) + if shardInfo.TransferProcessingQueueStates == nil { + shardInfo.TransferProcessingQueueStates = &types.ProcessingQueueStates{ StatesByCluster: make(map[string][]*types.ProcessingQueueState), - }, - CrossClusterProcessingQueueStates: &types.ProcessingQueueStates{ + } + } + if shardInfo.TimerProcessingQueueStates == nil { + shardInfo.TimerProcessingQueueStates = &types.ProcessingQueueStates{ StatesByCluster: make(map[string][]*types.ProcessingQueueState), - }, - TimerProcessingQueueStates: &types.ProcessingQueueStates{ + } + } + if shardInfo.CrossClusterProcessingQueueStates == nil { + shardInfo.CrossClusterProcessingQueueStates = &types.ProcessingQueueStates{ StatesByCluster: make(map[string][]*types.ProcessingQueueState), - }, + } } - context := &contextImpl{ - Resource: s.mockResource, + shard := &contextImpl{ + Resource: resource, shardID: shardInfo.ShardID, rangeID: shardInfo.RangeID, shardInfo: shardInfo, - executionManager: s.mockResource.ExecutionMgr, + executionManager: resource.ExecutionMgr, config: config, - logger: s.logger, - throttledLogger: s.logger, + logger: resource.GetLogger(), + throttledLogger: resource.GetThrottledLogger(), transferSequenceNumber: 1, - transferMaxReadLevel: testTransferMaxReadLevel, - maxTransferSequenceNumber: testMaxTransferSequenceNumber, + transferMaxReadLevel: 0, + maxTransferSequenceNumber: 100000, timerMaxReadLevelMap: make(map[string]time.Time), remoteClusterCurrentTime: make(map[string]time.Time), - transferFailoverLevels: make(map[string]TransferFailoverLevel), - timerFailoverLevels: make(map[string]TimerFailoverLevel), eventsCache: eventsCache, } - - s.Require().True(testMaxTransferSequenceNumber < (1<