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