github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/gossip/util/msgs_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "testing" 21 22 "github.com/hyperledger/fabric/gossip/common" 23 proto "github.com/hyperledger/fabric/protos/gossip" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func init() { 28 SetupTestLogging() 29 } 30 31 func TestMembershipStore(t *testing.T) { 32 membershipStore := NewMembershipStore() 33 34 id1 := common.PKIidType("id1") 35 id2 := common.PKIidType("id2") 36 37 msg1 := &proto.SignedGossipMessage{} 38 msg2 := &proto.SignedGossipMessage{Envelope: &proto.Envelope{}} 39 40 // Test initially created store is empty 41 assert.Nil(t, membershipStore.MsgByID(id1)) 42 assert.Equal(t, membershipStore.Size(), 0) 43 // Test put works as expected 44 membershipStore.Put(id1, msg1) 45 assert.NotNil(t, membershipStore.MsgByID(id1)) 46 // Test MsgByID returns the right instance stored 47 membershipStore.Put(id2, msg2) 48 assert.Equal(t, msg1, membershipStore.MsgByID(id1)) 49 assert.NotEqual(t, msg2, membershipStore.MsgByID(id1)) 50 // Test capacity grows 51 assert.Equal(t, membershipStore.Size(), 2) 52 // Test remove works 53 membershipStore.Remove(id1) 54 assert.Nil(t, membershipStore.MsgByID(id1)) 55 assert.Equal(t, membershipStore.Size(), 1) 56 // Test returned instance is not a copy 57 msg3 := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}} 58 msg3Clone := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}} 59 id3 := common.PKIidType("id3") 60 membershipStore.Put(id3, msg3) 61 assert.Equal(t, msg3Clone, msg3) 62 membershipStore.MsgByID(id3).Channel = []byte{0, 1, 2, 3} 63 assert.NotEqual(t, msg3Clone, msg3) 64 } 65 66 func TestToSlice(t *testing.T) { 67 membershipStore := NewMembershipStore() 68 id1 := common.PKIidType("id1") 69 id2 := common.PKIidType("id2") 70 id3 := common.PKIidType("id3") 71 id4 := common.PKIidType("id4") 72 73 msg1 := &proto.SignedGossipMessage{} 74 msg2 := &proto.SignedGossipMessage{Envelope: &proto.Envelope{}} 75 msg3 := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}} 76 msg4 := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}, Envelope: &proto.Envelope{}} 77 78 membershipStore.Put(id1, msg1) 79 membershipStore.Put(id2, msg2) 80 membershipStore.Put(id3, msg3) 81 membershipStore.Put(id4, msg4) 82 83 assert.Len(t, membershipStore.ToSlice(), 4) 84 85 existsInSlice := func(slice []*proto.SignedGossipMessage, msg *proto.SignedGossipMessage) bool { 86 for _, m := range slice { 87 if assert.ObjectsAreEqual(m, msg) { 88 return true 89 } 90 } 91 return false 92 } 93 94 expectedMsgs := []*proto.SignedGossipMessage{msg1, msg2, msg3, msg4} 95 for _, msg := range membershipStore.ToSlice() { 96 assert.True(t, existsInSlice(expectedMsgs, msg)) 97 } 98 99 }