throttle_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package flowcontrol
  14. import (
  15. "math"
  16. "sync"
  17. "testing"
  18. "time"
  19. )
  20. func TestBasicThrottle(t *testing.T) {
  21. r := NewTokenBucketRateLimiter(1, 3)
  22. for i := 0; i < 3; i++ {
  23. if !r.TryAccept() {
  24. t.Error("unexpected false accept")
  25. }
  26. }
  27. if r.TryAccept() {
  28. t.Error("unexpected true accept")
  29. }
  30. }
  31. func TestIncrementThrottle(t *testing.T) {
  32. r := NewTokenBucketRateLimiter(1, 1)
  33. if !r.TryAccept() {
  34. t.Error("unexpected false accept")
  35. }
  36. if r.TryAccept() {
  37. t.Error("unexpected true accept")
  38. }
  39. // Allow to refill
  40. time.Sleep(2 * time.Second)
  41. if !r.TryAccept() {
  42. t.Error("unexpected false accept")
  43. }
  44. }
  45. func TestThrottle(t *testing.T) {
  46. r := NewTokenBucketRateLimiter(10, 5)
  47. // Should consume 5 tokens immediately, then
  48. // the remaining 11 should take at least 1 second (0.1s each)
  49. expectedFinish := time.Now().Add(time.Second * 1)
  50. for i := 0; i < 16; i++ {
  51. r.Accept()
  52. }
  53. if time.Now().Before(expectedFinish) {
  54. t.Error("rate limit was not respected, finished too early")
  55. }
  56. }
  57. func TestRateLimiterSaturation(t *testing.T) {
  58. const e = 0.000001
  59. tests := []struct {
  60. capacity int
  61. take int
  62. expectedSaturation float64
  63. }{
  64. {1, 1, 1},
  65. {10, 3, 0.3},
  66. }
  67. for i, tt := range tests {
  68. rl := NewTokenBucketRateLimiter(1, tt.capacity)
  69. for i := 0; i < tt.take; i++ {
  70. rl.Accept()
  71. }
  72. if math.Abs(rl.Saturation()-tt.expectedSaturation) > e {
  73. t.Fatalf("#%d: Saturation rate difference isn't within tolerable range\n want=%f, get=%f",
  74. i, tt.expectedSaturation, rl.Saturation())
  75. }
  76. }
  77. }
  78. func TestAlwaysFake(t *testing.T) {
  79. rl := NewFakeAlwaysRateLimiter()
  80. if !rl.TryAccept() {
  81. t.Error("TryAccept in AlwaysFake should return true.")
  82. }
  83. // If this will block the test will timeout
  84. rl.Accept()
  85. }
  86. func TestNeverFake(t *testing.T) {
  87. rl := NewFakeNeverRateLimiter()
  88. if rl.TryAccept() {
  89. t.Error("TryAccept in NeverFake should return false.")
  90. }
  91. finished := false
  92. wg := sync.WaitGroup{}
  93. wg.Add(1)
  94. go func() {
  95. rl.Accept()
  96. finished = true
  97. wg.Done()
  98. }()
  99. // Wait some time to make sure it never finished.
  100. time.Sleep(time.Second)
  101. if finished {
  102. t.Error("Accept should block forever in NeverFake.")
  103. }
  104. rl.Stop()
  105. wg.Wait()
  106. if !finished {
  107. t.Error("Stop should make Accept unblock in NeverFake.")
  108. }
  109. }