github.com/Hnampk/fabric@v2.1.1+incompatible/gossip/filter/filter_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package filter 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/gossip/comm" 13 "github.com/hyperledger/fabric/gossip/common" 14 "github.com/hyperledger/fabric/gossip/discovery" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestSelectPolicies(t *testing.T) { 19 assert.True(t, SelectAllPolicy(discovery.NetworkMember{})) 20 assert.False(t, SelectNonePolicy(discovery.NetworkMember{})) 21 } 22 23 func TestCombineRoutingFilters(t *testing.T) { 24 nm := discovery.NetworkMember{ 25 Endpoint: "a", 26 InternalEndpoint: "b", 27 } 28 // Ensure that combine routing filter is a logical AND 29 a := func(nm discovery.NetworkMember) bool { 30 return nm.Endpoint == "a" 31 } 32 b := func(nm discovery.NetworkMember) bool { 33 return nm.InternalEndpoint == "b" 34 } 35 assert.True(t, CombineRoutingFilters(a, b)(nm)) 36 assert.False(t, CombineRoutingFilters(CombineRoutingFilters(a, b), SelectNonePolicy)(nm)) 37 assert.False(t, CombineRoutingFilters(a, b)(discovery.NetworkMember{InternalEndpoint: "b"})) 38 } 39 40 func TestAnyMatch(t *testing.T) { 41 peerA := discovery.NetworkMember{Endpoint: "a"} 42 peerB := discovery.NetworkMember{Endpoint: "b"} 43 peerC := discovery.NetworkMember{Endpoint: "c"} 44 peerD := discovery.NetworkMember{Endpoint: "d"} 45 46 peers := []discovery.NetworkMember{peerA, peerB, peerC, peerD} 47 48 matchB := func(nm discovery.NetworkMember) bool { 49 return nm.Endpoint == "b" 50 } 51 matchC := func(nm discovery.NetworkMember) bool { 52 return nm.Endpoint == "c" 53 } 54 55 matched := AnyMatch(peers, matchB, matchC) 56 assert.Len(t, matched, 2) 57 assert.Contains(t, matched, peerB) 58 assert.Contains(t, matched, peerC) 59 } 60 61 func TestFirst(t *testing.T) { 62 var peers []discovery.NetworkMember 63 // nil slice 64 assert.Nil(t, First(nil, SelectAllPolicy)) 65 66 // empty slice 67 peers = []discovery.NetworkMember{} 68 assert.Nil(t, First(peers, SelectAllPolicy)) 69 70 // first in slice with any marcher 71 peerA := discovery.NetworkMember{Endpoint: "a"} 72 peerB := discovery.NetworkMember{Endpoint: "b"} 73 peers = []discovery.NetworkMember{peerA, peerB} 74 assert.Equal(t, &comm.RemotePeer{Endpoint: "a"}, First(peers, SelectAllPolicy)) 75 76 // second in slice with matcher that checks for a specific peer 77 assert.Equal(t, &comm.RemotePeer{Endpoint: "b"}, First(peers, func(nm discovery.NetworkMember) bool { 78 return nm.PreferredEndpoint() == "b" 79 })) 80 } 81 82 func TestSelectPeers(t *testing.T) { 83 a := func(nm discovery.NetworkMember) bool { 84 return nm.Endpoint == "a" 85 } 86 b := func(nm discovery.NetworkMember) bool { 87 return nm.InternalEndpoint == "b" 88 } 89 nm1 := discovery.NetworkMember{ 90 Endpoint: "a", 91 InternalEndpoint: "b", 92 PKIid: common.PKIidType("a"), 93 } 94 nm2 := discovery.NetworkMember{ 95 Endpoint: "a", 96 InternalEndpoint: "b", 97 PKIid: common.PKIidType("b"), 98 } 99 nm3 := discovery.NetworkMember{ 100 Endpoint: "d", 101 InternalEndpoint: "b", 102 PKIid: common.PKIidType("c"), 103 } 104 105 // individual filters 106 assert.Len(t, SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, a), 2) 107 assert.Len(t, SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, b), 3) 108 // combined filters 109 crf := CombineRoutingFilters(a, b) 110 assert.Len(t, SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, crf), 2) 111 assert.Len(t, SelectPeers(1, []discovery.NetworkMember{nm1, nm2, nm3}, crf), 1) 112 } 113 114 func BenchmarkSelectPeers(t *testing.B) { 115 a := func(nm discovery.NetworkMember) bool { 116 return nm.Endpoint == "a" 117 } 118 b := func(nm discovery.NetworkMember) bool { 119 return nm.InternalEndpoint == "b" 120 } 121 nm1 := discovery.NetworkMember{ 122 Endpoint: "a", 123 InternalEndpoint: "b", 124 PKIid: common.PKIidType("a"), 125 } 126 nm2 := discovery.NetworkMember{ 127 Endpoint: "a", 128 InternalEndpoint: "b", 129 PKIid: common.PKIidType("b"), 130 } 131 nm3 := discovery.NetworkMember{ 132 Endpoint: "d", 133 InternalEndpoint: "b", 134 PKIid: common.PKIidType("c"), 135 } 136 crf := CombineRoutingFilters(a, b) 137 138 var l1, l2, l3, l4 int 139 140 t.ResetTimer() 141 for i := 0; i < t.N; i++ { 142 // individual filters 143 l1 = len(SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, a)) 144 l2 = len(SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, b)) 145 // combined filters 146 l3 = len(SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, crf)) 147 l4 = len(SelectPeers(1, []discovery.NetworkMember{nm1, nm2, nm3}, crf)) 148 } 149 t.StopTimer() 150 151 assert.Equal(t, l1, 2) 152 assert.Equal(t, l2, 3) 153 assert.Equal(t, l3, 2) 154 assert.Equal(t, l4, 1) 155 }