github.com/kaituanwang/hyperledger@v2.0.1+incompatible/discovery/support/gossip/support_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package gossip_test 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric-protos-go/gossip" 13 gossipSupport "github.com/hyperledger/fabric/discovery/support/gossip" 14 "github.com/hyperledger/fabric/discovery/support/gossip/mocks" 15 "github.com/hyperledger/fabric/gossip/common" 16 "github.com/hyperledger/fabric/gossip/discovery" 17 "github.com/hyperledger/fabric/gossip/protoext" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestChannelExists(t *testing.T) { 22 g := &mocks.Gossip{} 23 sup := gossipSupport.NewDiscoverySupport(g) 24 assert.False(t, sup.ChannelExists("")) 25 } 26 27 func TestPeers(t *testing.T) { 28 g := &mocks.Gossip{} 29 sup := gossipSupport.NewDiscoverySupport(g) 30 p1Envelope := &gossip.Envelope{ 31 Payload: []byte{4, 5, 6}, 32 SecretEnvelope: &gossip.SecretEnvelope{ 33 Payload: []byte{1, 2, 3}, 34 }, 35 } 36 peers := []discovery.NetworkMember{ 37 {PKIid: common.PKIidType("p1"), Endpoint: "p1", Envelope: p1Envelope}, 38 {PKIid: common.PKIidType("p2")}, 39 } 40 g.PeersReturnsOnCall(0, peers) 41 g.SelfMembershipInfoReturnsOnCall(0, discovery.NetworkMember{PKIid: common.PKIidType("p0"), Endpoint: "p0"}) 42 p1ExpectedEnvelope := &gossip.Envelope{ 43 Payload: []byte{4, 5, 6}, 44 } 45 expected := discovery.Members{{PKIid: common.PKIidType("p1"), Endpoint: "p1", Envelope: p1ExpectedEnvelope}, {PKIid: common.PKIidType("p0"), Endpoint: "p0"}} 46 actual := sup.Peers() 47 assert.Equal(t, expected, actual) 48 } 49 50 func TestPeersOfChannel(t *testing.T) { 51 stateInfo := &gossip.GossipMessage{ 52 Content: &gossip.GossipMessage_StateInfo{ 53 StateInfo: &gossip.StateInfo{ 54 PkiId: common.PKIidType("px"), 55 }, 56 }, 57 } 58 sMsg, _ := protoext.NoopSign(stateInfo) 59 g := &mocks.Gossip{} 60 g.SelfChannelInfoReturnsOnCall(0, nil) 61 g.SelfChannelInfoReturnsOnCall(1, sMsg) 62 g.PeersOfChannelReturnsOnCall(0, []discovery.NetworkMember{{PKIid: common.PKIidType("p1")}, {PKIid: common.PKIidType("p2")}}) 63 sup := gossipSupport.NewDiscoverySupport(g) 64 assert.Empty(t, sup.PeersOfChannel(common.ChannelID(""))) 65 expected := discovery.Members{{PKIid: common.PKIidType("p1")}, {PKIid: common.PKIidType("p2")}, {PKIid: common.PKIidType("px"), Envelope: sMsg.Envelope}} 66 assert.Equal(t, expected, sup.PeersOfChannel(common.ChannelID(""))) 67 }