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.

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