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

[code-coverage] Add tests for mutable_state_util.go #6062

Merged
merged 2 commits into from
Jun 6, 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
267 changes: 267 additions & 0 deletions service/history/execution/mutable_state_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@
package execution

import (
"errors"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber-go/tally"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/clock"
"github.com/uber/cadence/common/log/testlogger"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/testing/testdatagen/idlfuzzedtestdata"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/common/types/testdata"
"github.com/uber/cadence/service/history/config"
"github.com/uber/cadence/service/history/constants"
"github.com/uber/cadence/service/history/events"
"github.com/uber/cadence/service/history/shard"
)

func TestCopyActivityInfo(t *testing.T) {
Expand Down Expand Up @@ -316,3 +328,258 @@ func TestConvertWorkflowRequests(t *testing.T) {

assert.ElementsMatch(t, expectedOutputs, convertWorkflowRequests(inputs))
}

func TestCreatePersistenceMutableState(t *testing.T) {
ctrl := gomock.NewController(t)
mockShardContext := shard.NewMockContext(ctrl)
logger := testlogger.New(t)
mockEventsCache := events.NewMockCache(ctrl)
mockDomainCache := cache.NewMockDomainCache(ctrl)
mockShardContext.EXPECT().GetClusterMetadata().Return(constants.TestClusterMetadata).Times(2)
mockShardContext.EXPECT().GetEventsCache().Return(mockEventsCache)
mockShardContext.EXPECT().GetConfig().Return(config.NewForTest())
mockShardContext.EXPECT().GetTimeSource().Return(clock.NewMockedTimeSource())
mockShardContext.EXPECT().GetMetricsClient().Return(metrics.NewClient(tally.NoopScope, metrics.History))
mockShardContext.EXPECT().GetDomainCache().Return(mockDomainCache)

builder := newMutableStateBuilder(mockShardContext, logger, constants.TestLocalDomainEntry)
builder.pendingActivityInfoIDs[0] = &persistence.ActivityInfo{}
builder.pendingTimerInfoIDs["some-key"] = &persistence.TimerInfo{}
builder.pendingSignalInfoIDs[0] = &persistence.SignalInfo{}
builder.pendingChildExecutionInfoIDs[0] = &persistence.ChildExecutionInfo{}
builder.bufferedEvents = []*types.HistoryEvent{{}}
builder.updateBufferedEvents = []*types.HistoryEvent{{}}
builder.versionHistories = &persistence.VersionHistories{CurrentVersionHistoryIndex: 0, Histories: []*persistence.VersionHistory{}}
builder.pendingRequestCancelInfoIDs[0] = &persistence.RequestCancelInfo{}
builder.decisionTaskManager.(*mutableStateDecisionTaskManagerImpl).HasInFlightDecision()
builder.executionInfo.DecisionStartedID = 1

mutableState := CreatePersistenceMutableState(builder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mapper, taking a type (mutableStateBuilder) and creating a persistence Mutable State. Is it possible to see if there's the reverse somewhere?

This kind of code is quite prone to not getting updated, I feel fairly strongly that we should round-trip test these mappings rather than just unit-testing them. Happy to sync to discuss what I mean.

See TestMutableStateBuilder_CopyToPersistence_roundtrip for an example of a roundtrip test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the msb.CopyToPersistance() method I was testing looks weirdly similar, but I think they're different. Not super clear, but I think this one is a deep copy, so modifications to the copied persistence struct shouldn't affect the original maybe? not super sure how rigorous that is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can add the roundtrip test on a follow up PR. I'm looking into the example.

assert.NotNil(t, mutableState)
ketsiambaku marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, builder.executionInfo, mutableState.ExecutionInfo)
assert.Equal(t, builder.pendingActivityInfoIDs, mutableState.ActivityInfos)
assert.Equal(t, builder.pendingSignalInfoIDs, mutableState.SignalInfos)
assert.Equal(t, builder.pendingTimerInfoIDs, mutableState.TimerInfos)
assert.Equal(t, builder.pendingRequestCancelInfoIDs, mutableState.RequestCancelInfos)
assert.Equal(t, len(builder.bufferedEvents)+len(builder.updateBufferedEvents), len(mutableState.BufferedEvents))
}

func TestGetChildExecutionDomainName(t *testing.T) {
t.Run("nonempty domain ID", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{DomainID: testdata.DomainID}
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
expected := testdata.DomainName
mockDomainCache.EXPECT().GetDomainName(childInfo.DomainID).Return(expected, nil)
name, err := GetChildExecutionDomainName(childInfo, mockDomainCache, constants.TestLocalDomainEntry)
require.NoError(t, err)
assert.Equal(t, expected, name)
})

t.Run("nonempty domain name", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{DomainNameDEPRECATED: testdata.DomainName}
parentDomainEntry := constants.TestLocalDomainEntry
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
name, err := GetChildExecutionDomainName(childInfo, mockDomainCache, parentDomainEntry)
require.NoError(t, err)
assert.Equal(t, testdata.DomainName, name)
})

t.Run("empty childInfo", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{}
parentDomainEntry := constants.TestLocalDomainEntry
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
name, err := GetChildExecutionDomainName(childInfo, mockDomainCache, parentDomainEntry)
require.NoError(t, err)
assert.Equal(t, parentDomainEntry.GetInfo().Name, name)
})
}

func TestGetChildExecutionDomainID(t *testing.T) {
t.Run("nonempty domain ID", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{DomainID: testdata.DomainID}
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
name, err := GetChildExecutionDomainID(childInfo, mockDomainCache, constants.TestLocalDomainEntry)
require.NoError(t, err)
assert.Equal(t, testdata.DomainID, name)
})

t.Run("nonempty domain name", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{DomainNameDEPRECATED: testdata.DomainName}
parentDomainEntry := constants.TestLocalDomainEntry
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
mockDomainCache.EXPECT().GetDomainID(testdata.DomainName).Return(testdata.DomainID, nil)
name, err := GetChildExecutionDomainID(childInfo, mockDomainCache, parentDomainEntry)
require.NoError(t, err)
assert.Equal(t, testdata.DomainID, name)
})

t.Run("empty childInfo", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{}
parentDomainEntry := constants.TestLocalDomainEntry
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
name, err := GetChildExecutionDomainID(childInfo, mockDomainCache, parentDomainEntry)
require.NoError(t, err)
assert.Equal(t, parentDomainEntry.GetInfo().ID, name)
})
}

func TestGetChildExecutionDomainEntry(t *testing.T) {
t.Run("nonempty domain ID", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{DomainID: testdata.DomainID}
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
expected := &cache.DomainCacheEntry{}
mockDomainCache.EXPECT().GetDomainByID(childInfo.DomainID).Return(expected, nil)
entry, err := GetChildExecutionDomainEntry(childInfo, mockDomainCache, constants.TestLocalDomainEntry)
require.NoError(t, err)
assert.Equal(t, expected, entry)
})

t.Run("nonempty domain name", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{DomainNameDEPRECATED: testdata.DomainName}
parentDomainEntry := constants.TestLocalDomainEntry
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
expected := &cache.DomainCacheEntry{}
mockDomainCache.EXPECT().GetDomain(childInfo.DomainNameDEPRECATED).Return(expected, nil)
entry, err := GetChildExecutionDomainEntry(childInfo, mockDomainCache, parentDomainEntry)
require.NoError(t, err)
assert.Equal(t, expected, entry)
})

t.Run("empty childInfo", func(t *testing.T) {
childInfo := &persistence.ChildExecutionInfo{}
parentDomainEntry := constants.TestLocalDomainEntry
mockDomainCache := cache.NewMockDomainCache(gomock.NewController(t))
entry, err := GetChildExecutionDomainEntry(childInfo, mockDomainCache, parentDomainEntry)
require.NoError(t, err)
assert.Equal(t, parentDomainEntry, entry)
})
}

func TestConvert(t *testing.T) {
t.Run("convertSyncActivityInfos", func(t *testing.T) {
activityInfos := map[int64]*persistence.ActivityInfo{1: {Version: 1, ScheduleID: 1}}
inputs := map[int64]struct{}{1: {}}
outputs := convertSyncActivityInfos(activityInfos, inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, int64(1), outputs[0].(*persistence.SyncActivityTask).ScheduledID)
assert.Equal(t, int64(1), outputs[0].GetVersion())
})

t.Run("convertUpdateRequestCancelInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.RequestCancelInfo{key: {}}
outputs := convertUpdateRequestCancelInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})

t.Run("convertPendingRequestCancelInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.RequestCancelInfo{key: {}}
outputs := convertPendingRequestCancelInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})

t.Run("convertInt64SetToSlice", func(t *testing.T) {
key := int64(0)
inputs := map[int64]struct{}{key: {}}
outputs := convertInt64SetToSlice(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, key, outputs[0])
})

t.Run("convertUpdateChildExecutionInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.ChildExecutionInfo{key: {}}
outputs := convertUpdateChildExecutionInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})

t.Run("convertUpdateSignalInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.SignalInfo{key: {}}
outputs := convertUpdateSignalInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})
}

func TestScheduleDecision(t *testing.T) {
t.Run("mutable state has pending decision", func(t *testing.T) {
mockMutableState := NewMockMutableState(gomock.NewController(t))
mockMutableState.EXPECT().HasPendingDecision().Return(true)
err := ScheduleDecision(mockMutableState)
require.NoError(t, err)
})

t.Run("internal service error", func(t *testing.T) {
mockMutableState := NewMockMutableState(gomock.NewController(t))
mockMutableState.EXPECT().HasPendingDecision().Return(false)
mockMutableState.EXPECT().AddDecisionTaskScheduledEvent(false).Return(nil, errors.New("some error"))
err := ScheduleDecision(mockMutableState)
assert.NotNil(t, err)
assert.Equal(t, "Failed to add decision scheduled event.", err.Error())
})

t.Run("success", func(t *testing.T) {
mockMutableState := NewMockMutableState(gomock.NewController(t))
mockMutableState.EXPECT().HasPendingDecision().Return(false)
mockMutableState.EXPECT().AddDecisionTaskScheduledEvent(false).Return(nil, nil)
err := ScheduleDecision(mockMutableState)
require.NoError(t, err)
})
}

func TestFailDecision(t *testing.T) {
t.Run("success", func(t *testing.T) {
mockMutableState := NewMockMutableState(gomock.NewController(t))
decision := &DecisionInfo{}
failureCause := new(types.DecisionTaskFailedCause)
mockMutableState.EXPECT().AddDecisionTaskFailedEvent(
decision.ScheduleID,
decision.StartedID,
*failureCause,
nil,
IdentityHistoryService,
"",
"",
"",
"",
int64(0),
"",
).Return(nil, nil)
mockMutableState.EXPECT().FlushBufferedEvents() // only on success
err := FailDecision(mockMutableState, decision, *failureCause)
require.NoError(t, err)
})

t.Run("failure", func(t *testing.T) {
mockMutableState := NewMockMutableState(gomock.NewController(t))
decision := &DecisionInfo{}
failureCause := new(types.DecisionTaskFailedCause)
mockMutableState.EXPECT().AddDecisionTaskFailedEvent(
decision.ScheduleID,
decision.StartedID,
*failureCause,
nil,
IdentityHistoryService,
"",
"",
"",
"",
int64(0),
"",
).Return(nil, errors.New("some error adding failed event"))
err := FailDecision(mockMutableState, decision, *failureCause)
assert.NotNil(t, err)
assert.Equal(t, "some error adding failed event", err.Error())
})
}
Loading