github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/gossip/filter/filter_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 filter
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/hyperledger/fabric/gossip/common"
    23  	"github.com/hyperledger/fabric/gossip/discovery"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestSelectPolicies(t *testing.T) {
    28  	assert.True(t, SelectAllPolicy(discovery.NetworkMember{}))
    29  	assert.False(t, SelectNonePolicy(discovery.NetworkMember{}))
    30  }
    31  
    32  func TestCombineRoutingFilters(t *testing.T) {
    33  	nm := discovery.NetworkMember{
    34  		Endpoint:         "a",
    35  		InternalEndpoint: "b",
    36  	}
    37  	// Ensure that combine routing filter is a logical AND
    38  	a := func(nm discovery.NetworkMember) bool {
    39  		return nm.Endpoint == "a"
    40  	}
    41  	b := func(nm discovery.NetworkMember) bool {
    42  		return nm.InternalEndpoint == "b"
    43  	}
    44  	assert.True(t, CombineRoutingFilters(a, b)(nm))
    45  	assert.False(t, CombineRoutingFilters(CombineRoutingFilters(a, b), SelectNonePolicy)(nm))
    46  	assert.False(t, CombineRoutingFilters(a, b)(discovery.NetworkMember{InternalEndpoint: "b"}))
    47  }
    48  
    49  func TestSelectPeers(t *testing.T) {
    50  	a := func(nm discovery.NetworkMember) bool {
    51  		return nm.Endpoint == "a"
    52  	}
    53  	b := func(nm discovery.NetworkMember) bool {
    54  		return nm.InternalEndpoint == "b"
    55  	}
    56  	nm1 := discovery.NetworkMember{
    57  		Endpoint:         "a",
    58  		InternalEndpoint: "b",
    59  		PKIid:            common.PKIidType("a"),
    60  	}
    61  	nm2 := discovery.NetworkMember{
    62  		Endpoint:         "a",
    63  		InternalEndpoint: "b",
    64  		PKIid:            common.PKIidType("b"),
    65  	}
    66  	nm3 := discovery.NetworkMember{
    67  		Endpoint:         "d",
    68  		InternalEndpoint: "b",
    69  		PKIid:            common.PKIidType("c"),
    70  	}
    71  	assert.Len(t, SelectPeers(3, []discovery.NetworkMember{nm1, nm2, nm3}, CombineRoutingFilters(a, b)), 2)
    72  	assert.Len(t, SelectPeers(1, []discovery.NetworkMember{nm1, nm2, nm3}, CombineRoutingFilters(a, b)), 1)
    73  }