github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/gossip/state/metrics_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package state 8 9 import ( 10 "sync" 11 "testing" 12 13 proto "github.com/hyperledger/fabric-protos-go/gossip" 14 "github.com/osdi23p228/fabric/gossip/discovery" 15 "github.com/osdi23p228/fabric/gossip/metrics" 16 gmetricsmocks "github.com/osdi23p228/fabric/gossip/metrics/mocks" 17 "github.com/osdi23p228/fabric/gossip/protoext" 18 "github.com/osdi23p228/fabric/gossip/state/mocks" 19 "github.com/osdi23p228/fabric/protoutil" 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/mock" 22 ) 23 24 func TestMetrics(t *testing.T) { 25 mc := &mockCommitter{Mock: &mock.Mock{}} 26 mc.On("CommitLegacy", mock.Anything).Return(nil) 27 mc.On("LedgerHeight", mock.Anything).Return(uint64(100), nil).Twice() 28 mc.On("DoesPvtDataInfoExistInLedger", mock.Anything).Return(false, nil) 29 g := &mocks.GossipMock{} 30 g.On("PeersOfChannel", mock.Anything).Return([]discovery.NetworkMember{}) 31 g.On("Accept", mock.Anything, false).Return(make(<-chan *proto.GossipMessage), nil) 32 g.On("Accept", mock.Anything, true).Return(nil, make(chan protoext.ReceivedMessage)) 33 34 heightWG := sync.WaitGroup{} 35 heightWG.Add(1) 36 committedDurationWG := sync.WaitGroup{} 37 committedDurationWG.Add(1) 38 39 testMetricProvider := gmetricsmocks.TestUtilConstructMetricProvider() 40 41 testMetricProvider.FakeHeightGauge.SetStub = func(delta float64) { 42 heightWG.Done() 43 } 44 testMetricProvider.FakeCommitDurationHist.ObserveStub = func(value float64) { 45 committedDurationWG.Done() 46 } 47 48 gossipMetrics := metrics.NewGossipMetrics(testMetricProvider.FakeProvider) 49 50 // create peer with fake metrics provider for gossip state 51 p := newPeerNodeWithGossipWithMetrics(0, mc, noopPeerIdentityAcceptor, g, gossipMetrics) 52 defer p.shutdown() 53 54 // add a payload to the payload buffer 55 err := p.s.AddPayload(&proto.Payload{ 56 SeqNum: 100, 57 Data: protoutil.MarshalOrPanic(protoutil.NewBlock(100, []byte{})), 58 }) 59 assert.NoError(t, err) 60 61 // update the ledger height to prepare for the pop operation 62 mc.On("LedgerHeight", mock.Anything).Return(uint64(101), nil) 63 64 // ensure the block commit event was sent, and the update event was sent 65 heightWG.Wait() 66 committedDurationWG.Wait() 67 68 // ensure the right height was reported 69 assert.Equal(t, 70 []string{"channel", "testchannelid"}, 71 testMetricProvider.FakeHeightGauge.WithArgsForCall(0), 72 ) 73 assert.EqualValues(t, 74 101, 75 testMetricProvider.FakeHeightGauge.SetArgsForCall(0), 76 ) 77 78 // after push or pop payload buffer size should be reported 79 assert.Equal(t, 80 []string{"channel", "testchannelid"}, 81 testMetricProvider.FakePayloadBufferSizeGauge.WithArgsForCall(0), 82 ) 83 assert.Equal(t, 84 []string{"channel", "testchannelid"}, 85 testMetricProvider.FakePayloadBufferSizeGauge.WithArgsForCall(1), 86 ) 87 // both 0 and 1 as size can be reported, depends on timing 88 size := testMetricProvider.FakePayloadBufferSizeGauge.SetArgsForCall(0) 89 assert.True(t, size == 1 || size == 0) 90 size = testMetricProvider.FakePayloadBufferSizeGauge.SetArgsForCall(1) 91 assert.True(t, size == 1 || size == 0) 92 93 }