lint: errcheck (#5091)

## Description

add more error checks to tests


gonna do a third PR that tackles the non test cases
This commit is contained in:
Marko
2020-07-14 11:04:41 +00:00
committed by GitHub
parent 0825d57cf7
commit 6ccccb0933
32 changed files with 773 additions and 229 deletions
+17 -8
View File
@@ -5,11 +5,13 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTrustMetricScores(t *testing.T) {
tm := NewMetric()
tm.Start()
err := tm.Start()
require.NoError(t, err)
// Perfect score
tm.GoodEvents(1)
@@ -20,7 +22,8 @@ func TestTrustMetricScores(t *testing.T) {
tm.BadEvents(10)
score = tm.TrustScore()
assert.NotEqual(t, 100, score)
tm.Stop()
err = tm.Stop()
require.NoError(t, err)
}
func TestTrustMetricConfig(t *testing.T) {
@@ -32,7 +35,8 @@ func TestTrustMetricConfig(t *testing.T) {
}
tm := NewMetricWithConfig(config)
tm.Start()
err := tm.Start()
require.NoError(t, err)
// The max time intervals should be the TrackingWindow / IntervalLen
assert.Equal(t, int(config.TrackingWindow/config.IntervalLength), tm.maxIntervals)
@@ -41,18 +45,21 @@ func TestTrustMetricConfig(t *testing.T) {
// These weights should still be the default values
assert.Equal(t, dc.ProportionalWeight, tm.proportionalWeight)
assert.Equal(t, dc.IntegralWeight, tm.integralWeight)
tm.Stop()
err = tm.Stop()
require.NoError(t, err)
tm.Wait()
config.ProportionalWeight = 0.3
config.IntegralWeight = 0.7
tm = NewMetricWithConfig(config)
tm.Start()
err = tm.Start()
require.NoError(t, err)
// 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()
err = tm.Stop()
require.NoError(t, err)
tm.Wait()
}
@@ -72,7 +79,8 @@ func _TestTrustMetricStopPause(t *testing.T) {
tt := NewTestTicker()
tm := NewMetric()
tm.SetTicker(tt)
tm.Start()
err := tm.Start()
require.NoError(t, err)
// Allow some time intervals to pass and pause
tt.NextTick()
tt.NextTick()
@@ -91,7 +99,8 @@ func _TestTrustMetricStopPause(t *testing.T) {
// Allow some time intervals to pass and stop
tt.NextTick()
tt.NextTick()
tm.Stop()
err = tm.Stop()
require.NoError(t, err)
tm.Wait()
second := tm.Copy().numIntervals
+22 -11
View File
@@ -31,7 +31,8 @@ func TestTrustMetricStoreSaveLoad(t *testing.T) {
// Load the data from the file
store = NewTrustMetricStore(historyDB, DefaultConfig())
store.SetLogger(log.TestingLogger())
store.Start()
err = store.Start()
require.NoError(t, err)
// Make sure we still have 0 entries
assert.Zero(t, store.Size())
@@ -48,7 +49,8 @@ func TestTrustMetricStoreSaveLoad(t *testing.T) {
tm := NewMetric()
tm.SetTicker(tt[i])
tm.Start()
err = tm.Start()
require.NoError(t, err)
store.AddPeerTrustMetric(key, tm)
tm.BadEvents(10)
@@ -62,12 +64,14 @@ func TestTrustMetricStoreSaveLoad(t *testing.T) {
tt[i].NextTick()
}
// Stop all the trust metrics and save
store.Stop()
err = store.Stop()
require.NoError(t, err)
// Load the data from the DB
store = NewTrustMetricStore(historyDB, DefaultConfig())
store.SetLogger(log.TestingLogger())
store.Start()
err = store.Start()
require.NoError(t, err)
// Check that we still have 100 peers with imperfect trust values
assert.Equal(t, 100, store.Size())
@@ -75,7 +79,8 @@ func TestTrustMetricStoreSaveLoad(t *testing.T) {
assert.NotEqual(t, 1.0, tm.TrustValue())
}
store.Stop()
err = store.Stop()
require.NoError(t, err)
}
func TestTrustMetricStoreConfig(t *testing.T) {
@@ -90,7 +95,8 @@ func TestTrustMetricStoreConfig(t *testing.T) {
// Create a store with custom config
store := NewTrustMetricStore(historyDB, config)
store.SetLogger(log.TestingLogger())
store.Start()
err = store.Start()
require.NoError(t, err)
// Have the store make us a metric with the config
tm := store.GetPeerTrustMetric("TestKey")
@@ -98,7 +104,8 @@ func TestTrustMetricStoreConfig(t *testing.T) {
// Check that the options made it to the metric
assert.Equal(t, 0.5, tm.proportionalWeight)
assert.Equal(t, 0.5, tm.integralWeight)
store.Stop()
err = store.Stop()
require.NoError(t, err)
}
func TestTrustMetricStoreLookup(t *testing.T) {
@@ -107,7 +114,8 @@ func TestTrustMetricStoreLookup(t *testing.T) {
store := NewTrustMetricStore(historyDB, DefaultConfig())
store.SetLogger(log.TestingLogger())
store.Start()
err = store.Start()
require.NoError(t, err)
// Create 100 peers in the trust metric store
for i := 0; i < 100; i++ {
@@ -119,7 +127,8 @@ func TestTrustMetricStoreLookup(t *testing.T) {
assert.NotNil(t, ktm, "Expected to find TrustMetric %s but wasn't there.", key)
}
store.Stop()
err = store.Stop()
require.NoError(t, err)
}
func TestTrustMetricStorePeerScore(t *testing.T) {
@@ -128,7 +137,8 @@ func TestTrustMetricStorePeerScore(t *testing.T) {
store := NewTrustMetricStore(historyDB, DefaultConfig())
store.SetLogger(log.TestingLogger())
store.Start()
err = store.Start()
require.NoError(t, err)
key := "TestKey"
tm := store.GetPeerTrustMetric(key)
@@ -152,5 +162,6 @@ func TestTrustMetricStorePeerScore(t *testing.T) {
// We will remember our experiences with this peer
tm = store.GetPeerTrustMetric(key)
assert.NotEqual(t, 100, tm.TrustScore())
store.Stop()
err = store.Stop()
require.NoError(t, err)
}