abovethresholdratio_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package aggregator
  2. import (
  3. "math"
  4. "reflect"
  5. "testing"
  6. "time"
  7. )
  8. func TestAboveThresholdRatioAggregator_Value(t *testing.T) {
  9. time1 := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
  10. time2 := time.Date(1, 1, 1, 0, 1, 0, 0, time.UTC)
  11. time3 := time.Date(1, 1, 1, 0, 2, 0, 0, time.UTC)
  12. time4 := time.Date(1, 1, 1, 0, 3, 0, 0, time.UTC)
  13. type update struct {
  14. value float64
  15. timestamp time.Time
  16. }
  17. tests := map[string]struct {
  18. threshold float64
  19. updates []update
  20. want []MetricValue
  21. }{
  22. "no update": {
  23. threshold: 0.9,
  24. updates: []update{},
  25. want: []MetricValue{{Value: 0}},
  26. },
  27. "all above": {
  28. threshold: 0.9,
  29. updates: []update{
  30. {value: 0.95, timestamp: time1},
  31. {value: 0.99, timestamp: time2},
  32. },
  33. want: []MetricValue{{Value: 1}},
  34. },
  35. "threshold is inclusive": {
  36. threshold: 0.9,
  37. updates: []update{{value: 0.9, timestamp: time1}},
  38. want: []MetricValue{{Value: 1}},
  39. },
  40. "quarter above": {
  41. threshold: 0.9,
  42. updates: []update{
  43. {value: 0.95, timestamp: time1},
  44. {value: 0.5, timestamp: time2},
  45. {value: 0.89, timestamp: time3},
  46. {value: 0.1, timestamp: time4},
  47. },
  48. want: []MetricValue{{Value: 0.25}},
  49. },
  50. "duplicate timestamp counts once": {
  51. threshold: 0.9,
  52. updates: []update{
  53. {value: 0.95, timestamp: time1},
  54. {value: 0.1, timestamp: time1},
  55. {value: 0.1, timestamp: time2},
  56. },
  57. want: []MetricValue{{Value: 0.5}},
  58. },
  59. "NaN counts as below": {
  60. threshold: 0.9,
  61. updates: []update{
  62. {value: math.NaN(), timestamp: time1},
  63. {value: 0.95, timestamp: time2},
  64. },
  65. want: []MetricValue{{Value: 0.5}},
  66. },
  67. }
  68. for name, tt := range tests {
  69. t.Run(name, func(t *testing.T) {
  70. agg := AboveThresholdRatio(tt.threshold)([]string{})
  71. for _, u := range tt.updates {
  72. agg.Update(u.value, u.timestamp, nil)
  73. }
  74. if got := agg.Value(); !reflect.DeepEqual(got, tt.want) {
  75. t.Errorf("Value() = %v, want %v", got, tt.want)
  76. }
  77. })
  78. }
  79. }