github.com/kaituanwang/hyperledger@v2.0.1+incompatible/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/hyperledger/fabric/common/channelconfig" 15 "github.com/hyperledger/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 func (mr *mockReceiver) updateEndpoints(chainID string, endpoints []string) { 36 } 37 38 type mockConfig mockReceiver 39 40 func (mc *mockConfig) OrdererAddresses() []string { 41 return []string{"localhost:7050"} 42 } 43 44 func (mc *mockConfig) Sequence() uint64 { 45 return mc.sequence 46 } 47 48 func (mc *mockConfig) Organizations() map[string]channelconfig.ApplicationOrg { 49 return mc.orgs 50 } 51 52 func (mc *mockConfig) ChannelID() string { 53 return testChannelID 54 } 55 56 const testOrgID = "testID" 57 58 func TestInitialUpdate(t *testing.T) { 59 mc := &mockConfig{ 60 sequence: 7, 61 orgs: map[string]channelconfig.ApplicationOrg{ 62 testOrgID: &appGrp{ 63 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 64 }, 65 }, 66 } 67 68 mr := &mockReceiver{} 69 70 ce := newConfigEventer(mr) 71 ce.ProcessConfigUpdate(mc) 72 73 if !reflect.DeepEqual(mc, (*mockConfig)(mr)) { 74 t.Fatalf("Should have updated config on initial update but did not") 75 } 76 } 77 78 func TestSecondUpdate(t *testing.T) { 79 appGrps := map[string]channelconfig.ApplicationOrg{ 80 testOrgID: &appGrp{ 81 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 82 }, 83 } 84 mc := &mockConfig{ 85 sequence: 7, 86 orgs: appGrps, 87 } 88 89 mr := &mockReceiver{} 90 91 ce := newConfigEventer(mr) 92 ce.ProcessConfigUpdate(mc) 93 94 mc.sequence = 8 95 appGrps[testOrgID] = &appGrp{ 96 anchorPeers: []*peer.AnchorPeer{{Port: 10}}, 97 } 98 99 ce.ProcessConfigUpdate(mc) 100 101 if !reflect.DeepEqual(mc, (*mockConfig)(mr)) { 102 t.Fatal("Should have updated config on initial update but did not") 103 } 104 } 105 106 func TestSecondSameUpdate(t *testing.T) { 107 mc := &mockConfig{ 108 sequence: 7, 109 orgs: map[string]channelconfig.ApplicationOrg{ 110 testOrgID: &appGrp{ 111 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 112 }, 113 }, 114 } 115 116 mr := &mockReceiver{} 117 118 ce := newConfigEventer(mr) 119 ce.ProcessConfigUpdate(mc) 120 mr.sequence = 0 121 mr.orgs = nil 122 ce.ProcessConfigUpdate(mc) 123 124 if mr.sequence != 0 { 125 t.Error("Should not have updated sequence when reprocessing same config") 126 } 127 128 if mr.orgs != nil { 129 t.Error("Should not have updated anchor peers when reprocessing same config") 130 } 131 } 132 133 func TestUpdatedSeqOnly(t *testing.T) { 134 mc := &mockConfig{ 135 sequence: 7, 136 orgs: map[string]channelconfig.ApplicationOrg{ 137 testOrgID: &appGrp{ 138 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 139 }, 140 }, 141 } 142 143 mr := &mockReceiver{} 144 145 ce := newConfigEventer(mr) 146 ce.ProcessConfigUpdate(mc) 147 mc.sequence = 9 148 ce.ProcessConfigUpdate(mc) 149 150 if mr.sequence != 7 { 151 t.Errorf("Should not have updated sequence when reprocessing same config") 152 } 153 154 if !reflect.DeepEqual(mr.orgs, mc.orgs) { 155 t.Errorf("Should not have cleared anchor peers when reprocessing newer config with higher sequence") 156 } 157 }