suppressor_test.go 2.7 KB

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