github.com/defanghe/fabric@v2.1.1+incompatible/gossip/comm/mock/mock_comm_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package mock 8 9 import ( 10 "testing" 11 12 proto "github.com/hyperledger/fabric-protos-go/gossip" 13 "github.com/hyperledger/fabric/gossip/comm" 14 "github.com/hyperledger/fabric/gossip/common" 15 "github.com/hyperledger/fabric/gossip/protoext" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestMockComm(t *testing.T) { 20 first := &socketMock{"first", make(chan interface{})} 21 second := &socketMock{"second", make(chan interface{})} 22 members := make(map[string]*socketMock) 23 24 members[first.endpoint] = first 25 members[second.endpoint] = second 26 27 comm1 := NewCommMock(first.endpoint, members) 28 defer comm1.Stop() 29 30 msgCh := comm1.Accept(func(message interface{}) bool { 31 return message.(protoext.ReceivedMessage).GetGossipMessage().GetStateRequest() != nil || 32 message.(protoext.ReceivedMessage).GetGossipMessage().GetStateResponse() != nil 33 }) 34 35 comm2 := NewCommMock(second.endpoint, members) 36 defer comm2.Stop() 37 38 sMsg, _ := protoext.NoopSign(&proto.GossipMessage{ 39 Content: &proto.GossipMessage_StateRequest{StateRequest: &proto.RemoteStateRequest{ 40 StartSeqNum: 1, 41 EndSeqNum: 3, 42 }}, 43 }) 44 comm2.Send(sMsg, &comm.RemotePeer{Endpoint: "first", PKIID: common.PKIidType("first")}) 45 46 msg := <-msgCh 47 48 assert.NotNil(t, msg.GetGossipMessage().GetStateRequest()) 49 assert.Equal(t, "first", string(comm1.GetPKIid())) 50 } 51 52 func TestMockComm_PingPong(t *testing.T) { 53 members := make(map[string]*socketMock) 54 55 members["peerA"] = &socketMock{"peerA", make(chan interface{})} 56 members["peerB"] = &socketMock{"peerB", make(chan interface{})} 57 58 peerA := NewCommMock("peerA", members) 59 peerB := NewCommMock("peerB", members) 60 61 all := func(interface{}) bool { 62 return true 63 } 64 65 rcvChA := peerA.Accept(all) 66 rcvChB := peerB.Accept(all) 67 68 sMsg, _ := protoext.NoopSign(&proto.GossipMessage{ 69 Content: &proto.GossipMessage_DataMsg{ 70 DataMsg: &proto.DataMessage{ 71 Payload: &proto.Payload{ 72 SeqNum: 1, 73 Data: []byte("Ping"), 74 }, 75 }}, 76 }) 77 peerA.Send(sMsg, &comm.RemotePeer{Endpoint: "peerB", PKIID: common.PKIidType("peerB")}) 78 79 msg := <-rcvChB 80 dataMsg := msg.GetGossipMessage().GetDataMsg() 81 data := string(dataMsg.Payload.Data) 82 assert.Equal(t, "Ping", data) 83 84 msg.Respond(&proto.GossipMessage{ 85 Content: &proto.GossipMessage_DataMsg{ 86 DataMsg: &proto.DataMessage{ 87 Payload: &proto.Payload{ 88 SeqNum: 1, 89 Data: []byte("Pong"), 90 }, 91 }}, 92 }) 93 94 msg = <-rcvChA 95 dataMsg = msg.GetGossipMessage().GetDataMsg() 96 data = string(dataMsg.Payload.Data) 97 assert.Equal(t, "Pong", data) 98 99 }