github.com/koko1123/flow-go-1@v0.29.6/engine/consensus/provider/engine_test.go (about) 1 // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED 2 3 package provider 4 5 import ( 6 "testing" 7 8 "github.com/stretchr/testify/mock" 9 "github.com/stretchr/testify/suite" 10 11 "github.com/koko1123/flow-go-1/model/flow" 12 "github.com/koko1123/flow-go-1/module/metrics" 13 module "github.com/koko1123/flow-go-1/module/mock" 14 "github.com/koko1123/flow-go-1/module/trace" 15 "github.com/koko1123/flow-go-1/network/mocknetwork" 16 protocol "github.com/koko1123/flow-go-1/state/protocol/mock" 17 "github.com/koko1123/flow-go-1/utils/unittest" 18 ) 19 20 type Suite struct { 21 suite.Suite 22 23 me *module.Local 24 conduit *mocknetwork.Conduit 25 state *protocol.State 26 final *protocol.Snapshot 27 28 identities flow.IdentityList 29 30 engine *Engine 31 } 32 33 func TestProviderEngine(t *testing.T) { 34 suite.Run(t, new(Suite)) 35 } 36 37 func (suite *Suite) SetupTest() { 38 39 suite.me = new(module.Local) 40 suite.conduit = new(mocknetwork.Conduit) 41 suite.state = new(protocol.State) 42 suite.final = new(protocol.Snapshot) 43 44 suite.engine = &Engine{ 45 me: suite.me, 46 state: suite.state, 47 con: suite.conduit, 48 message: metrics.NewNoopCollector(), 49 tracer: trace.NewNoopTracer(), 50 } 51 52 suite.identities = unittest.CompleteIdentitySet() 53 localID := suite.identities[0].NodeID 54 55 suite.me.On("NodeID").Return(localID) 56 suite.state.On("Final").Return(suite.final) 57 suite.final.On("Identities", mock.Anything).Return( 58 func(f flow.IdentityFilter) flow.IdentityList { return suite.identities.Filter(f) }, 59 func(flow.IdentityFilter) error { return nil }, 60 ) 61 } 62 63 // proposals submitted by remote nodes should not be accepted. 64 func (suite *Suite) TestOnBlockProposal_RemoteOrigin() { 65 66 proposal := unittest.ProposalFixture() 67 // message submitted by remote node 68 err := suite.engine.onBlockProposal(suite.identities[1].NodeID, proposal) 69 suite.Assert().Error(err) 70 } 71 72 func (suite *Suite) OnBlockProposal_Success() { 73 74 proposal := unittest.ProposalFixture() 75 76 params := []interface{}{proposal} 77 for _, identity := range suite.identities { 78 // skip consensus nodes 79 if identity.Role == flow.RoleConsensus { 80 continue 81 } 82 params = append(params, identity.NodeID) 83 } 84 85 suite.conduit.On("Publish", params...).Return(nil).Once() 86 87 err := suite.engine.onBlockProposal(suite.me.NodeID(), proposal) 88 suite.Require().Nil(err) 89 suite.conduit.AssertExpectations(suite.T()) 90 }