github.com/vipernet-xyz/tm@v0.34.24/p2p/trust/metric_test.go (about) 1 package trust 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestTrustMetricScores(t *testing.T) { 12 tm := NewMetric() 13 err := tm.Start() 14 require.NoError(t, err) 15 16 // Perfect score 17 tm.GoodEvents(1) 18 score := tm.TrustScore() 19 assert.Equal(t, 100, score) 20 21 // Less than perfect score 22 tm.BadEvents(10) 23 score = tm.TrustScore() 24 assert.NotEqual(t, 100, score) 25 err = tm.Stop() 26 require.NoError(t, err) 27 } 28 29 func TestTrustMetricConfig(t *testing.T) { 30 // 7 days 31 window := time.Minute * 60 * 24 * 7 32 config := MetricConfig{ 33 TrackingWindow: window, 34 IntervalLength: 2 * time.Minute, 35 } 36 37 tm := NewMetricWithConfig(config) 38 err := tm.Start() 39 require.NoError(t, err) 40 41 // The max time intervals should be the TrackingWindow / IntervalLen 42 assert.Equal(t, int(config.TrackingWindow/config.IntervalLength), tm.maxIntervals) 43 44 dc := DefaultConfig() 45 // These weights should still be the default values 46 assert.Equal(t, dc.ProportionalWeight, tm.proportionalWeight) 47 assert.Equal(t, dc.IntegralWeight, tm.integralWeight) 48 err = tm.Stop() 49 require.NoError(t, err) 50 tm.Wait() 51 52 config.ProportionalWeight = 0.3 53 config.IntegralWeight = 0.7 54 tm = NewMetricWithConfig(config) 55 err = tm.Start() 56 require.NoError(t, err) 57 58 // These weights should be equal to our custom values 59 assert.Equal(t, config.ProportionalWeight, tm.proportionalWeight) 60 assert.Equal(t, config.IntegralWeight, tm.integralWeight) 61 err = tm.Stop() 62 require.NoError(t, err) 63 tm.Wait() 64 } 65 66 func TestTrustMetricCopyNilPointer(t *testing.T) { 67 var tm *Metric 68 69 ctm := tm.Copy() 70 71 assert.Nil(t, ctm) 72 } 73 74 // XXX: This test fails non-deterministically 75 // 76 //nolint:unused,deadcode 77 func _TestTrustMetricStopPause(t *testing.T) { 78 // The TestTicker will provide manual control over 79 // the passing of time within the metric 80 tt := NewTestTicker() 81 tm := NewMetric() 82 tm.SetTicker(tt) 83 err := tm.Start() 84 require.NoError(t, err) 85 // Allow some time intervals to pass and pause 86 tt.NextTick() 87 tt.NextTick() 88 tm.Pause() 89 90 // could be 1 or 2 because Pause and NextTick race 91 first := tm.Copy().numIntervals 92 93 // Allow more time to pass and check the intervals are unchanged 94 tt.NextTick() 95 tt.NextTick() 96 assert.Equal(t, first, tm.Copy().numIntervals) 97 98 // Get the trust metric activated again 99 tm.GoodEvents(5) 100 // Allow some time intervals to pass and stop 101 tt.NextTick() 102 tt.NextTick() 103 err = tm.Stop() 104 require.NoError(t, err) 105 tm.Wait() 106 107 second := tm.Copy().numIntervals 108 // Allow more intervals to pass while the metric is stopped 109 // and check that the number of intervals match 110 tm.NextTimeInterval() 111 tm.NextTimeInterval() 112 // XXX: fails non-deterministically: 113 // expected 5, got 6 114 assert.Equal(t, second+2, tm.Copy().numIntervals) 115 116 if first > second { 117 t.Fatalf("numIntervals should always increase or stay the same over time") 118 } 119 }