github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/gossip/service/eventer_test.go (about)

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