Skip to content

Commit

Permalink
chore(tests): use mockery constructors to ensure assertions (#2500)
Browse files Browse the repository at this point in the history
- Use generated mockery constructors for mockery mocks to ensure assertions are done in the test
- Fix bugs caused by the now-fixed mock assertions
- Remove existing `.Assert*` mock method calls
- Fix data race in storage observers
  • Loading branch information
qdm12 authored Sep 2, 2022
1 parent b98c45a commit 78a1dbd
Show file tree
Hide file tree
Showing 34 changed files with 448 additions and 497 deletions.
38 changes: 19 additions & 19 deletions devnet/cmd/scale-down-ecs-service/service_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
//go:generate mockery --srcpkg=github.com/aws/aws-sdk-go/service/ecs/ecsiface --name ECSAPI --case underscore

func Test_serviceScaler_findServiceArns(t *testing.T) {
mockECS := mocks.ECSAPI{}
mockECS := mocks.NewECSAPI(t)
mockECS.
On("ListServicesWithContext", mock.Anything, &ecs.ListServicesInput{
Cluster: aws.String("someCluster"),
Expand Down Expand Up @@ -66,7 +66,7 @@ func Test_serviceScaler_findServiceArns(t *testing.T) {
name: "with next token",
fields: fields{
cluster: "someCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -83,7 +83,7 @@ func Test_serviceScaler_findServiceArns(t *testing.T) {
name: "ListServicesWithContext err",
fields: fields{
cluster: "someErrCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -95,7 +95,7 @@ func Test_serviceScaler_findServiceArns(t *testing.T) {
name: "no services err",
fields: fields{
cluster: "someEmptyCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -106,7 +106,7 @@ func Test_serviceScaler_findServiceArns(t *testing.T) {
{
name: "regex err",
fields: fields{
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand Down Expand Up @@ -135,7 +135,7 @@ func Test_serviceScaler_findServiceArns(t *testing.T) {
}

func Test_serviceScaler_drainServices(t *testing.T) {
mockECS := mocks.ECSAPI{}
mockECS := mocks.NewECSAPI(t)
mockECS.
On("UpdateServiceWithContext", mock.Anything, &ecs.UpdateServiceInput{
Cluster: aws.String("someCluster"),
Expand Down Expand Up @@ -172,7 +172,7 @@ func Test_serviceScaler_drainServices(t *testing.T) {
name: "happy path",
fields: fields{
cluster: "someCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -186,7 +186,7 @@ func Test_serviceScaler_drainServices(t *testing.T) {
name: "UpdateServiceWithContext err",
fields: fields{
cluster: "someErrCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -213,7 +213,7 @@ func Test_serviceScaler_drainServices(t *testing.T) {
}

func Test_serviceScaler_waitForRunningCount(t *testing.T) {
mockECS := mocks.ECSAPI{}
mockECS := mocks.NewECSAPI(t)
mockECS.
On("DescribeServicesWithContext", mock.Anything, &ecs.DescribeServicesInput{
Cluster: aws.String("someCluster"),
Expand Down Expand Up @@ -241,7 +241,7 @@ func Test_serviceScaler_waitForRunningCount(t *testing.T) {
}).Return(nil, fmt.Errorf("someError")).Once()

ctx, cancel := context.WithCancel(context.Background())
mockECSCancel := mocks.ECSAPI{}
mockECSCancel := mocks.NewECSAPI(t)
mockECSCancel.
On("DescribeServicesWithContext", mock.Anything, &ecs.DescribeServicesInput{
Cluster: aws.String("someCluster"),
Expand Down Expand Up @@ -288,7 +288,7 @@ func Test_serviceScaler_waitForRunningCount(t *testing.T) {
fields: fields{
tickerDuration: time.Nanosecond,
cluster: "someCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -303,7 +303,7 @@ func Test_serviceScaler_waitForRunningCount(t *testing.T) {
fields: fields{
tickerDuration: time.Nanosecond,
cluster: "someErrorCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -319,7 +319,7 @@ func Test_serviceScaler_waitForRunningCount(t *testing.T) {
fields: fields{
tickerDuration: 10 * time.Millisecond,
cluster: "someCluster",
ecs: &mockECSCancel,
ecs: mockECSCancel,
},
args: args{
ctx: ctx,
Expand Down Expand Up @@ -371,7 +371,7 @@ func Test_newServiceScaler(t *testing.T) {
}

func Test_serviceScaler_scaleServices(t *testing.T) {
mockECS := mocks.ECSAPI{}
mockECS := mocks.NewECSAPI(t)
mockECS.
On("ListServicesWithContext", mock.Anything, &ecs.ListServicesInput{
Cluster: aws.String("someCluster"),
Expand Down Expand Up @@ -408,13 +408,13 @@ func Test_serviceScaler_scaleServices(t *testing.T) {
},
}}, nil).Once()

findServiceArnsErrECS := mocks.ECSAPI{}
findServiceArnsErrECS := mocks.NewECSAPI(t)
findServiceArnsErrECS.
On("ListServicesWithContext", mock.Anything, &ecs.ListServicesInput{
Cluster: aws.String("someCluster"),
}).Return(nil, fmt.Errorf("someError")).Once()

updateServicesErrECS := mocks.ECSAPI{}
updateServicesErrECS := mocks.NewECSAPI(t)
updateServicesErrECS.
On("ListServicesWithContext", mock.Anything, &ecs.ListServicesInput{
Cluster: aws.String("someCluster"),
Expand Down Expand Up @@ -449,7 +449,7 @@ func Test_serviceScaler_scaleServices(t *testing.T) {
fields: fields{
tickerDuration: 10 * time.Millisecond,
cluster: "someCluster",
ecs: &mockECS,
ecs: mockECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -461,7 +461,7 @@ func Test_serviceScaler_scaleServices(t *testing.T) {
fields: fields{
tickerDuration: 10 * time.Millisecond,
cluster: "someCluster",
ecs: &findServiceArnsErrECS,
ecs: findServiceArnsErrECS,
},
args: args{
ctx: context.Background(),
Expand All @@ -474,7 +474,7 @@ func Test_serviceScaler_scaleServices(t *testing.T) {
fields: fields{
tickerDuration: 10 * time.Millisecond,
cluster: "someCluster",
ecs: &updateServicesErrECS,
ecs: updateServicesErrECS,
},
args: args{
ctx: context.Background(),
Expand Down
6 changes: 3 additions & 3 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func TestServiceHandleTransactionMessage(t *testing.T) {
testEmptyHeader := types.NewEmptyHeader()
testExtrinsic := []types.Extrinsic{{1, 2, 3}}

runtimeMock := new(mocksruntime.Instance)
runtimeMock2 := new(mocksruntime.Instance)
runtimeMock3 := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
runtimeMock2 := mocksruntime.NewInstance(t)
runtimeMock3 := mocksruntime.NewInstance(t)

type args struct {
peerID peer.ID
Expand Down
26 changes: 13 additions & 13 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func Test_Service_handleBlock(t *testing.T) {
block.Header.Number = 21

ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().StoreTrie(trieState, &block.Header).Return(nil)
mockBlockState := NewMockBlockState(ctrl)
Expand All @@ -458,7 +458,7 @@ func Test_Service_handleBlock(t *testing.T) {
block.Header.Number = 21

ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().StoreTrie(trieState, &block.Header).Return(nil)
mockBlockState := NewMockBlockState(ctrl)
Expand Down Expand Up @@ -516,7 +516,7 @@ func Test_Service_HandleBlockProduced(t *testing.T) {
}

ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().StoreTrie(trieState, &block.Header).Return(nil)
mockBlockState := NewMockBlockState(ctrl)
Expand Down Expand Up @@ -558,7 +558,7 @@ func Test_Service_maintainTransactionPool(t *testing.T) {
vt := transaction.NewValidTransaction(extrinsic, validity)

ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
runtimeMock.On("ValidateTransaction", types.Extrinsic{21}).Return(nil, errTestDummyError)
mockTxnState := NewMockTransactionState(ctrl)
mockTxnState.EXPECT().RemoveExtrinsic(types.Extrinsic{21}).Times(2)
Expand Down Expand Up @@ -593,7 +593,7 @@ func Test_Service_maintainTransactionPool(t *testing.T) {
tx := transaction.NewValidTransaction(types.Extrinsic{21}, &transaction.Validity{Propagate: true})

ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
runtimeMock.On("ValidateTransaction", types.Extrinsic{21}).
Return(&transaction.Validity{Propagate: true}, nil)
mockTxnState := NewMockTransactionState(ctrl)
Expand Down Expand Up @@ -682,7 +682,7 @@ func Test_Service_handleBlocksAsync(t *testing.T) {
block.Header.Number = 21

ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
runtimeMock.On("ValidateTransaction", types.Extrinsic{21}).Return(nil, errTestDummyError)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{}).Times(2)
Expand Down Expand Up @@ -812,7 +812,7 @@ func TestService_handleChainReorg(t *testing.T) {
t.Run("invalid transaction", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
runtimeMockErr := new(mocksruntime.Instance)
runtimeMockErr := mocksruntime.NewInstance(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().HighestCommonAncestor(testPrevHash, testCurrentHash).
Return(testAncestorHash, nil)
Expand All @@ -831,7 +831,7 @@ func TestService_handleChainReorg(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
runtimeMockOk := new(mocksruntime.Instance)
runtimeMockOk := mocksruntime.NewInstance(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().HighestCommonAncestor(testPrevHash, testCurrentHash).
Return(testAncestorHash, nil)
Expand Down Expand Up @@ -981,7 +981,7 @@ func TestService_DecodeSessionKeys(t *testing.T) {
t.Run("ok case", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
runtimeMock.On("DecodeSessionKeys", testEncKeys).Return(testEncKeys, nil)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetRuntime(nil).Return(runtimeMock, nil)
Expand Down Expand Up @@ -1075,7 +1075,7 @@ func TestServiceGetRuntimeVersion(t *testing.T) {
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil).MaxTimes(2)
mockStorageState.EXPECT().TrieState(&common.Hash{}).Return(ts, nil).MaxTimes(2)

runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetRuntime(&common.Hash{}).Return(runtimeMock, nil)
runtimeMock.On("SetContextStorage", ts)
Expand Down Expand Up @@ -1154,7 +1154,7 @@ func TestServiceHandleSubmittedExtrinsic(t *testing.T) {
ctrl := gomock.NewController(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
runtimeMockErr := new(mocksruntime.Instance)
runtimeMockErr := mocksruntime.NewInstance(t)
mockBlockState.EXPECT().GetRuntime(&common.Hash{}).Return(runtimeMockErr, nil).MaxTimes(2)

mockStorageState := NewMockStorageState(ctrl)
Expand All @@ -1179,7 +1179,7 @@ func TestServiceHandleSubmittedExtrinsic(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)

runtimeMock := new(mocksruntime.Instance)
runtimeMock := mocksruntime.NewInstance(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{})
mockBlockState.EXPECT().GetRuntime(&common.Hash{}).Return(runtimeMock, nil).MaxTimes(2)
Expand Down Expand Up @@ -1258,7 +1258,7 @@ func TestServiceGetMetadata(t *testing.T) {
ctrl := gomock.NewController(t)
mockStorageState := NewMockStorageState(ctrl)
mockStorageState.EXPECT().TrieState(nil).Return(&rtstorage.TrieState{}, nil)
runtimeMockOk := new(mocksruntime.Instance)
runtimeMockOk := mocksruntime.NewInstance(t)
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().GetRuntime(nil).Return(runtimeMockOk, nil)
runtimeMockOk.On("SetContextStorage", &rtstorage.TrieState{})
Expand Down
22 changes: 7 additions & 15 deletions dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

func TestRegisterModules(t *testing.T) {
rpcapiMocks := new(mocks.RPCAPI)
rpcapiMocks := mocks.NewRPCAPI(t)

mods := []string{
"system", "author", "chain",
Expand All @@ -56,10 +56,6 @@ func TestRegisterModules(t *testing.T) {
}

NewHTTPServer(cfg)

for _, modName := range mods {
rpcapiMocks.AssertCalled(t, "BuildMethodNames", mock.Anything, modName)
}
}

func TestNewHTTPServer(t *testing.T) {
Expand Down Expand Up @@ -177,7 +173,7 @@ func TestRPCUnsafeExpose(t *testing.T) {
_, err := buf.Write(data)
require.NoError(t, err)

netmock := new(mocks.NetworkAPI)
netmock := mocks.NewNetworkAPI(t)
netmock.On("AddReservedPeers", mock.AnythingOfType("string")).Return(nil)

cfg := &HTTPServerConfig{
Expand Down Expand Up @@ -214,15 +210,11 @@ func TestUnsafeRPCJustToLocalhost(t *testing.T) {
_, err := buf.Write(data)
require.NoError(t, err)

netmock := new(mocks.NetworkAPI)
netmock.On("AddReservedPeers", mock.AnythingOfType("string")).Return(nil)

cfg := &HTTPServerConfig{
Modules: []string{"system"},
RPCPort: 7880,
RPCAPI: NewService(),
RPCUnsafe: true,
NetworkAPI: netmock,
Modules: []string{"system"},
RPCPort: 7880,
RPCAPI: NewService(),
RPCUnsafe: true,
}

s := NewHTTPServer(cfg)
Expand Down Expand Up @@ -262,7 +254,7 @@ func TestRPCExternalEnable_UnsafeExternalNotEnabled(t *testing.T) {
safebuf := new(bytes.Buffer)
safebuf.Write(safeData)

netmock := new(mocks.NetworkAPI)
netmock := mocks.NewNetworkAPI(t)
netmock.On("NetworkState").Return(common.NetworkState{
PeerID: "peer id",
})
Expand Down
Loading

0 comments on commit 78a1dbd

Please sign in to comment.