github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/gossip/service/eventer_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package service 8 9 import ( 10 "testing" 11 12 "github.com/hechain20/hechain/common/channelconfig" 13 "github.com/hechain20/hechain/gossip/util" 14 "github.com/hyperledger/fabric-protos-go/peer" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func init() { 19 util.SetupTestLogging() 20 } 21 22 type mockReceiver struct { 23 cu ConfigUpdate 24 updateCount int 25 } 26 27 func (mr *mockReceiver) updateAnchors(cu ConfigUpdate) { 28 logger.Debugf("[TEST] Setting config to %d %v", cu.Sequence, cu.Organizations) 29 mr.updateCount++ 30 mr.cu = cu 31 } 32 33 func TestInitialUpdate(t *testing.T) { 34 cu := ConfigUpdate{ 35 ChannelID: "channel-id", 36 OrdererAddresses: []string{"localhost:7050"}, 37 Sequence: 7, 38 Organizations: map[string]channelconfig.ApplicationOrg{ 39 "testID": &appGrp{ 40 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 41 }, 42 }, 43 } 44 45 mr := &mockReceiver{} 46 ce := newConfigEventer(mr) 47 ce.ProcessConfigUpdate(cu) 48 49 require.Equal(t, cu, mr.cu, "should have updated config on initial update but did not") 50 } 51 52 func TestSecondUpdate(t *testing.T) { 53 cu := ConfigUpdate{ 54 ChannelID: "channel-id", 55 OrdererAddresses: []string{"localhost:7050"}, 56 Sequence: 7, 57 Organizations: map[string]channelconfig.ApplicationOrg{ 58 "testID": &appGrp{ 59 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 60 }, 61 }, 62 } 63 64 mr := &mockReceiver{} 65 ce := newConfigEventer(mr) 66 67 ce.ProcessConfigUpdate(cu) 68 require.Equal(t, cu, mr.cu, "should have updated config on initial update but did not") 69 require.Equal(t, 1, mr.updateCount) 70 71 cu.Sequence = 8 72 cu.Organizations["testID"] = &appGrp{ 73 anchorPeers: []*peer.AnchorPeer{{Port: 10}}, 74 } 75 76 ce.ProcessConfigUpdate(cu) 77 require.Equal(t, cu, mr.cu, "should have updated config on second update but did not") 78 require.Equal(t, 2, mr.updateCount) 79 } 80 81 func TestSecondSameUpdate(t *testing.T) { 82 cu := ConfigUpdate{ 83 ChannelID: "channel-id", 84 OrdererAddresses: []string{"localhost:7050"}, 85 Sequence: 7, 86 Organizations: map[string]channelconfig.ApplicationOrg{ 87 "testID": &appGrp{ 88 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 89 }, 90 }, 91 } 92 93 mr := &mockReceiver{} 94 ce := newConfigEventer(mr) 95 96 ce.ProcessConfigUpdate(cu) 97 require.Equal(t, cu, mr.cu, "should have updated config on initial update but did not") 98 require.Equal(t, 1, mr.updateCount) 99 100 ce.ProcessConfigUpdate( 101 ConfigUpdate{Organizations: cu.Organizations}, 102 ) 103 require.Equal(t, cu, mr.cu, "should not have updated when reprocessing the same config") 104 require.Equal(t, 1, mr.updateCount) 105 } 106 107 func TestUpdatedSeqOnly(t *testing.T) { 108 cu := ConfigUpdate{ 109 ChannelID: "channel-id", 110 OrdererAddresses: []string{"localhost:7050"}, 111 Sequence: 7, 112 Organizations: map[string]channelconfig.ApplicationOrg{ 113 "testID": &appGrp{ 114 anchorPeers: []*peer.AnchorPeer{{Port: 9}}, 115 }, 116 }, 117 } 118 119 mr := &mockReceiver{} 120 ce := newConfigEventer(mr) 121 122 ce.ProcessConfigUpdate(cu) 123 require.Equal(t, cu, mr.cu, "should have updated config on initial update but did not") 124 require.Equal(t, 1, mr.updateCount) 125 126 cu2 := cu 127 cu2.Sequence = 9 128 ce.ProcessConfigUpdate(cu2) 129 require.Equal(t, cu, mr.cu, "should not have updated config when reprocessing same config") 130 require.Equal(t, 1, mr.updateCount) 131 }