Skip to content

Commit

Permalink
testing(dot/sync): create dot sync unit tests (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmack authored Jun 13, 2022
1 parent 6220402 commit 16313f8
Show file tree
Hide file tree
Showing 31 changed files with 6,197 additions and 489 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ linters:
- nolintlint
- revive
- staticcheck
- tparallel
- unconvert
- unparam
- varcheck
Expand Down
1 change: 1 addition & 0 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func TestAnnounceBlock(t *testing.T) {
}

func TestService_InsertKey(t *testing.T) {
t.Parallel()
ks := keystore.NewGlobalKeystore()

cfg := &Config{
Expand Down
3 changes: 3 additions & 0 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func TestAuthorModule_SubmitExtrinsic_AlreadyInPool(t *testing.T) {
}

func TestAuthorModule_InsertKey_Integration(t *testing.T) {
t.Parallel()
integrationTestController := setupStateAndRuntime(t, t.TempDir(), useInstanceFromGenesis)
auth := newAuthorModule(t, integrationTestController)

Expand Down Expand Up @@ -336,6 +337,7 @@ func TestAuthorModule_InsertKey_Integration(t *testing.T) {
}

func TestAuthorModule_HasKey_Integration(t *testing.T) {
t.Parallel()
integrationTestController := setupStateAndRuntime(t, t.TempDir(), useInstanceFromGenesis)

ks := keystore.NewGlobalKeystore()
Expand Down Expand Up @@ -403,6 +405,7 @@ func TestAuthorModule_HasKey_Integration(t *testing.T) {
}

func TestAuthorModule_HasSessionKeys_Integration(t *testing.T) {
t.Parallel()
integrationTestController := setupStateAndRuntime(t, t.TempDir(), useInstanceFromGenesis)
auth := newAuthorModule(t, integrationTestController)

Expand Down
2 changes: 2 additions & 0 deletions dot/sync/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func Test_newSyncBenchmarker(t *testing.T) {
t.Parallel()

t.Run("10 samples to keep", func(t *testing.T) {
t.Parallel()
const samplesToKeep = 10
actual := newSyncBenchmarker(samplesToKeep)

Expand All @@ -27,6 +28,7 @@ func Test_newSyncBenchmarker(t *testing.T) {
})

t.Run("panics on 0 sample to keep", func(t *testing.T) {
t.Parallel()
const samplesToKeep = 0
assert.PanicsWithValue(t, "cannot have 0 samples to keep", func() {
newSyncBenchmarker(samplesToKeep)
Expand Down
113 changes: 113 additions & 0 deletions dot/sync/bootstrap_syncer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package sync

import (
"errors"
"testing"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func Test_bootstrapSyncer_handleWorkerResult(t *testing.T) {
t.Parallel()
mockError := errors.New("mock testing error")

tests := map[string]struct {
blockStateBuilder func(ctrl *gomock.Controller) BlockState
worker *worker
wantWorkerToRetry *worker
err error
}{
"nil worker.err returns nil": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
return NewMockBlockState(ctrl)
},
worker: &worker{},
},
"best block header error": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHeader().Return(nil,
mockError)
return mockBlockState
},
worker: &worker{
err: &workerError{},
targetNumber: uintPtr(0),
},
err: mockError,
},
"targetNumber < bestBlockHeader number returns nil": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHeader().Return(&types.Header{Number: 2}, nil)
return mockBlockState
},
worker: &worker{
err: &workerError{},
targetNumber: uintPtr(0),
},
},
"targetNumber > bestBlockHeader number worker errUnknownParent, error GetHighestFinalisedHeader": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHeader().Return(&types.Header{Number: 2}, nil)
mockBlockState.EXPECT().GetHighestFinalisedHeader().Return(nil, mockError)
return mockBlockState
},
worker: &worker{
err: &workerError{err: errUnknownParent},
targetNumber: uintPtr(3),
},
err: mockError,
},
"targetNumber > bestBlockHeader number worker errUnknownParent returns worker": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHeader().Return(&types.Header{Number: 2}, nil)
mockBlockState.EXPECT().GetHighestFinalisedHeader().Return(&types.Header{Number: 1}, nil)
return mockBlockState
},
worker: &worker{
err: &workerError{err: errUnknownParent},
targetNumber: uintPtr(3),
},
wantWorkerToRetry: &worker{
startNumber: uintPtr(1),
targetNumber: uintPtr(3),
},
},
"targetNumber > bestBlockHeader number returns worker": {
blockStateBuilder: func(ctrl *gomock.Controller) BlockState {
mockBlockState := NewMockBlockState(ctrl)
mockBlockState.EXPECT().BestBlockHeader().Return(&types.Header{Number: 2}, nil)
return mockBlockState
},
worker: &worker{
err: &workerError{},
targetNumber: uintPtr(3),
},
wantWorkerToRetry: &worker{
startNumber: uintPtr(3),
targetNumber: uintPtr(3),
},
},
}
for testName, tt := range tests {
tt := tt
t.Run(testName, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
s := &bootstrapSyncer{
blockState: tt.blockStateBuilder(ctrl),
}
gotWorkerToRetry, err := s.handleWorkerResult(tt.worker)
assert.ErrorIs(t, err, tt.err)
assert.Equal(t, tt.wantWorkerToRetry, gotWorkerToRetry)
})
}
}
18 changes: 3 additions & 15 deletions dot/sync/chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/ChainSafe/gossamer/lib/blocktree"
)

//go:generate mockgen -destination=mock_chain_processor_test.go -package=$GOPACKAGE . ChainProcessor

// ChainProcessor processes ready blocks.
// it is implemented by *chainProcessor
type ChainProcessor interface {
Expand Down Expand Up @@ -160,7 +162,7 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {
logger.Debugf("processing block data with hash %s", bd.Hash)

if bd.Header != nil && bd.Body != nil {
if err := s.handleHeader(bd.Header); err != nil {
if err := s.babeVerifier.VerifyBlock(bd.Header); err != nil {
return err
}

Expand Down Expand Up @@ -191,16 +193,6 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {
return nil
}

// handleHeader handles headers included in BlockResponses
func (s *chainProcessor) handleHeader(header *types.Header) error {
err := s.babeVerifier.VerifyBlock(header)
if err != nil {
return fmt.Errorf("%w: %s", ErrInvalidBlock, err.Error())
}

return nil
}

// handleHeader handles block bodies included in BlockResponses
func (s *chainProcessor) handleBody(body *types.Body) {
for _, ext := range *body {
Expand All @@ -210,10 +202,6 @@ func (s *chainProcessor) handleBody(body *types.Body) {

// handleHeader handles blocks (header+body) included in BlockResponses
func (s *chainProcessor) handleBlock(block *types.Block) error {
if block == nil || block.Body == nil {
return errors.New("block or body is nil")
}

parent, err := s.blockState.GetHeader(block.Header.ParentHash)
if err != nil {
return fmt.Errorf("%w: %s", errFailedToGetParent, err)
Expand Down
Loading

0 comments on commit 16313f8

Please sign in to comment.