github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/dbft/backend/handler_test.go (about) 1 package backend 2 3 /* 4 import ( 5 "testing" 6 7 lru "github.com/hashicorp/golang-lru" 8 "github.com/quickchainproject/quickchain/common" 9 "github.com/quickchainproject/quickchain/consensus/dbft" 10 "github.com/quickchainproject/quickchain/p2p" 11 "github.com/quickchainproject/quickchain/rlp" 12 ) 13 14 func TestBFTMessage(t *testing.T) { 15 _, backend := newBlockChain(1) 16 17 // generate one msg 18 data := []byte("data1") 19 hash := bft.RLPHash(data) 20 msg := makeMsg(bftMsg, data) 21 addr := common.StringToAddress("address") 22 23 // 1. this message should not be in cache 24 // for peers 25 if _, ok := backend.recentMessages.Get(addr); ok { 26 t.Fatalf("the cache of messages for this peer should be nil") 27 } 28 29 // for self 30 if _, ok := backend.knownMessages.Get(hash); ok { 31 t.Fatalf("the cache of messages should be nil") 32 } 33 34 // 2. this message should be in cache after we handle it 35 _, err := backend.HandleMsg(addr, msg) 36 if err != nil { 37 t.Fatalf("handle message failed: %v", err) 38 } 39 // for peers 40 if ms, ok := backend.recentMessages.Get(addr); ms == nil || !ok { 41 t.Fatalf("the cache of messages for this peer cannot be nil") 42 } else if m, ok := ms.(*lru.ARCCache); !ok { 43 t.Fatalf("the cache of messages for this peer cannot be casted") 44 } else if _, ok := m.Get(hash); !ok { 45 t.Fatalf("the cache of messages for this peer cannot be found") 46 } 47 48 // for self 49 if _, ok := backend.knownMessages.Get(hash); !ok { 50 t.Fatalf("the cache of messages cannot be found") 51 } 52 } 53 54 func makeMsg(msgcode uint64, data interface{}) p2p.Msg { 55 size, r, _ := rlp.EncodeToReader(data) 56 return p2p.Msg{Code: msgcode, Size: uint32(size), Payload: r} 57 } 58 */