github.com/aergoio/aergo@v1.3.1/p2p/metric/peermetric_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package metric 7 8 import ( 9 "github.com/stretchr/testify/assert" 10 "testing" 11 "time" 12 13 "github.com/aergoio/aergo/types" 14 ) 15 16 func TestPeerMetric_OnRead(t *testing.T) { 17 pid, _ := types.IDB58Decode("16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm") 18 19 tests := []struct { 20 name string 21 22 // inSize should be small or equal to out 23 outSize int 24 inSize int 25 26 remove bool 27 }{ 28 {"Tzero", 0,0, false}, 29 {"TzeroRM", 0,0, true}, 30 {"TSameInOut", 999,999, false}, 31 {"TSameInOutRM", 999,999, true}, 32 {"TLeave", 2999,999, false}, 33 {"TLeaveRM", 2999,999, true}, 34 // TODO: test cases 35 } 36 for _, test := range tests { 37 t.Run(test.name, func(t *testing.T) { 38 mm := NewMetricManager(1) 39 target := PeerMetric{mm: mm, PeerID: pid, seq: 1, InMetric:NewExponentMetric5(mm.interval), OutMetric:NewExponentMetric5(mm.interval), Since:time.Now()} 40 41 if test.outSize > 0 { 42 target.OnRead(0, test.inSize) 43 target.InMetric.Calculate() 44 assert.Equal(t, int64(test.inSize), target.TotalIn()) 45 assert.True(t, target.InMetric.LoadScore() > 0 ) 46 assert.True(t, target.InMetric.LoadScore() <= target.TotalIn()) 47 } 48 }) 49 } 50 }