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 unit test for helper function of sql execution store #5707

Merged
merged 1 commit into from
Mar 4, 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
72 changes: 72 additions & 0 deletions common/persistence/sql/sql_execution_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
"github.com/stretchr/testify/require"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/log/testlogger"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/persistence/serialization"
"github.com/uber/cadence/common/persistence/sql/sqlplugin"
"github.com/uber/cadence/common/types"
)

func TestDeleteCurrentWorkflowExecution(t *testing.T) {
Expand Down Expand Up @@ -1883,3 +1885,73 @@ func TestDeleteWorkflowExecution(t *testing.T) {
})
}
}

func TestTxExecuteShardLocked(t *testing.T) {
tests := []struct {
name string
mockSetup func(*sqlplugin.MockDB, *sqlplugin.MockTx)
operation string
rangeID int64
fn func(sqlplugin.Tx) error
wantError error
}{
{
name: "Success",
mockSetup: func(mockDB *sqlplugin.MockDB, mockTx *sqlplugin.MockTx) {
mockDB.EXPECT().BeginTx(gomock.Any(), gomock.Any()).Return(mockTx, nil)
mockTx.EXPECT().ReadLockShards(gomock.Any(), gomock.Any()).Return(11, nil)
mockTx.EXPECT().Commit().Return(nil)
},
operation: "Insert",
rangeID: 11,
fn: func(sqlplugin.Tx) error { return nil },
wantError: nil,
},
{
name: "Error",
mockSetup: func(mockDB *sqlplugin.MockDB, mockTx *sqlplugin.MockTx) {
mockDB.EXPECT().BeginTx(gomock.Any(), gomock.Any()).Return(mockTx, nil)
mockTx.EXPECT().ReadLockShards(gomock.Any(), gomock.Any()).Return(11, nil)
mockTx.EXPECT().Rollback().Return(nil)
mockDB.EXPECT().IsNotFoundError(gomock.Any()).Return(false)
mockDB.EXPECT().IsTimeoutError(gomock.Any()).Return(false)
mockDB.EXPECT().IsThrottlingError(gomock.Any()).Return(false)
},
operation: "Insert",
rangeID: 11,
fn: func(sqlplugin.Tx) error { return errors.New("error") },
wantError: &types.InternalServiceError{Message: "Insert operation failed. Error: error"},
},
{
name: "Error - shard ownership lost",
mockSetup: func(mockDB *sqlplugin.MockDB, mockTx *sqlplugin.MockTx) {
mockDB.EXPECT().BeginTx(gomock.Any(), gomock.Any()).Return(mockTx, nil)
mockTx.EXPECT().ReadLockShards(gomock.Any(), gomock.Any()).Return(12, nil)
mockTx.EXPECT().Rollback().Return(nil)
},
operation: "Insert",
rangeID: 11,
fn: func(sqlplugin.Tx) error { return errors.New("error") },
wantError: &persistence.ShardOwnershipLostError{ShardID: 0, Msg: "Failed to lock shard. Previous range ID: 11; new range ID: 12"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
mockDB := sqlplugin.NewMockDB(ctrl)
mockTx := sqlplugin.NewMockTx(ctrl)
tt.mockSetup(mockDB, mockTx)

s := &sqlExecutionStore{
shardID: 0,
sqlStore: sqlStore{
db: mockDB,
logger: testlogger.New(t),
},
}

gotError := s.txExecuteShardLocked(context.Background(), 0, tt.operation, tt.rangeID, tt.fn)
assert.Equal(t, tt.wantError, gotError)
})
}
}