github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/gossip/comm/mock/mock_comm_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 mock 18 19 import ( 20 "testing" 21 22 "github.com/hyperledger/fabric/gossip/comm" 23 "github.com/hyperledger/fabric/gossip/common" 24 proto "github.com/hyperledger/fabric/protos/gossip" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestMockComm(t *testing.T) { 29 first := &socketMock{"first", make(chan interface{})} 30 second := &socketMock{"second", make(chan interface{})} 31 members := make(map[string]*socketMock) 32 33 members[first.endpoint] = first 34 members[second.endpoint] = second 35 36 comm1 := NewCommMock(first.endpoint, members) 37 defer comm1.Stop() 38 39 msgCh := comm1.Accept(func(message interface{}) bool { 40 return message.(proto.ReceivedMessage).GetGossipMessage().GetStateRequest() != nil || 41 message.(proto.ReceivedMessage).GetGossipMessage().GetStateResponse() != nil 42 }) 43 44 comm2 := NewCommMock(second.endpoint, members) 45 defer comm2.Stop() 46 47 comm2.Send((&proto.GossipMessage{ 48 Content: &proto.GossipMessage_StateRequest{&proto.RemoteStateRequest{ 49 StartSeqNum: 1, 50 EndSeqNum: 3, 51 }}, 52 }).NoopSign(), &comm.RemotePeer{"first", common.PKIidType("first")}) 53 54 msg := <-msgCh 55 56 assert.NotNil(t, msg.GetGossipMessage().GetStateRequest()) 57 assert.Equal(t, "first", string(comm1.GetPKIid())) 58 } 59 60 func TestMockComm_PingPong(t *testing.T) { 61 members := make(map[string]*socketMock) 62 63 members["peerA"] = &socketMock{"peerA", make(chan interface{})} 64 members["peerB"] = &socketMock{"peerB", make(chan interface{})} 65 66 peerA := NewCommMock("peerA", members) 67 peerB := NewCommMock("peerB", members) 68 69 all := func(interface{}) bool { 70 return true 71 } 72 73 rcvChA := peerA.Accept(all) 74 rcvChB := peerB.Accept(all) 75 76 peerA.Send((&proto.GossipMessage{ 77 Content: &proto.GossipMessage_DataMsg{ 78 &proto.DataMessage{ 79 &proto.Payload{1, "", []byte("Ping")}, 80 }}, 81 }).NoopSign(), &comm.RemotePeer{"peerB", common.PKIidType("peerB")}) 82 83 msg := <-rcvChB 84 dataMsg := msg.GetGossipMessage().GetDataMsg() 85 data := string(dataMsg.Payload.Data) 86 assert.Equal(t, "Ping", data) 87 88 msg.Respond(&proto.GossipMessage{ 89 Content: &proto.GossipMessage_DataMsg{ 90 &proto.DataMessage{ 91 &proto.Payload{1, "", []byte("Pong")}, 92 }}, 93 }) 94 95 msg = <-rcvChA 96 dataMsg = msg.GetGossipMessage().GetDataMsg() 97 data = string(dataMsg.Payload.Data) 98 assert.Equal(t, "Pong", data) 99 100 }