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 2.9KB

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