uptime_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package aggregator
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestActiveMinutesAggregator_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: 1,
  34. Timestamp: &time1,
  35. },
  36. },
  37. },
  38. "two sequential updates": {
  39. updates: []update{
  40. {
  41. value: 2,
  42. timestamp: time1,
  43. },
  44. {
  45. value: 1,
  46. timestamp: time2,
  47. },
  48. },
  49. want: []MetricValue{
  50. {
  51. Value: 1,
  52. Timestamp: &time1,
  53. },
  54. {
  55. Value: 1,
  56. Timestamp: &time2,
  57. },
  58. },
  59. },
  60. "multi update on single time": {
  61. updates: []update{
  62. {
  63. value: 1,
  64. timestamp: time1,
  65. },
  66. {
  67. value: 2,
  68. timestamp: time1,
  69. },
  70. },
  71. want: []MetricValue{
  72. {
  73. Value: 1,
  74. Timestamp: &time1,
  75. },
  76. },
  77. },
  78. "three sequential updates": {
  79. updates: []update{
  80. {
  81. value: 1,
  82. timestamp: time1,
  83. },
  84. {
  85. value: 1,
  86. timestamp: time2,
  87. },
  88. {
  89. value: 1,
  90. timestamp: time3,
  91. },
  92. },
  93. want: []MetricValue{
  94. {
  95. Value: 1,
  96. Timestamp: &time1,
  97. },
  98. {
  99. Value: 1,
  100. Timestamp: &time3,
  101. },
  102. },
  103. },
  104. }
  105. for name, tt := range tests {
  106. t.Run(name, func(t *testing.T) {
  107. agg := uptimeAggregator{}
  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. }