Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for NewHistoryReplicator in history_replicator.go #5994

Merged
merged 5 commits into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
359 changes: 359 additions & 0 deletions service/history/ndc/history_replicator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
// 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 ndc

import (
bowenxia marked this conversation as resolved.
Show resolved Hide resolved
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/dynamicconfig"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/service/history/config"
"github.com/uber/cadence/service/history/events"
"github.com/uber/cadence/service/history/execution"
"github.com/uber/cadence/service/history/resource"
"github.com/uber/cadence/service/history/shard"
)

func createTestHistoryReplicator(t *testing.T) historyReplicatorImpl {
ctrl := gomock.NewController(t)

mockShard := shard.NewMockContext(ctrl)
mockShard.EXPECT().GetConfig().Return(&config.Config{
NumberOfShards: 0,
IsAdvancedVisConfigExist: false,
MaxResponseSize: 0,
HistoryCacheInitialSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheMaxSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheTTL: dynamicconfig.GetDurationPropertyFn(10),
HostName: "test-host",
}).Times(1)

// before going into NewHistoryReplicator
mockExecutionManager := persistence.NewMockExecutionManager(ctrl)
mockShard.EXPECT().GetExecutionManager().Return(mockExecutionManager).Times(1)
mockShard.EXPECT().GetLogger().Return(log.NewNoop()).Times(1)
mockShard.EXPECT().GetMetricsClient().Return(nil).Times(3)

testExecutionCache := execution.NewCache(mockShard)
mockEventsReapplier := NewMockEventsReapplier(ctrl)

// going into NewHistoryReplicator -> newTransactionManager()
mockShard.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)
mockHistoryManager := persistence.NewMockHistoryManager(ctrl)
mockShard.EXPECT().GetHistoryManager().Return(mockHistoryManager).Times(3)

mockHistoryResource := resource.NewMockResource(ctrl)
mockShard.EXPECT().GetService().Return(mockHistoryResource).Times(2)
mockPayloadSerializer := persistence.NewMockPayloadSerializer(ctrl)
mockHistoryResource.EXPECT().GetPayloadSerializer().Return(mockPayloadSerializer).Times(1)

// going into NewHistoryReplicator -> newTransactionManager -> reset.NewWorkflowResetter
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShard.EXPECT().GetDomainCache().Return(mockDomainCache).Times(2)

// going back to NewHistoryReplicator
mockHistoryResource.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(1)

replicator := NewHistoryReplicator(mockShard, testExecutionCache, mockEventsReapplier, log.NewNoop())
replicatorImpl := replicator.(*historyReplicatorImpl)
return *replicatorImpl
}

func TestNewHistoryReplicator(t *testing.T) {
assert.NotNil(t, createTestHistoryReplicator(t))
}

func TestNewHistoryReplicator_newBranchManager(t *testing.T) {
ctrl := gomock.NewController(t)

mockShard := shard.NewMockContext(ctrl)
mockShard.EXPECT().GetConfig().Return(&config.Config{
NumberOfShards: 0,
IsAdvancedVisConfigExist: false,
MaxResponseSize: 0,
HistoryCacheInitialSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheMaxSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheTTL: dynamicconfig.GetDurationPropertyFn(10),
HostName: "test-host",
}).Times(1)

// before going into NewHistoryReplicator
mockExecutionManager := persistence.NewMockExecutionManager(ctrl)
mockShard.EXPECT().GetExecutionManager().Return(mockExecutionManager).Times(1)
mockShard.EXPECT().GetLogger().Return(log.NewNoop()).Times(1)
mockShard.EXPECT().GetMetricsClient().Return(nil).Times(3)

testExecutionCache := execution.NewCache(mockShard)
mockEventsReapplier := NewMockEventsReapplier(ctrl)

// going into NewHistoryReplicator -> newTransactionManager()
mockShard.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)
mockHistoryManager := persistence.NewMockHistoryManager(ctrl)
mockShard.EXPECT().GetHistoryManager().Return(mockHistoryManager).Times(4)

mockHistoryResource := resource.NewMockResource(ctrl)
mockShard.EXPECT().GetService().Return(mockHistoryResource).Times(3)
mockPayloadSerializer := persistence.NewMockPayloadSerializer(ctrl)
mockHistoryResource.EXPECT().GetPayloadSerializer().Return(mockPayloadSerializer).Times(1)

// going into NewHistoryReplicator -> newTransactionManager -> reset.NewWorkflowResetter
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShard.EXPECT().GetDomainCache().Return(mockDomainCache).Times(2)

// going back to NewHistoryReplicator
mockHistoryResource.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)

testReplicator := NewHistoryReplicator(mockShard, testExecutionCache, mockEventsReapplier, log.NewNoop())
testReplicatorImpl := testReplicator.(*historyReplicatorImpl)

// test newBranchManager function in history replicator
mockExecutionContext := execution.NewMockContext(ctrl)
mockExecutionMutableState := execution.NewMockMutableState(ctrl)
assert.NotNil(t, testReplicatorImpl.newBranchManager(mockExecutionContext, mockExecutionMutableState, log.NewNoop()))
}

func TestNewHistoryReplicator_newConflictResolver(t *testing.T) {
ctrl := gomock.NewController(t)

mockShard := shard.NewMockContext(ctrl)
mockShard.EXPECT().GetConfig().Return(&config.Config{
NumberOfShards: 0,
IsAdvancedVisConfigExist: false,
MaxResponseSize: 0,
HistoryCacheInitialSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheMaxSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheTTL: dynamicconfig.GetDurationPropertyFn(10),
HostName: "test-host",
}).Times(2)

// before going into NewHistoryReplicator
mockExecutionManager := persistence.NewMockExecutionManager(ctrl)
mockShard.EXPECT().GetExecutionManager().Return(mockExecutionManager).Times(1)
mockShard.EXPECT().GetLogger().Return(log.NewNoop()).Times(1)
mockShard.EXPECT().GetMetricsClient().Return(nil).Times(3)

testExecutionCache := execution.NewCache(mockShard)
mockEventsReapplier := NewMockEventsReapplier(ctrl)

// going into NewHistoryReplicator -> newTransactionManager()
mockShard.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(3)
mockHistoryManager := persistence.NewMockHistoryManager(ctrl)
mockShard.EXPECT().GetHistoryManager().Return(mockHistoryManager).Times(4)

mockHistoryResource := resource.NewMockResource(ctrl)
mockShard.EXPECT().GetService().Return(mockHistoryResource).Times(3)
mockPayloadSerializer := persistence.NewMockPayloadSerializer(ctrl)
mockHistoryResource.EXPECT().GetPayloadSerializer().Return(mockPayloadSerializer).Times(1)

// going into NewHistoryReplicator -> newTransactionManager -> reset.NewWorkflowResetter
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShard.EXPECT().GetDomainCache().Return(mockDomainCache).Times(4)

// going back to NewHistoryReplicator
mockHistoryResource.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)

testReplicator := NewHistoryReplicator(mockShard, testExecutionCache, mockEventsReapplier, log.NewNoop())
testReplicatorImpl := testReplicator.(*historyReplicatorImpl)

// test newConflictResolver function in history replicator
mockEventsCache := events.NewMockCache(ctrl)
mockShard.EXPECT().GetEventsCache().Return(mockEventsCache).Times(1)
mockShard.EXPECT().GetShardID().Return(0).Times(1)

mockExecutionContext := execution.NewMockContext(ctrl)
mockExecutionMutableState := execution.NewMockMutableState(ctrl)
assert.NotNil(t, testReplicatorImpl.newConflictResolver(mockExecutionContext, mockExecutionMutableState, log.NewNoop()))
}

func TestNewHistoryReplicator_newWorkflowResetter(t *testing.T) {
ctrl := gomock.NewController(t)

mockShard := shard.NewMockContext(ctrl)
mockShard.EXPECT().GetConfig().Return(&config.Config{
NumberOfShards: 0,
IsAdvancedVisConfigExist: false,
MaxResponseSize: 0,
HistoryCacheInitialSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheMaxSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheTTL: dynamicconfig.GetDurationPropertyFn(10),
HostName: "test-host",
}).Times(2)

// before going into NewHistoryReplicator
mockExecutionManager := persistence.NewMockExecutionManager(ctrl)
mockShard.EXPECT().GetExecutionManager().Return(mockExecutionManager).Times(1)
mockShard.EXPECT().GetLogger().Return(log.NewNoop()).Times(1)
mockShard.EXPECT().GetMetricsClient().Return(nil).Times(3)

testExecutionCache := execution.NewCache(mockShard)
mockEventsReapplier := NewMockEventsReapplier(ctrl)

// going into NewHistoryReplicator -> newTransactionManager()
mockShard.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(3)
mockHistoryManager := persistence.NewMockHistoryManager(ctrl)
mockShard.EXPECT().GetHistoryManager().Return(mockHistoryManager).Times(5)

mockHistoryResource := resource.NewMockResource(ctrl)
mockShard.EXPECT().GetService().Return(mockHistoryResource).Times(3)
mockPayloadSerializer := persistence.NewMockPayloadSerializer(ctrl)
mockHistoryResource.EXPECT().GetPayloadSerializer().Return(mockPayloadSerializer).Times(1)

// going into NewHistoryReplicator -> newTransactionManager -> reset.NewWorkflowResetter
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShard.EXPECT().GetDomainCache().Return(mockDomainCache).Times(4)

// going back to NewHistoryReplicator
mockHistoryResource.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)

testReplicator := NewHistoryReplicator(mockShard, testExecutionCache, mockEventsReapplier, log.NewNoop())
testReplicatorImpl := testReplicator.(*historyReplicatorImpl)

// test newWorkflowResetter function in history replicator
mockEventsCache := events.NewMockCache(ctrl)
mockShard.EXPECT().GetEventsCache().Return(mockEventsCache).Times(1)
mockShard.EXPECT().GetShardID().Return(0).Times(1)

mockExecutionContext := execution.NewMockContext(ctrl)
assert.NotNil(t, testReplicatorImpl.newWorkflowResetter(
"test-domain-id",
"test-workflow-id",
"test-base-run-id",
mockExecutionContext,
"test-run-id",
log.NewNoop(),
))
}

func TestNewHistoryReplicator_newStateBuilder(t *testing.T) {
ctrl := gomock.NewController(t)

mockShard := shard.NewMockContext(ctrl)
mockShard.EXPECT().GetConfig().Return(&config.Config{
NumberOfShards: 0,
IsAdvancedVisConfigExist: false,
MaxResponseSize: 0,
HistoryCacheInitialSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheMaxSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheTTL: dynamicconfig.GetDurationPropertyFn(10),
HostName: "test-host",
}).Times(1)

// before going into NewHistoryReplicator
mockExecutionManager := persistence.NewMockExecutionManager(ctrl)
mockShard.EXPECT().GetExecutionManager().Return(mockExecutionManager).Times(1)
mockShard.EXPECT().GetLogger().Return(log.NewNoop()).Times(1)
mockShard.EXPECT().GetMetricsClient().Return(nil).Times(3)

testExecutionCache := execution.NewCache(mockShard)
mockEventsReapplier := NewMockEventsReapplier(ctrl)

// going into NewHistoryReplicator -> newTransactionManager()
mockShard.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)
mockHistoryManager := persistence.NewMockHistoryManager(ctrl)
mockShard.EXPECT().GetHistoryManager().Return(mockHistoryManager).Times(3)

mockHistoryResource := resource.NewMockResource(ctrl)
mockShard.EXPECT().GetService().Return(mockHistoryResource).Times(3)
mockPayloadSerializer := persistence.NewMockPayloadSerializer(ctrl)
mockHistoryResource.EXPECT().GetPayloadSerializer().Return(mockPayloadSerializer).Times(1)

// going into NewHistoryReplicator -> newTransactionManager -> reset.NewWorkflowResetter
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShard.EXPECT().GetDomainCache().Return(mockDomainCache).Times(3)

// going back to NewHistoryReplicator
mockHistoryResource.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(2)

testReplicator := NewHistoryReplicator(mockShard, testExecutionCache, mockEventsReapplier, log.NewNoop())
testReplicatorImpl := testReplicator.(*historyReplicatorImpl)

// test newStateBuilder function in history replicator
mockExecutionMutableState := execution.NewMockMutableState(ctrl)
assert.NotNil(t, testReplicatorImpl.newStateBuilder(mockExecutionMutableState, log.NewNoop()))
}

func TestNewHistoryReplicator_newMutableState(t *testing.T) {
ctrl := gomock.NewController(t)

mockShard := shard.NewMockContext(ctrl)
mockShard.EXPECT().GetConfig().Return(&config.Config{
NumberOfShards: 0,
IsAdvancedVisConfigExist: false,
MaxResponseSize: 0,
HistoryCacheInitialSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheMaxSize: dynamicconfig.GetIntPropertyFn(10),
HistoryCacheTTL: dynamicconfig.GetDurationPropertyFn(10),
HostName: "test-host",
}).Times(2)

// before going into NewHistoryReplicator
mockExecutionManager := persistence.NewMockExecutionManager(ctrl)
mockShard.EXPECT().GetExecutionManager().Return(mockExecutionManager).Times(1)
mockShard.EXPECT().GetLogger().Return(log.NewNoop()).Times(1)
mockShard.EXPECT().GetMetricsClient().Return(nil).Times(4)

testExecutionCache := execution.NewCache(mockShard)
mockEventsReapplier := NewMockEventsReapplier(ctrl)

// going into NewHistoryReplicator -> newTransactionManager()
mockShard.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(4)
mockHistoryManager := persistence.NewMockHistoryManager(ctrl)
mockShard.EXPECT().GetHistoryManager().Return(mockHistoryManager).Times(3)

mockHistoryResource := resource.NewMockResource(ctrl)
mockShard.EXPECT().GetService().Return(mockHistoryResource).Times(2)
mockPayloadSerializer := persistence.NewMockPayloadSerializer(ctrl)
mockHistoryResource.EXPECT().GetPayloadSerializer().Return(mockPayloadSerializer).Times(1)

// going into NewHistoryReplicator -> newTransactionManager -> reset.NewWorkflowResetter
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShard.EXPECT().GetDomainCache().Return(mockDomainCache).Times(3)

// going back to NewHistoryReplicator
mockHistoryResource.EXPECT().GetClusterMetadata().Return(cluster.Metadata{}).Times(1)

testReplicator := NewHistoryReplicator(mockShard, testExecutionCache, mockEventsReapplier, log.NewNoop())
testReplicatorImpl := testReplicator.(*historyReplicatorImpl)

// test newMutableState function in history replicator
deadline := int64(0)
mockShard.EXPECT().GetTimeSource().Return(clock.NewMockedTimeSource()).Times(1)
mockEventsCache := events.NewMockCache(ctrl)
mockShard.EXPECT().GetEventsCache().Return(mockEventsCache).Times(1)
mockDomainCacheEntry := cache.NewDomainCacheEntryForTest(
&persistence.DomainInfo{Name: "test-domain"},
nil,
true,
&persistence.DomainReplicationConfig{ActiveClusterName: "test-active-cluster"},
0,
&deadline,
)
assert.NotNil(t, testReplicatorImpl.newMutableState(mockDomainCacheEntry, log.NewNoop()))
}
Loading