Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "testing"
  6. "time"
  7. )
  8. type mockTime struct {
  9. now time.Time
  10. sleepList []time.Duration
  11. lastCheckedSleep int
  12. }
  13. func (mt *mockTime) Now() (now time.Time) {
  14. return mt.now
  15. }
  16. func (mt *mockTime) Sleep(dur time.Duration) {
  17. mt.sleepList = append(mt.sleepList, dur)
  18. mt.pause(dur)
  19. }
  20. func (mt *mockTime) pause(dur time.Duration) {
  21. mt.now = mt.now.Add(dur)
  22. }
  23. func (mt *mockTime) lastSleep() (slept bool, duration time.Duration) {
  24. if mt.lastCheckedSleep == len(mt.sleepList)-1 {
  25. slept = false
  26. return
  27. }
  28. slept = true
  29. mt.lastCheckedSleep += 1
  30. duration = mt.sleepList[mt.lastCheckedSleep]
  31. return
  32. }
  33. func newFakelagForTesting(window time.Duration, burstLimit uint, throttleMessagesPerWindow uint, cooldown time.Duration) (*Fakelag, *mockTime) {
  34. fl := NewFakelag(window, burstLimit, throttleMessagesPerWindow, cooldown)
  35. mt := new(mockTime)
  36. mt.now, _ = time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
  37. mt.lastCheckedSleep = -1
  38. fl.nowFunc = mt.Now
  39. fl.sleepFunc = mt.Sleep
  40. return fl, mt
  41. }
  42. func TestFakelag(t *testing.T) {
  43. window, _ := time.ParseDuration("1s")
  44. fl, mt := newFakelagForTesting(window, 3, 2, window)
  45. fl.Touch()
  46. slept, _ := mt.lastSleep()
  47. if slept {
  48. t.Fatalf("should not have slept")
  49. }
  50. interval, _ := time.ParseDuration("100ms")
  51. for i := 0; i < 2; i++ {
  52. mt.pause(interval)
  53. fl.Touch()
  54. slept, _ := mt.lastSleep()
  55. if slept {
  56. t.Fatalf("should not have slept")
  57. }
  58. }
  59. mt.pause(interval)
  60. fl.Touch()
  61. if fl.state != FakelagThrottled {
  62. t.Fatalf("should be throttled")
  63. }
  64. slept, duration := mt.lastSleep()
  65. if !slept {
  66. t.Fatalf("should have slept due to fakelag")
  67. }
  68. expected, _ := time.ParseDuration("400ms")
  69. if duration != expected {
  70. t.Fatalf("incorrect sleep time: %v != %v", expected, duration)
  71. }
  72. fl.Touch()
  73. if fl.state != FakelagThrottled {
  74. t.Fatalf("should be throttled")
  75. }
  76. slept, duration = mt.lastSleep()
  77. if duration != interval {
  78. t.Fatalf("incorrect sleep time: %v != %v", interval, duration)
  79. }
  80. mt.pause(interval * 6)
  81. fl.Touch()
  82. if fl.state != FakelagThrottled {
  83. t.Fatalf("should still be throttled")
  84. }
  85. slept, duration = mt.lastSleep()
  86. if duration != 0 {
  87. t.Fatalf("we paused for long enough that we shouldn't sleep here")
  88. }
  89. mt.pause(window * 2)
  90. fl.Touch()
  91. if fl.state != FakelagBursting {
  92. t.Fatalf("should be bursting again")
  93. }
  94. slept, _ = mt.lastSleep()
  95. if slept {
  96. t.Fatalf("should not have slept")
  97. }
  98. }