You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.8 KiB

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