increase_test.go 2.0 KB

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