From 91d2454e8c6337e80f421cf73513a8050c54fb13 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Fri, 1 Nov 2019 04:46:48 +0300 Subject: [PATCH] Add a test for BandwidthCounter::Reset --- metrics/bandwidth_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/metrics/bandwidth_test.go b/metrics/bandwidth_test.go index de7b0fdc..49309b07 100644 --- a/metrics/bandwidth_test.go +++ b/metrics/bandwidth_test.go @@ -143,6 +143,63 @@ func TestBandwidthCounter(t *testing.T) { } } +func TestResetBandwidthCounter(t *testing.T) { + bwc := NewBandwidthCounter() + + p := peer.ID("peer-0") + proto := protocol.ID("proto-0") + + bwc.LogSentMessage(42) + bwc.LogRecvMessage(24) + bwc.LogSentMessageStream(100, proto, p) + bwc.LogRecvMessageStream(50, proto, p) + + time.Sleep(1 * time.Second) + + { + stats := bwc.GetBandwidthTotals() + assertEq(t, 42, stats.TotalOut) + assertEq(t, 24, stats.TotalIn) + } + + { + stats := bwc.GetBandwidthByProtocol() + assertApproxEq(t, 1, float64(len(stats))) + stat := stats[proto] + assertApproxEq(t, 100, stat.RateOut) + assertApproxEq(t, 50, stat.RateIn) + } + + { + stats := bwc.GetBandwidthByPeer() + assertApproxEq(t, 1, float64(len(stats))) + stat := stats[p] + assertApproxEq(t, 100, stat.RateOut) + assertApproxEq(t, 50, stat.RateIn) + } + + bwc.Reset() + { + stats := bwc.GetBandwidthTotals() + assertEq(t, 0, stats.TotalOut) + assertEq(t, 0, stats.TotalIn) + } + + { + byProtocol := bwc.GetBandwidthByProtocol() + if len(byProtocol) != 0 { + t.Errorf("expected 0 protocols, got %d", len(byProtocol)) + } + } + + { + byPeer := bwc.GetBandwidthByPeer() + if len(byPeer) != 0 { + t.Errorf("expected 0 peers, got %d", len(byPeer)) + } + } +} + func assertEq(t *testing.T, expected, actual int64) { if expected != actual { t.Errorf("expected %d, got %d", expected, actual)