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