mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-27 18:34:39 +00:00
* Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package trust
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTrustMetricScores(t *testing.T) {
|
|
tm := NewMetric()
|
|
tm.Start()
|
|
|
|
// Perfect score
|
|
tm.GoodEvents(1)
|
|
score := tm.TrustScore()
|
|
assert.Equal(t, 100, score)
|
|
|
|
// Less than perfect score
|
|
tm.BadEvents(10)
|
|
score = tm.TrustScore()
|
|
assert.NotEqual(t, 100, score)
|
|
tm.Stop()
|
|
}
|
|
|
|
func TestTrustMetricConfig(t *testing.T) {
|
|
// 7 days
|
|
window := time.Minute * 60 * 24 * 7
|
|
config := MetricConfig{
|
|
TrackingWindow: window,
|
|
IntervalLength: 2 * time.Minute,
|
|
}
|
|
|
|
tm := NewMetricWithConfig(config)
|
|
tm.Start()
|
|
|
|
// The max time intervals should be the TrackingWindow / IntervalLen
|
|
assert.Equal(t, int(config.TrackingWindow/config.IntervalLength), tm.maxIntervals)
|
|
|
|
dc := DefaultConfig()
|
|
// These weights should still be the default values
|
|
assert.Equal(t, dc.ProportionalWeight, tm.proportionalWeight)
|
|
assert.Equal(t, dc.IntegralWeight, tm.integralWeight)
|
|
tm.Stop()
|
|
tm.Wait()
|
|
|
|
config.ProportionalWeight = 0.3
|
|
config.IntegralWeight = 0.7
|
|
tm = NewMetricWithConfig(config)
|
|
tm.Start()
|
|
|
|
// These weights should be equal to our custom values
|
|
assert.Equal(t, config.ProportionalWeight, tm.proportionalWeight)
|
|
assert.Equal(t, config.IntegralWeight, tm.integralWeight)
|
|
tm.Stop()
|
|
tm.Wait()
|
|
}
|
|
|
|
func TestTrustMetricCopyNilPointer(t *testing.T) {
|
|
var tm *Metric
|
|
|
|
ctm := tm.Copy()
|
|
|
|
assert.Nil(t, ctm)
|
|
}
|
|
|
|
// XXX: This test fails non-deterministically
|
|
//nolint:unused,deadcode
|
|
func _TestTrustMetricStopPause(t *testing.T) {
|
|
// The TestTicker will provide manual control over
|
|
// the passing of time within the metric
|
|
tt := NewTestTicker()
|
|
tm := NewMetric()
|
|
tm.SetTicker(tt)
|
|
tm.Start()
|
|
// Allow some time intervals to pass and pause
|
|
tt.NextTick()
|
|
tt.NextTick()
|
|
tm.Pause()
|
|
|
|
// could be 1 or 2 because Pause and NextTick race
|
|
first := tm.Copy().numIntervals
|
|
|
|
// Allow more time to pass and check the intervals are unchanged
|
|
tt.NextTick()
|
|
tt.NextTick()
|
|
assert.Equal(t, first, tm.Copy().numIntervals)
|
|
|
|
// Get the trust metric activated again
|
|
tm.GoodEvents(5)
|
|
// Allow some time intervals to pass and stop
|
|
tt.NextTick()
|
|
tt.NextTick()
|
|
tm.Stop()
|
|
tm.Wait()
|
|
|
|
second := tm.Copy().numIntervals
|
|
// Allow more intervals to pass while the metric is stopped
|
|
// and check that the number of intervals match
|
|
tm.NextTimeInterval()
|
|
tm.NextTimeInterval()
|
|
// XXX: fails non-deterministically:
|
|
// expected 5, got 6
|
|
assert.Equal(t, second+2, tm.Copy().numIntervals)
|
|
|
|
if first > second {
|
|
t.Fatalf("numIntervals should always increase or stay the same over time")
|
|
}
|
|
}
|