You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

throttler_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2018 Shivaram Lingamneni
  2. // released under the MIT license
  3. package connection_limits
  4. import (
  5. "reflect"
  6. "testing"
  7. "time"
  8. )
  9. func assertEqual(supplied, expected interface{}, t *testing.T) {
  10. if !reflect.DeepEqual(supplied, expected) {
  11. t.Errorf("expected %v but got %v", expected, supplied)
  12. }
  13. }
  14. func TestGenericThrottle(t *testing.T) {
  15. minute, _ := time.ParseDuration("1m")
  16. second, _ := time.ParseDuration("1s")
  17. zero, _ := time.ParseDuration("0s")
  18. throttler := GenericThrottle{
  19. Duration: minute,
  20. Limit: 2,
  21. }
  22. now := time.Now()
  23. throttled, remaining := throttler.touch(now)
  24. assertEqual(throttled, false, t)
  25. assertEqual(remaining, zero, t)
  26. now = now.Add(second)
  27. throttled, remaining = throttler.touch(now)
  28. assertEqual(throttled, false, t)
  29. assertEqual(remaining, zero, t)
  30. now = now.Add(second)
  31. throttled, remaining = throttler.touch(now)
  32. assertEqual(throttled, true, t)
  33. assertEqual(remaining, 58*second, t)
  34. now = now.Add(minute)
  35. throttled, remaining = throttler.touch(now)
  36. assertEqual(throttled, false, t)
  37. assertEqual(remaining, zero, t)
  38. }
  39. func TestGenericThrottleDisabled(t *testing.T) {
  40. minute, _ := time.ParseDuration("1m")
  41. throttler := GenericThrottle{
  42. Duration: minute,
  43. Limit: 0,
  44. }
  45. for i := 0; i < 1024; i += 1 {
  46. throttled, _ := throttler.Touch()
  47. if throttled {
  48. t.Error("disabled throttler should not throttle")
  49. }
  50. }
  51. }
  52. func makeTestThrottler(v4len, v6len int) *Limiter {
  53. minute, _ := time.ParseDuration("1m")
  54. maxConnections := 3
  55. config := LimiterConfig{
  56. rawLimiterConfig: rawLimiterConfig{
  57. Count: false,
  58. Throttle: true,
  59. CidrLenIPv4: v4len,
  60. CidrLenIPv6: v6len,
  61. MaxPerWindow: maxConnections,
  62. Window: minute,
  63. },
  64. }
  65. config.postprocess()
  66. var limiter Limiter
  67. limiter.ApplyConfig(&config)
  68. return &limiter
  69. }
  70. func TestConnectionThrottle(t *testing.T) {
  71. throttler := makeTestThrottler(32, 64)
  72. addr := easyParseIP("8.8.8.8")
  73. for i := 0; i < 3; i += 1 {
  74. err := throttler.AddClient(addr)
  75. assertEqual(err, nil, t)
  76. }
  77. err := throttler.AddClient(addr)
  78. assertEqual(err, ErrThrottleExceeded, t)
  79. }
  80. func TestConnectionThrottleIPv6(t *testing.T) {
  81. throttler := makeTestThrottler(32, 64)
  82. var err error
  83. err = throttler.AddClient(easyParseIP("2001:0db8::1"))
  84. assertEqual(err, nil, t)
  85. err = throttler.AddClient(easyParseIP("2001:0db8::2"))
  86. assertEqual(err, nil, t)
  87. err = throttler.AddClient(easyParseIP("2001:0db8::3"))
  88. assertEqual(err, nil, t)
  89. err = throttler.AddClient(easyParseIP("2001:0db8::4"))
  90. assertEqual(err, ErrThrottleExceeded, t)
  91. }
  92. func TestConnectionThrottleIPv4(t *testing.T) {
  93. throttler := makeTestThrottler(24, 64)
  94. var err error
  95. err = throttler.AddClient(easyParseIP("192.168.1.101"))
  96. assertEqual(err, nil, t)
  97. err = throttler.AddClient(easyParseIP("192.168.1.102"))
  98. assertEqual(err, nil, t)
  99. err = throttler.AddClient(easyParseIP("192.168.1.103"))
  100. assertEqual(err, nil, t)
  101. err = throttler.AddClient(easyParseIP("192.168.1.104"))
  102. assertEqual(err, ErrThrottleExceeded, t)
  103. }