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

Added a test for the GetworkflowExecution in the nosql_execution_store.go file. #5692

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Changes from 3 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
177 changes: 127 additions & 50 deletions common/persistence/nosql/nosql_execution_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,108 +34,179 @@ import (
"github.com/uber/cadence/service/history/constants"
)

func TestCreateWorkflowExecution(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDB := nosqlplugin.NewMockDB(ctrl)
logger := log.NewNoop()

store := &nosqlExecutionStore{
shardID: 1,
nosqlStore: nosqlStore{logger: logger, db: mockDB},
}

func TestNosqlExecutionStore(t *testing.T) {
ctx := context.Background()
request := &persistence.InternalCreateWorkflowExecutionRequest{
RangeID: 123,
Mode: persistence.CreateWorkflowModeBrandNew,
PreviousRunID: "previous-run-id",
PreviousLastWriteVersion: 456,
NewWorkflowSnapshot: getNewWorkflowSnapshot(),
}
shardID := 1
testCases := []struct {
name string
setupMock func()
request *persistence.InternalCreateWorkflowExecutionRequest
setupMock func(*gomock.Controller) *nosqlExecutionStore
agautam478 marked this conversation as resolved.
Show resolved Hide resolved
testFunc func(*nosqlExecutionStore) error
expectedError error
}{
{
name: "success",
setupMock: func() {
name: "CreateWorkflowExecution success",
agautam478 marked this conversation as resolved.
Show resolved Hide resolved
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
InsertWorkflowExecutionWithTasks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
agautam478 marked this conversation as resolved.
Show resolved Hide resolved
Return(nil).Times(1)
Return(nil)
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.CreateWorkflowExecution(ctx, newCreateWorkflowExecutionRequest())
return err
},
request: request,
expectedError: nil,
},
{
name: "failure - workflow already exists",
setupMock: func() {
name: "CreateWorkflowExecution failure - workflow already exists",
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
InsertWorkflowExecutionWithTasks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&persistence.WorkflowExecutionAlreadyStartedError{}).Times(1)
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false).AnyTimes() // Assuming the error is not a "not found" error
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsTimeoutError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsThrottlingError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsDBUnavailableError(gomock.Any()).Return(false).AnyTimes()
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.CreateWorkflowExecution(ctx, newCreateWorkflowExecutionRequest())
return err
},
request: request,
expectedError: &persistence.WorkflowExecutionAlreadyStartedError{},
},
{
name: "shard ownership lost",
setupMock: func() {
name: "CreateWorkflowExecution failure - shard ownership lost",
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
InsertWorkflowExecutionWithTasks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&persistence.ShardOwnershipLostError{ShardID: store.shardID, Msg: "shard ownership lost"}).
Times(1)
Return(&persistence.ShardOwnershipLostError{ShardID: shardID, Msg: "shard ownership lost"}).Times(1)
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsTimeoutError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsThrottlingError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsDBUnavailableError(gomock.Any()).Return(false).AnyTimes()
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.CreateWorkflowExecution(ctx, newCreateWorkflowExecutionRequest())
return err
},
request: request,
expectedError: &persistence.ShardOwnershipLostError{},
},
{
name: "current workflow condition failed",
setupMock: func() {
name: "CreateWorkflowExecution failure - current workflow condition failed",
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
InsertWorkflowExecutionWithTasks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&persistence.CurrentWorkflowConditionFailedError{Msg: "current workflow condition failed"}).
Times(1)
Return(&persistence.CurrentWorkflowConditionFailedError{Msg: "current workflow condition failed"}).Times(1)
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsTimeoutError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsThrottlingError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsDBUnavailableError(gomock.Any()).Return(false).AnyTimes()
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.CreateWorkflowExecution(ctx, newCreateWorkflowExecutionRequest())
return err
},
request: request,
expectedError: &persistence.CurrentWorkflowConditionFailedError{},
},
{
name: "generic internal service error",
setupMock: func() {
name: "CreateWorkflowExecution failure - generic internal service error",
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
InsertWorkflowExecutionWithTasks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&types.InternalServiceError{Message: "generic internal service error"}).
Times(1)
Return(&types.InternalServiceError{Message: "generic internal service error"}).Times(1)
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsTimeoutError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsThrottlingError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsDBUnavailableError(gomock.Any()).Return(false).AnyTimes()
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.CreateWorkflowExecution(ctx, newCreateWorkflowExecutionRequest())
return err
},
request: request,
expectedError: &types.InternalServiceError{},
},
{
name: "GetWorkflowExecution success",
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
SelectWorkflowExecution(ctx, shardID, gomock.Any(), gomock.Any(), gomock.Any()).
Return(&nosqlplugin.WorkflowExecution{}, nil).Times(1)
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.GetWorkflowExecution(ctx, newGetWorkflowExecutionRequest())
return err
},
expectedError: nil,
},
{
name: "GetWorkflowExecution failure - not found",
setupMock: func(ctrl *gomock.Controller) *nosqlExecutionStore {
mockDB := nosqlplugin.NewMockDB(ctrl)
mockDB.EXPECT().
SelectWorkflowExecution(ctx, shardID, gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, &types.EntityNotExistsError{}).Times(1)
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false).AnyTimes()
agautam478 marked this conversation as resolved.
Show resolved Hide resolved
mockDB.EXPECT().IsTimeoutError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsThrottlingError(gomock.Any()).Return(false).AnyTimes()
mockDB.EXPECT().IsDBUnavailableError(gomock.Any()).Return(false).AnyTimes()
return newTestNosqlExecutionStore(mockDB, log.NewNoop())
},
testFunc: func(store *nosqlExecutionStore) error {
_, err := store.GetWorkflowExecution(ctx, newGetWorkflowExecutionRequest())
return err
},
expectedError: &types.EntityNotExistsError{},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.setupMock != nil {
tc.setupMock()
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()

_, err := store.CreateWorkflowExecution(ctx, tc.request)
store := tc.setupMock(ctrl)
err := tc.testFunc(store)

if tc.expectedError != nil {
require.ErrorAs(t, err, &tc.expectedError)
var expectedErrType error
require.ErrorAs(t, err, &expectedErrType, "Expected error type does not match.")
} else {
require.NoError(t, err)
}
})
}
}

func newCreateWorkflowExecutionRequest() *persistence.InternalCreateWorkflowExecutionRequest {
return &persistence.InternalCreateWorkflowExecutionRequest{
RangeID: 123,
Mode: persistence.CreateWorkflowModeBrandNew,
PreviousRunID: "previous-run-id",
PreviousLastWriteVersion: 456,
NewWorkflowSnapshot: getNewWorkflowSnapshot(),
}
}

func newGetWorkflowExecutionRequest() *persistence.InternalGetWorkflowExecutionRequest {
return &persistence.InternalGetWorkflowExecutionRequest{
DomainID: constants.TestDomainID,
Execution: types.WorkflowExecution{
WorkflowID: constants.TestWorkflowID,
RunID: constants.TestRunID,
},
}
}

func getNewWorkflowSnapshot() persistence.InternalWorkflowSnapshot {
return persistence.InternalWorkflowSnapshot{
VersionHistories: &persistence.DataBlob{},
Expand All @@ -146,3 +217,9 @@ func getNewWorkflowSnapshot() persistence.InternalWorkflowSnapshot {
},
}
}
func newTestNosqlExecutionStore(db nosqlplugin.DB, logger log.Logger) *nosqlExecutionStore {
return &nosqlExecutionStore{
shardID: 1,
nosqlStore: nosqlStore{logger: logger, db: db},
}
}