github.com/number571/tendermint@v0.34.11-gost/internal/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 //nolint:unused,deadcode 76 func _TestTrustMetricStopPause(t *testing.T) { 77 // The TestTicker will provide manual control over 78 // the passing of time within the metric 79 tt := NewTestTicker() 80 tm := NewMetric() 81 tm.SetTicker(tt) 82 err := tm.Start() 83 require.NoError(t, err) 84 // Allow some time intervals to pass and pause 85 tt.NextTick() 86 tt.NextTick() 87 tm.Pause() 88 89 // could be 1 or 2 because Pause and NextTick race 90 first := tm.Copy().numIntervals 91 92 // Allow more time to pass and check the intervals are unchanged 93 tt.NextTick() 94 tt.NextTick() 95 assert.Equal(t, first, tm.Copy().numIntervals) 96 97 // Get the trust metric activated again 98 tm.GoodEvents(5) 99 // Allow some time intervals to pass and stop 100 tt.NextTick() 101 tt.NextTick() 102 err = tm.Stop() 103 require.NoError(t, err) 104 tm.Wait() 105 106 second := tm.Copy().numIntervals 107 // Allow more intervals to pass while the metric is stopped 108 // and check that the number of intervals match 109 tm.NextTimeInterval() 110 tm.NextTimeInterval() 111 // XXX: fails non-deterministically: 112 // expected 5, got 6 113 assert.Equal(t, second+2, tm.Copy().numIntervals) 114 115 if first > second { 116 t.Fatalf("numIntervals should always increase or stay the same over time") 117 } 118 }