rate_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package aggregator
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestRateAggregator_Value(t *testing.T) {
  8. time1 := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
  9. time2 := time.Date(1, 1, 1, 0, 0, 1, 0, time.UTC)
  10. time3 := time.Date(1, 1, 1, 0, 0, 2, 0, time.UTC)
  11. type update struct {
  12. value float64
  13. timestamp time.Time
  14. additionalInformation map[string]string
  15. }
  16. tests := map[string]struct {
  17. updates []update
  18. want []MetricValue
  19. }{
  20. "no update": {
  21. updates: []update{},
  22. want: []MetricValue{},
  23. },
  24. "single update": {
  25. updates: []update{
  26. {
  27. value: 1,
  28. timestamp: time1,
  29. },
  30. },
  31. want: []MetricValue{
  32. {
  33. Value: 0,
  34. },
  35. },
  36. },
  37. "normal increase": {
  38. updates: []update{
  39. {
  40. value: 1,
  41. timestamp: time1,
  42. },
  43. {
  44. value: 2,
  45. timestamp: time2,
  46. },
  47. },
  48. want: []MetricValue{
  49. {
  50. Value: 1,
  51. },
  52. },
  53. },
  54. "multi increase": {
  55. updates: []update{
  56. {
  57. value: 1,
  58. timestamp: time1,
  59. },
  60. {
  61. value: 2,
  62. timestamp: time2,
  63. },
  64. {
  65. value: 4,
  66. timestamp: time3,
  67. },
  68. },
  69. want: []MetricValue{
  70. {
  71. Value: 1.5,
  72. },
  73. },
  74. },
  75. "aggregated increase": {
  76. updates: []update{
  77. {
  78. value: 1,
  79. timestamp: time1,
  80. },
  81. {
  82. value: 2,
  83. timestamp: time1,
  84. },
  85. {
  86. value: 3,
  87. timestamp: time2,
  88. },
  89. {
  90. value: 4,
  91. timestamp: time2,
  92. },
  93. },
  94. want: []MetricValue{
  95. {
  96. Value: 4,
  97. },
  98. },
  99. },
  100. }
  101. for name, tt := range tests {
  102. t.Run(name, func(t *testing.T) {
  103. agg := rateAggregator{}
  104. for _, u := range tt.updates {
  105. agg.Update(u.value, u.timestamp, u.additionalInformation)
  106. }
  107. got := agg.Value()
  108. if !reflect.DeepEqual(got, tt.want) {
  109. t.Errorf("got = %v, want %v", got, tt.want)
  110. }
  111. })
  112. }
  113. }