github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 TestMembershipStore(t *testing.T) {
    28  	membershipStore := NewMembershipStore()
    29  
    30  	id1 := common.PKIidType("id1")
    31  	id2 := common.PKIidType("id2")
    32  
    33  	msg1 := &proto.SignedGossipMessage{}
    34  	msg2 := &proto.SignedGossipMessage{Envelope: &proto.Envelope{}}
    35  
    36  	// Test initially created store is empty
    37  	assert.Nil(t, membershipStore.MsgByID(id1))
    38  	assert.Equal(t, membershipStore.Size(), 0)
    39  	// Test put works as expected
    40  	membershipStore.Put(id1, msg1)
    41  	assert.NotNil(t, membershipStore.MsgByID(id1))
    42  	// Test MsgByID returns the right instance stored
    43  	membershipStore.Put(id2, msg2)
    44  	assert.Equal(t, msg1, membershipStore.MsgByID(id1))
    45  	assert.NotEqual(t, msg2, membershipStore.MsgByID(id1))
    46  	// Test capacity grows
    47  	assert.Equal(t, membershipStore.Size(), 2)
    48  	// Test remove works
    49  	membershipStore.Remove(id1)
    50  	assert.Nil(t, membershipStore.MsgByID(id1))
    51  	assert.Equal(t, membershipStore.Size(), 1)
    52  	// Test returned instance is not a copy
    53  	msg3 := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}}
    54  	msg3Clone := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}}
    55  	id3 := common.PKIidType("id3")
    56  	membershipStore.Put(id3, msg3)
    57  	assert.Equal(t, msg3Clone, msg3)
    58  	membershipStore.MsgByID(id3).Channel = []byte{0, 1, 2, 3}
    59  	assert.NotEqual(t, msg3Clone, msg3)
    60  }
    61  
    62  func TestToSlice(t *testing.T) {
    63  	membershipStore := NewMembershipStore()
    64  	id1 := common.PKIidType("id1")
    65  	id2 := common.PKIidType("id2")
    66  	id3 := common.PKIidType("id3")
    67  	id4 := common.PKIidType("id4")
    68  
    69  	msg1 := &proto.SignedGossipMessage{}
    70  	msg2 := &proto.SignedGossipMessage{Envelope: &proto.Envelope{}}
    71  	msg3 := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}}
    72  	msg4 := &proto.SignedGossipMessage{GossipMessage: &proto.GossipMessage{}, Envelope: &proto.Envelope{}}
    73  
    74  	membershipStore.Put(id1, msg1)
    75  	membershipStore.Put(id2, msg2)
    76  	membershipStore.Put(id3, msg3)
    77  	membershipStore.Put(id4, msg4)
    78  
    79  	assert.Len(t, membershipStore.ToSlice(), 4)
    80  
    81  	existsInSlice := func(slice []*proto.SignedGossipMessage, msg *proto.SignedGossipMessage) bool {
    82  		for _, m := range slice {
    83  			if assert.ObjectsAreEqual(m, msg) {
    84  				return true
    85  			}
    86  		}
    87  		return false
    88  	}
    89  
    90  	expectedMsgs := []*proto.SignedGossipMessage{msg1, msg2, msg3, msg4}
    91  	for _, msg := range membershipStore.ToSlice() {
    92  		assert.True(t, existsInSlice(expectedMsgs, msg))
    93  	}
    94  
    95  }