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