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

fix(dot,lib): Replacing handmade mocks to generated new ones #1626

Merged
merged 34 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
abd0bbb
chore: using mockery to generate mocks
EclesioMeloJunior Jun 2, 2021
6694dd5
chore: core tests done
EclesioMeloJunior Jun 3, 2021
2bce519
chore: removing all interfaces and gen the mocks for specifc
EclesioMeloJunior Jun 3, 2021
ddedd2e
chore: network and core tests done
EclesioMeloJunior Jun 4, 2021
a581a29
chore: resolve core, network and rpc tests
EclesioMeloJunior Jun 7, 2021
45b241f
chore: fix state observer mock
EclesioMeloJunior Jun 7, 2021
49d1b24
chore: dot/sync done
EclesioMeloJunior Jun 7, 2021
ae54847
chore: lib/runtime done
EclesioMeloJunior Jun 7, 2021
0b228cc
chore: lib done
EclesioMeloJunior Jun 7, 2021
48400c2
chore: lib done
EclesioMeloJunior Jun 7, 2021
31f113f
Merge branch 'eclesio/cleanup-mock-interfaces' of github.com:ChainSaf…
EclesioMeloJunior Jun 7, 2021
2b3089b
chore: resolve conflicts
EclesioMeloJunior Jun 7, 2021
9dc193c
chore: fix ci lint
EclesioMeloJunior Jun 8, 2021
c1d2d65
chore: update test on dot/sync folder
EclesioMeloJunior Jun 8, 2021
9e42c32
chore: removing debbuging code
EclesioMeloJunior Jun 8, 2021
ab5cb79
chore: resolve problem with interface conversion
EclesioMeloJunior Jun 8, 2021
b2ce4b6
chore: fix test to use require.Len properly
EclesioMeloJunior Jun 8, 2021
cf5ae15
chore: resolving nil pointer exception
EclesioMeloJunior Jun 8, 2021
dc54e99
Merge branch 'development' into eclesio/cleanup-mock-interfaces
EclesioMeloJunior Jun 9, 2021
c31bbdd
chore: remove unused packages
EclesioMeloJunior Jun 9, 2021
05b7e1b
chore: removing prints and update tests
EclesioMeloJunior Jun 9, 2021
0fab0b5
chore: remove sleep at network service clean up func
EclesioMeloJunior Jun 9, 2021
dc0e605
chore: get back with sleep
EclesioMeloJunior Jun 9, 2021
56023dd
chore: remove dead code
EclesioMeloJunior Jun 10, 2021
64f3f89
Merge branch 'development' into eclesio/cleanup-mock-interfaces
EclesioMeloJunior Jun 10, 2021
3024323
chore: use inmemory datastore
EclesioMeloJunior Jun 11, 2021
f126338
chore: get back with gossip test values
EclesioMeloJunior Jun 11, 2021
b7d1411
Merge branch 'eclesio/cleanup-mock-interfaces' of github.com:ChainSaf…
EclesioMeloJunior Jun 11, 2021
34e0c32
chore: remove commented code
EclesioMeloJunior Jun 11, 2021
1ceab10
chore: use mock when non-authority
EclesioMeloJunior Jun 11, 2021
b972926
chore: rolling back lib/services
EclesioMeloJunior Jun 11, 2021
1a3e6d4
Merge branch 'development' into eclesio/cleanup-mock-interfaces
EclesioMeloJunior Jun 11, 2021
555c1dd
chore: remove useless return
EclesioMeloJunior Jun 14, 2021
92e5b88
Merge branch 'eclesio/cleanup-mock-interfaces' of github.com:ChainSaf…
EclesioMeloJunior Jun 14, 2021
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
8 changes: 8 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ Changes that only affect a single file can be tested with
$ go test <file_you_are_working_on>
```

Sometimes you may need to create mocks for interfaces, in that case you will just execute:

```
$ make mock
```

The above command will generate mocks for all the interfaces inside the project! This command does not affect unchanged interfaces.

**8. Lint your changes.**

Before opening a pull request be sure to run the linter
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,16 @@ gossamer: clean
## install: install the gossamer binary in $GOPATH/bin
install:
GOBIN=$(GOPATH)/bin go run scripts/ci.go install

MOCKGEN := $(shell command -v $(GOPATH)/bin/mockery 2> /dev/null)
mock:
ifndef MOCKGEN
@echo "> Installing mockery ..."
@go get github.com/vektra/mockery/v2/.../
endif
@echo "> Generating mocks at ./tests/mocks ..."
$(GOPATH)/bin/mockery --all --recursive --inpackage --case underscore




7 changes: 3 additions & 4 deletions dot/core/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,19 @@ func newGrandpaChange(raw []*types.GrandpaAuthoritiesRaw, delay uint32, currBloc
}

func (h *DigestHandler) handleBABEOnDisabled(d *types.ConsensusDigest, header *types.Header) error {
od := &types.BABEOnDisabled{}
dec, err := scale.Decode(d.Data[1:], od)
od := new(types.BABEOnDisabled)
_, err := scale.Decode(d.Data[1:], od)
if err != nil {
return err
}
od = dec.(*types.BABEOnDisabled)

logger.Debug("handling BABEOnDisabled", "data", od)

err = h.verifier.SetOnDisabled(od.ID, header)

if err != nil {
return err
}

h.babe.SetOnDisabled(od.ID)
return nil
}
Expand Down
36 changes: 28 additions & 8 deletions dot/core/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"

. "github.com/ChainSafe/gossamer/dot/core/mocks"

log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -45,13 +48,17 @@ func newTestDigestHandler(t *testing.T, withBABE, withGrandpa bool) *DigestHandl
err = stateSrvc.Start()
require.NoError(t, err)

var bp BlockProducer
var bp *MockBlockProducer
if withBABE {
bp = &mockBlockProducer{}
bp = new(MockBlockProducer)
blockC := make(chan types.Block)
bp.On("GetBlockChannel", nil).Return(blockC)
}

time.Sleep(time.Second)
dh, err := NewDigestHandler(stateSrvc.Block, stateSrvc.Epoch, stateSrvc.Grandpa, bp, &mockVerifier{})
verifier := new(MockVerifier)
verifier.On("SetOnDisabled", mock.Anything, mock.Anything).Return(nil)

dh, err := NewDigestHandler(stateSrvc.Block, stateSrvc.Epoch, stateSrvc.Grandpa, nil, nil)
require.NoError(t, err)
return dh
}
Expand Down Expand Up @@ -314,8 +321,19 @@ func TestNextGrandpaAuthorityChange_MultipleChanges(t *testing.T) {

func TestDigestHandler_HandleBABEOnDisabled(t *testing.T) {
handler := newTestDigestHandler(t, true, false)
handler.Start()
defer handler.Stop()

babemock := new(MockBlockProducer)
babemock.On("SetOnDisabled", uint32(7))

header := &types.Header{
Number: big.NewInt(1),
}

verifier := new(MockVerifier)
verifier.On("SetOnDisabled", uint32(7), header).Return(nil)

handler.babe = babemock
handler.verifier = verifier

digest := &types.BABEOnDisabled{
ID: 7,
Expand All @@ -329,9 +347,11 @@ func TestDigestHandler_HandleBABEOnDisabled(t *testing.T) {
Data: data,
}

err = handler.HandleConsensusDigest(d, nil)
err = handler.HandleConsensusDigest(d, header)

require.NoError(t, err)
require.Equal(t, uint32(7), handler.babe.(*mockBlockProducer).disabled)

babemock.AssertCalled(t, "SetOnDisabled", uint32(7))
}

func createHeaderWithPreDigest(slotNumber uint64) *types.Header {
Expand Down
41 changes: 25 additions & 16 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

. "github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
Expand All @@ -36,7 +37,8 @@ import (

func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
// TODO: move to sync package
net := new(mockNetwork)
net := new(MockNetwork)

newBlocks := make(chan types.Block)

cfg := &Config{
Expand All @@ -50,28 +52,31 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
err := s.Start()
require.Nil(t, err)

expected := &network.BlockAnnounceMessage{
Number: big.NewInt(1),
ParentHash: s.blockState.BestBlockHash(),
StateRoot: common.Hash{},
ExtrinsicsRoot: common.Hash{},
Digest: nil,
BestBlock: true,
}

// simulate block sent from BABE session
newBlocks <- types.Block{
newBlock := types.Block{
Header: &types.Header{
Number: big.NewInt(1),
ParentHash: s.blockState.BestBlockHash(),
},
Body: types.NewBody([]byte{}),
}

time.Sleep(testMessageTimeout)
require.NotNil(t, net.Message)
require.Equal(t, network.BlockAnnounceMsgType, net.Message.(network.NotificationsMessage).Type())
require.Equal(t, expected, net.Message)
expected := &network.BlockAnnounceMessage{
ParentHash: newBlock.Header.ParentHash,
Number: newBlock.Header.Number,
StateRoot: newBlock.Header.StateRoot,
ExtrinsicsRoot: newBlock.Header.ExtrinsicsRoot,
Digest: newBlock.Header.Digest,
BestBlock: true,
}

//setup the SendMessage function
net.On("SendMessage", expected)
noot marked this conversation as resolved.
Show resolved Hide resolved
newBlocks <- newBlock

time.Sleep(time.Second * 2)

net.AssertCalled(t, "SendMessage", expected)
}

func createExtrinsics(t *testing.T, rt runtime.Instance, genHash common.Hash, nonce uint64) types.Extrinsic {
Expand Down Expand Up @@ -129,11 +134,15 @@ func TestService_HandleTransactionMessage(t *testing.T) {
ks := keystore.NewGlobalKeystore()
ks.Acco.Insert(kp)

bp := new(MockBlockProducer)
blockC := make(chan types.Block)
bp.On("GetBlockChannel", nil).Return(blockC)

cfg := &Config{
Keystore: ks,
TransactionState: state.NewTransactionState(),
IsBlockProducer: true,
BlockProducer: &mockBlockProducer{},
BlockProducer: bp,
}

s := NewTestService(t, cfg)
Expand Down
34 changes: 34 additions & 0 deletions dot/core/mocks/block_producer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions dot/core/mocks/network.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions dot/core/mocks/verifier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/ChainSafe/gossamer/lib/utils"
log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"

coremocks "github.com/ChainSafe/gossamer/dot/core/mocks"
)

func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) []*types.Header {
Expand Down Expand Up @@ -97,7 +99,7 @@ func TestStartService(t *testing.T) {
}

func TestAnnounceBlock(t *testing.T) {
net := new(mockNetwork)
net := new(coremocks.MockNetwork)
newBlocks := make(chan types.Block)

cfg := &Config{
Expand All @@ -111,17 +113,29 @@ func TestAnnounceBlock(t *testing.T) {
defer s.Stop()

// simulate block sent from BABE session
newBlocks <- types.Block{
newBlock := types.Block{
Header: &types.Header{
ParentHash: s.blockState.BestBlockHash(),
Number: big.NewInt(1),
},
Body: &types.Body{},
}

time.Sleep(testMessageTimeout)
require.NotNil(t, net.Message)
require.Equal(t, network.BlockAnnounceMsgType, net.Message.(network.NotificationsMessage).Type())
expected := &network.BlockAnnounceMessage{
ParentHash: newBlock.Header.ParentHash,
Number: newBlock.Header.Number,
StateRoot: newBlock.Header.StateRoot,
ExtrinsicsRoot: newBlock.Header.ExtrinsicsRoot,
Digest: newBlock.Header.Digest,
BestBlock: true,
}

net.On("SendMessage", expected)

newBlocks <- newBlock
time.Sleep(time.Second * 2)

net.AssertCalled(t, "SendMessage", expected)
}

func TestService_HasKey(t *testing.T) {
Expand Down
Loading