github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/dbft/core/core_test.go (about) 1 package core 2 3 import ( 4 "math/big" 5 "reflect" 6 "testing" 7 "time" 8 9 "github.com/quickchainproject/quickchain/consensus/dbft" 10 "github.com/quickchainproject/quickchain/core/types" 11 elog "github.com/quickchainproject/quickchain/log" 12 ) 13 14 func makeBlock(number int64) *types.Block { 15 header := &types.Header{ 16 Difficulty: big.NewInt(0), 17 Number: big.NewInt(number), 18 GasLimit: big.NewInt(0).Uint64(), 19 GasUsed: big.NewInt(0).Uint64(), 20 Time: big.NewInt(0), 21 } 22 block := &types.Block{} 23 return block.WithSeal(header) 24 } 25 26 func newTestProposal() bft.Proposal { 27 return makeBlock(1) 28 } 29 30 func TestNewRequest(t *testing.T) { 31 testLogger.SetHandler(elog.StdoutHandler) 32 33 N := uint64(4) 34 F := uint64(1) 35 36 sys := NewTestSystemWithBackend(N, F) 37 38 close := sys.Run(true) 39 defer close() 40 41 request1 := makeBlock(1) 42 sys.backends[0].NewRequest(request1) 43 44 select { 45 case <-time.After(1 * time.Second): 46 } 47 48 request2 := makeBlock(2) 49 sys.backends[0].NewRequest(request2) 50 51 select { 52 case <-time.After(1 * time.Second): 53 } 54 55 for _, backend := range sys.backends { 56 if len(backend.committedMsgs) != 2 { 57 t.Errorf("the number of executed requests mismatch: have %v, want 2", len(backend.committedMsgs)) 58 } 59 if !reflect.DeepEqual(request1.Number(), backend.committedMsgs[0].commitProposal.Number()) { 60 t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.committedMsgs[0].commitProposal.Number()) 61 } 62 if !reflect.DeepEqual(request2.Number(), backend.committedMsgs[1].commitProposal.Number()) { 63 t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.committedMsgs[1].commitProposal.Number()) 64 } 65 } 66 }