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.

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