github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/gossip/service/eventer_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 service
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/config"
    24  	"github.com/hyperledger/fabric/gossip/util"
    25  	"github.com/hyperledger/fabric/protos/peer"
    26  )
    27  
    28  const testChainID = "foo"
    29  
    30  func init() {
    31  	util.SetupTestLogging()
    32  }
    33  
    34  type mockReceiver struct {
    35  	orgs     map[string]config.ApplicationOrg
    36  	sequence uint64
    37  }
    38  
    39  func (mr *mockReceiver) configUpdated(config Config) {
    40  	logger.Debugf("[TEST] Setting config to %d %v", config.Sequence(), config.Organizations())
    41  	mr.orgs = config.Organizations()
    42  	mr.sequence = config.Sequence()
    43  }
    44  
    45  type mockConfig mockReceiver
    46  
    47  func (mc *mockConfig) Sequence() uint64 {
    48  	return mc.sequence
    49  }
    50  
    51  func (mc *mockConfig) Organizations() map[string]config.ApplicationOrg {
    52  	return mc.orgs
    53  }
    54  
    55  func (mc *mockConfig) ChainID() string {
    56  	return testChainID
    57  }
    58  
    59  const testOrgID = "testID"
    60  
    61  func TestInitialUpdate(t *testing.T) {
    62  	mc := &mockConfig{
    63  		sequence: 7,
    64  		orgs: map[string]config.ApplicationOrg{
    65  			testOrgID: &appGrp{
    66  				anchorPeers: []*peer.AnchorPeer{{Port: 9}},
    67  			},
    68  		},
    69  	}
    70  
    71  	mr := &mockReceiver{}
    72  
    73  	ce := newConfigEventer(mr)
    74  	ce.ProcessConfigUpdate(mc)
    75  
    76  	if !reflect.DeepEqual(mc, (*mockConfig)(mr)) {
    77  		t.Fatalf("Should have updated config on initial update but did not")
    78  	}
    79  }
    80  
    81  func TestSecondUpdate(t *testing.T) {
    82  	appGrps := map[string]config.ApplicationOrg{
    83  		testOrgID: &appGrp{
    84  			anchorPeers: []*peer.AnchorPeer{{Port: 9}},
    85  		},
    86  	}
    87  	mc := &mockConfig{
    88  		sequence: 7,
    89  		orgs:     appGrps,
    90  	}
    91  
    92  	mr := &mockReceiver{}
    93  
    94  	ce := newConfigEventer(mr)
    95  	ce.ProcessConfigUpdate(mc)
    96  
    97  	mc.sequence = 8
    98  	appGrps[testOrgID] = &appGrp{
    99  		anchorPeers: []*peer.AnchorPeer{{Port: 10}},
   100  	}
   101  
   102  	ce.ProcessConfigUpdate(mc)
   103  
   104  	if !reflect.DeepEqual(mc, (*mockConfig)(mr)) {
   105  		t.Fatal("Should have updated config on initial update but did not")
   106  	}
   107  }
   108  
   109  func TestSecondSameUpdate(t *testing.T) {
   110  	mc := &mockConfig{
   111  		sequence: 7,
   112  		orgs: map[string]config.ApplicationOrg{
   113  			testOrgID: &appGrp{
   114  				anchorPeers: []*peer.AnchorPeer{{Port: 9}},
   115  			},
   116  		},
   117  	}
   118  
   119  	mr := &mockReceiver{}
   120  
   121  	ce := newConfigEventer(mr)
   122  	ce.ProcessConfigUpdate(mc)
   123  	mr.sequence = 0
   124  	mr.orgs = nil
   125  	ce.ProcessConfigUpdate(mc)
   126  
   127  	if mr.sequence != 0 {
   128  		t.Error("Should not have updated sequence when reprocessing same config")
   129  	}
   130  
   131  	if mr.orgs != nil {
   132  		t.Error("Should not have updated anchor peers when reprocessing same config")
   133  	}
   134  }
   135  
   136  func TestUpdatedSeqOnly(t *testing.T) {
   137  	mc := &mockConfig{
   138  		sequence: 7,
   139  		orgs: map[string]config.ApplicationOrg{
   140  			testOrgID: &appGrp{
   141  				anchorPeers: []*peer.AnchorPeer{{Port: 9}},
   142  			},
   143  		},
   144  	}
   145  
   146  	mr := &mockReceiver{}
   147  
   148  	ce := newConfigEventer(mr)
   149  	ce.ProcessConfigUpdate(mc)
   150  	mc.sequence = 9
   151  	ce.ProcessConfigUpdate(mc)
   152  
   153  	if mr.sequence != 7 {
   154  		t.Errorf("Should not have updated sequence when reprocessing same config")
   155  	}
   156  
   157  	if !reflect.DeepEqual(mr.orgs, mc.orgs) {
   158  		t.Errorf("Should not have cleared anchor peers when reprocessing newer config with higher sequence")
   159  	}
   160  }