iratemax_test.go 2.2 KB

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