rate_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Value: 0,
  25. },
  26. },
  27. },
  28. "single update": {
  29. updates: []update{
  30. {
  31. value: 1,
  32. timestamp: time1,
  33. },
  34. },
  35. want: []MetricValue{
  36. {
  37. Value: 0,
  38. },
  39. },
  40. },
  41. "normal increase": {
  42. updates: []update{
  43. {
  44. value: 1,
  45. timestamp: time1,
  46. },
  47. {
  48. value: 2,
  49. timestamp: time2,
  50. },
  51. },
  52. want: []MetricValue{
  53. {
  54. Value: 1,
  55. },
  56. },
  57. },
  58. "multi increase": {
  59. updates: []update{
  60. {
  61. value: 1,
  62. timestamp: time1,
  63. },
  64. {
  65. value: 2,
  66. timestamp: time2,
  67. },
  68. {
  69. value: 4,
  70. timestamp: time3,
  71. },
  72. },
  73. want: []MetricValue{
  74. {
  75. Value: 1.5,
  76. },
  77. },
  78. },
  79. "aggregated increase": {
  80. updates: []update{
  81. {
  82. value: 1,
  83. timestamp: time1,
  84. },
  85. {
  86. value: 2,
  87. timestamp: time1,
  88. },
  89. {
  90. value: 3,
  91. timestamp: time2,
  92. },
  93. {
  94. value: 4,
  95. timestamp: time2,
  96. },
  97. },
  98. want: []MetricValue{
  99. {
  100. Value: 4,
  101. },
  102. },
  103. },
  104. }
  105. for name, tt := range tests {
  106. t.Run(name, func(t *testing.T) {
  107. agg := rateAggregator{}
  108. for _, u := range tt.updates {
  109. agg.Update(u.value, u.timestamp, u.additionalInformation)
  110. }
  111. got := agg.Value()
  112. if !reflect.DeepEqual(got, tt.want) {
  113. t.Errorf("got = %v, want %v", got, tt.want)
  114. }
  115. })
  116. }
  117. }