Skip to content

Commit

Permalink
Remove mockery from sync package (#2699)
Browse files Browse the repository at this point in the history
* wip/remove mockery

* remove mockery network mocks in sync

* wip/remove blockstate mockery mocks from sync

* test remove mockery blockState mocks

* add anyTimes to tip syncer mocks

* make mock calls any times for now

* wip

* remove mockery BlockImportHandler

* wip

* remove sync mockery mocks

* maybe fix tests

* clean up and lint

* remove mockery from sync package interface

* specify times that mock func is called

* remove times when times is 1 for mocks
  • Loading branch information
jimjbrettj authored Jul 26, 2022
1 parent c803ac6 commit 75fcd75
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 765 deletions.
5 changes: 1 addition & 4 deletions dot/network/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ type (
NotificationsMessageBatchHandler = func(peer peer.ID, msg NotificationsMessage)
)

// BatchMessage is exported for the mocks of lib/grandpa/mocks/network.go
// to be able to compile.
// TODO: unexport if changing mock library to e.g. github.com/golang/gomock
type BatchMessage struct {
type batchMessage struct {
msg NotificationsMessage
peer peer.ID
}
Expand Down
2 changes: 1 addition & 1 deletion dot/network/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func TestCreateNotificationsMessageHandler_HandleTransaction(t *testing.T) {

srvc2 := createTestService(t, configB)

txnBatch := make(chan *BatchMessage, batchSize)
txnBatch := make(chan *batchMessage, batchSize)
txnBatchHandler := srvc1.createBatchMessageHandler(txnBatch)

// connect nodes
Expand Down
2 changes: 1 addition & 1 deletion dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (s *Service) Start() error {
blockAnnounceID, err)
}

txnBatch := make(chan *BatchMessage, s.cfg.batchSize)
txnBatch := make(chan *batchMessage, s.cfg.batchSize)
txnBatchHandler := s.createBatchMessageHandler(txnBatch)

// register transactions protocol
Expand Down
6 changes: 3 additions & 3 deletions dot/network/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func decodeTransactionHandshake(_ []byte) (Handshake, error) {
return &transactionHandshake{}, nil
}

func (s *Service) startTxnBatchProcessing(txnBatchCh chan *BatchMessage, slotDuration time.Duration) {
func (s *Service) startTxnBatchProcessing(txnBatchCh chan *batchMessage, slotDuration time.Duration) {
protocolID := s.host.protocolID + transactionsID
ticker := time.NewTicker(slotDuration)
defer ticker.Stop()
Expand Down Expand Up @@ -148,11 +148,11 @@ func (s *Service) startTxnBatchProcessing(txnBatchCh chan *BatchMessage, slotDur
}
}

func (s *Service) createBatchMessageHandler(txnBatchCh chan *BatchMessage) NotificationsMessageBatchHandler {
func (s *Service) createBatchMessageHandler(txnBatchCh chan *batchMessage) NotificationsMessageBatchHandler {
go s.startTxnBatchProcessing(txnBatchCh, s.cfg.SlotDuration)

return func(peer peer.ID, msg NotificationsMessage) {
data := &BatchMessage{
data := &batchMessage{
msg: msg,
peer: peer,
}
Expand Down
9 changes: 5 additions & 4 deletions dot/sync/bootstrap_syncer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ package sync
import (
"testing"

syncmocks "github.com/ChainSafe/gossamer/dot/sync/mocks"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

Expand All @@ -28,9 +28,10 @@ func newTestBootstrapSyncer(t *testing.T) *bootstrapSyncer {
trie.EmptyHash, 200, types.NewDigest())
require.NoError(t, err)

bs := new(syncmocks.BlockState)
bs.On("BestBlockHeader").Return(header, nil)
bs.On("GetHighestFinalisedHeader").Return(finHeader, nil)
ctrl := gomock.NewController(t)
bs := NewMockBlockState(ctrl)
bs.EXPECT().BestBlockHeader().Return(header, nil).AnyTimes()
bs.EXPECT().GetHighestFinalisedHeader().Return(finHeader, nil).AnyTimes()

return newBootstrapSyncer(bs)
}
Expand Down
28 changes: 15 additions & 13 deletions dot/sync/chain_sync_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/sync/mocks"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/common/variadic"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -75,12 +73,14 @@ func TestChainSync_sync_bootstrap_withWorkerError_Integration(t *testing.T) {
func TestChainSync_sync_tip_Integration(t *testing.T) {
ctrl := gomock.NewController(t)
cs := newTestChainSync(ctrl)
cs.blockState = new(mocks.BlockState)
header, err := types.NewHeader(common.Hash{0}, trie.EmptyHash, trie.EmptyHash, 1000,
types.NewDigest())
require.NoError(t, err)
cs.blockState.(*mocks.BlockState).On("BestBlockHeader").Return(header, nil)
cs.blockState.(*mocks.BlockState).On("GetHighestFinalisedHeader").Return(header, nil)

bs := NewMockBlockState(ctrl)
bs.EXPECT().BestBlockHeader().Return(header, nil)
bs.EXPECT().GetHighestFinalisedHeader().Return(header, nil)
cs.blockState = bs

go cs.sync()
defer cs.cancel()
Expand Down Expand Up @@ -452,8 +452,8 @@ func TestChainSync_validateResponse_Integration(t *testing.T) {
func TestChainSync_validateResponse_firstBlock_Integration(t *testing.T) {
ctrl := gomock.NewController(t)
cs := newTestChainSync(ctrl)
bs := new(mocks.BlockState)
bs.On("HasHeader", mock.AnythingOfType("common.Hash")).Return(false, nil)
bs := NewMockBlockState(ctrl)
bs.EXPECT().HasHeader(gomock.AssignableToTypeOf(common.Hash{})).Return(false, nil)
cs.blockState = bs

req := &network.BlockRequestMessage{
Expand Down Expand Up @@ -539,9 +539,10 @@ func TestChainSync_doSync_Integration(t *testing.T) {
},
}

cs.network = new(mocks.Network)
cs.network.(*mocks.Network).On("DoBlockRequest", mock.AnythingOfType("peer.ID"),
mock.AnythingOfType("*network.BlockRequestMessage")).Return(resp, nil)
net := NewMockNetwork(ctrl)
net.EXPECT().DoBlockRequest(gomock.AssignableToTypeOf(peer.ID("0")),
gomock.AssignableToTypeOf(&network.BlockRequestMessage{})).Return(resp, nil)
cs.network = net

workerErr = cs.doSync(req, make(map[peer.ID]struct{}))
require.Nil(t, workerErr)
Expand Down Expand Up @@ -574,9 +575,10 @@ func TestChainSync_doSync_Integration(t *testing.T) {

// test to see if descending blocks get reversed
req.Direction = network.Descending
cs.network = new(mocks.Network)
cs.network.(*mocks.Network).On("DoBlockRequest", mock.AnythingOfType("peer.ID"),
mock.AnythingOfType("*network.BlockRequestMessage")).Return(resp, nil)
net = NewMockNetwork(ctrl)
net.EXPECT().DoBlockRequest(gomock.AssignableToTypeOf(peer.ID("0")),
gomock.AssignableToTypeOf(&network.BlockRequestMessage{})).Return(resp, nil)
cs.network = net
workerErr = cs.doSync(req, make(map[peer.ID]struct{}))
require.Nil(t, workerErr)

Expand Down
13 changes: 7 additions & 6 deletions dot/sync/chain_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import (

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/sync/mocks"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/common/variadic"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -440,14 +438,17 @@ func TestChainSync_sync_tip(t *testing.T) {

ctrl := gomock.NewController(t)
cs := newTestChainSync(ctrl)
cs.blockState = new(mocks.BlockState)
header, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, 1000,
types.NewDigest())
require.NoError(t, err)
cs.blockState.(*mocks.BlockState).On("BestBlockHeader").Return(header, nil)
cs.blockState.(*mocks.BlockState).On("GetHighestFinalisedHeader").Run(func(args mock.Arguments) {

bs := NewMockBlockState(ctrl)
bs.EXPECT().BestBlockHeader().Return(header, nil)
bs.EXPECT().GetHighestFinalisedHeader().DoAndReturn(func() (*types.Header, error) {
close(done)
}).Return(header, nil)
return header, nil
})
cs.blockState = bs

go cs.sync()
defer cs.cancel()
Expand Down
3 changes: 0 additions & 3 deletions dot/sync/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)

// TODO: replace usage of mockery generated mocks with mockgen generated mocks.
// Note: This mockery go:generate is still being used
//go:generate mockery --name BlockState --structname BlockState --case underscore --keeptree
//go:generate mockgen -destination=mock_interface_test.go -package=$GOPACKAGE . BlockState,StorageState,CodeSubstitutedState,TransactionState,BabeVerifier,FinalityGadget,BlockImportHandler,Network

// BlockState is the interface for the block state
Expand Down
28 changes: 0 additions & 28 deletions dot/sync/mocks/babe_verifier.go

This file was deleted.

29 changes: 0 additions & 29 deletions dot/sync/mocks/block_import_handler.go

This file was deleted.

Loading

0 comments on commit 75fcd75

Please sign in to comment.