github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 type applicationOrgs []*peer.AnchorPeer 31 32 func init() { 33 util.SetupTestLogging() 34 } 35 36 func (ao applicationOrgs) AnchorPeers() []*peer.AnchorPeer { 37 return ao 38 } 39 40 func (ao applicationOrgs) MSPID() string { 41 return "ORG1" 42 } 43 44 func (ao applicationOrgs) Name() string { 45 panic("Unimplimented") 46 } 47 48 type mockReceiver struct { 49 orgs map[string]config.ApplicationOrg 50 sequence uint64 51 } 52 53 func (mr *mockReceiver) configUpdated(config Config) { 54 logger.Debugf("[TEST] Setting config to %d %v", config.Sequence(), config.Organizations()) 55 mr.orgs = config.Organizations() 56 mr.sequence = config.Sequence() 57 } 58 59 type mockConfig mockReceiver 60 61 func (mc *mockConfig) Sequence() uint64 { 62 return mc.sequence 63 } 64 65 func (mc *mockConfig) Organizations() map[string]config.ApplicationOrg { 66 return mc.orgs 67 } 68 69 func (mc *mockConfig) ChainID() string { 70 return testChainID 71 } 72 73 const testOrgID = "testID" 74 75 func TestInitialUpdate(t *testing.T) { 76 mc := &mockConfig{ 77 sequence: 7, 78 orgs: map[string]config.ApplicationOrg{ 79 testOrgID: applicationOrgs([]*peer.AnchorPeer{ 80 &peer.AnchorPeer{ 81 Port: 9, 82 }, 83 }), 84 }, 85 } 86 87 mr := &mockReceiver{} 88 89 ce := newConfigEventer(mr) 90 ce.ProcessConfigUpdate(mc) 91 92 if !reflect.DeepEqual(mc, (*mockConfig)(mr)) { 93 t.Fatalf("Should have updated config on initial update but did not") 94 } 95 } 96 97 func TestSecondUpdate(t *testing.T) { 98 mc := &mockConfig{ 99 sequence: 7, 100 orgs: map[string]config.ApplicationOrg{ 101 testOrgID: applicationOrgs([]*peer.AnchorPeer{ 102 &peer.AnchorPeer{ 103 Port: 9, 104 }, 105 }), 106 }, 107 } 108 109 mr := &mockReceiver{} 110 111 ce := newConfigEventer(mr) 112 ce.ProcessConfigUpdate(mc) 113 114 mc.sequence = 8 115 mc.orgs = map[string]config.ApplicationOrg{ 116 testOrgID: applicationOrgs([]*peer.AnchorPeer{ 117 &peer.AnchorPeer{ 118 Port: 10, 119 }, 120 }), 121 } 122 123 ce.ProcessConfigUpdate(mc) 124 125 if !reflect.DeepEqual(mc, (*mockConfig)(mr)) { 126 t.Fatalf("Should have updated config on initial update but did not") 127 } 128 } 129 130 func TestSecondSameUpdate(t *testing.T) { 131 mc := &mockConfig{ 132 sequence: 7, 133 orgs: map[string]config.ApplicationOrg{ 134 testOrgID: applicationOrgs([]*peer.AnchorPeer{ 135 &peer.AnchorPeer{ 136 Port: 9, 137 }, 138 }), 139 }, 140 } 141 142 mr := &mockReceiver{} 143 144 ce := newConfigEventer(mr) 145 ce.ProcessConfigUpdate(mc) 146 mr.sequence = 0 147 mr.orgs = nil 148 ce.ProcessConfigUpdate(mc) 149 150 if mr.sequence != 0 { 151 t.Errorf("Should not have updated sequence when reprocessing same config") 152 } 153 154 if mr.orgs != nil { 155 t.Errorf("Should not have updated anchor peers when reprocessing same config") 156 } 157 } 158 159 func TestUpdatedSeqOnly(t *testing.T) { 160 mc := &mockConfig{ 161 sequence: 7, 162 orgs: map[string]config.ApplicationOrg{ 163 testOrgID: applicationOrgs([]*peer.AnchorPeer{ 164 &peer.AnchorPeer{ 165 Port: 9, 166 }, 167 }), 168 }, 169 } 170 171 mr := &mockReceiver{} 172 173 ce := newConfigEventer(mr) 174 ce.ProcessConfigUpdate(mc) 175 mc.sequence = 9 176 ce.ProcessConfigUpdate(mc) 177 178 if mr.sequence != 7 { 179 t.Errorf("Should not have updated sequence when reprocessing same config") 180 } 181 182 if !reflect.DeepEqual(mr.orgs, mc.orgs) { 183 t.Errorf("Should not have cleared anchor peers when reprocessing newer config with higher sequence") 184 } 185 }