Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Add a test for BandwidthCounter::Reset
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Nov 1, 2019
1 parent 7dbbdcc commit 91d2454
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions metrics/bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 91d2454

Please sign in to comment.