suppressor_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestSuppressor(t *testing.T) {
  7. s := suppressor{threshold: 10000}
  8. t0 := time.Now()
  9. t1 := t0
  10. sup, prev := s.suppress("foo", 10000, t1)
  11. if sup {
  12. t.Fatal("Never suppress first change")
  13. }
  14. if prev {
  15. t.Fatal("Incorrect prev status")
  16. }
  17. // bw is 10000 / 10 = 1000
  18. t1 = t0.Add(10 * time.Second)
  19. if bw := s.changes["foo"].bandwidth(t1); bw != 1000 {
  20. t.Error("Incorrect bw %d", bw)
  21. }
  22. sup, prev = s.suppress("foo", 10000, t1)
  23. if sup {
  24. t.Fatal("Should still be fine")
  25. }
  26. if prev {
  27. t.Fatal("Incorrect prev status")
  28. }
  29. // bw is (10000 + 10000) / 11 = 1818
  30. t1 = t0.Add(11 * time.Second)
  31. if bw := s.changes["foo"].bandwidth(t1); bw != 1818 {
  32. t.Error("Incorrect bw %d", bw)
  33. }
  34. sup, prev = s.suppress("foo", 100500, t1)
  35. if sup {
  36. t.Fatal("Should still be fine")
  37. }
  38. if prev {
  39. t.Fatal("Incorrect prev status")
  40. }
  41. // bw is (10000 + 10000 + 100500) / 12 = 10041
  42. t1 = t0.Add(12 * time.Second)
  43. if bw := s.changes["foo"].bandwidth(t1); bw != 10041 {
  44. t.Error("Incorrect bw %d", bw)
  45. }
  46. sup, prev = s.suppress("foo", 10000000, t1) // value will be ignored
  47. if !sup {
  48. t.Fatal("Should be over threshold")
  49. }
  50. if prev {
  51. t.Fatal("Incorrect prev status")
  52. }
  53. // bw is (10000 + 10000 + 100500) / 15 = 8033
  54. t1 = t0.Add(15 * time.Second)
  55. if bw := s.changes["foo"].bandwidth(t1); bw != 8033 {
  56. t.Error("Incorrect bw %d", bw)
  57. }
  58. sup, prev = s.suppress("foo", 10000000, t1)
  59. if sup {
  60. t.Fatal("Should be Ok")
  61. }
  62. if !prev {
  63. t.Fatal("Incorrect prev status")
  64. }
  65. }
  66. func TestHistory(t *testing.T) {
  67. h := changeHistory{}
  68. t0 := time.Now()
  69. h.append(40, t0)
  70. if l := len(h.changes); l != 1 {
  71. t.Errorf("Incorrect history length %d", l)
  72. }
  73. if s := h.changes[0].size; s != 40 {
  74. t.Errorf("Incorrect first record size %d", s)
  75. }
  76. for i := 1; i < MAX_CHANGE_HISTORY; i++ {
  77. h.append(int64(40+i), t0.Add(time.Duration(i)*time.Second))
  78. }
  79. if l := len(h.changes); l != MAX_CHANGE_HISTORY {
  80. t.Errorf("Incorrect history length %d", l)
  81. }
  82. if s := h.changes[0].size; s != 40 {
  83. t.Errorf("Incorrect first record size %d", s)
  84. }
  85. if s := h.changes[MAX_CHANGE_HISTORY-1].size; s != 40+MAX_CHANGE_HISTORY-1 {
  86. t.Errorf("Incorrect last record size %d", s)
  87. }
  88. h.append(999, t0.Add(time.Duration(999)*time.Second))
  89. if l := len(h.changes); l != MAX_CHANGE_HISTORY {
  90. t.Errorf("Incorrect history length %d", l)
  91. }
  92. if s := h.changes[0].size; s != 41 {
  93. t.Errorf("Incorrect first record size %d", s)
  94. }
  95. if s := h.changes[MAX_CHANGE_HISTORY-1].size; s != 999 {
  96. t.Errorf("Incorrect last record size %d", s)
  97. }
  98. }