limiter_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package limiter
  4. import (
  5. "bytes"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/google/go-cmp/cmp"
  10. )
  11. const testRefillInterval = time.Second
  12. func TestLimiter(t *testing.T) {
  13. // 1qps, burst of 10, 2 keys tracked
  14. l := &Limiter[string]{
  15. Size: 2,
  16. Max: 10,
  17. RefillInterval: testRefillInterval,
  18. }
  19. // Consume entire burst
  20. now := time.Now().Truncate(testRefillInterval)
  21. allowed(t, l, "foo", 10, now)
  22. denied(t, l, "foo", 1, now)
  23. hasTokens(t, l, "foo", 0)
  24. allowed(t, l, "bar", 10, now)
  25. denied(t, l, "bar", 1, now)
  26. hasTokens(t, l, "bar", 0)
  27. // Refill 1 token for both foo and bar
  28. now = now.Add(time.Second + time.Millisecond)
  29. allowed(t, l, "foo", 1, now)
  30. denied(t, l, "foo", 1, now)
  31. hasTokens(t, l, "foo", 0)
  32. allowed(t, l, "bar", 1, now)
  33. denied(t, l, "bar", 1, now)
  34. hasTokens(t, l, "bar", 0)
  35. // Refill 2 tokens for foo and bar
  36. now = now.Add(2*time.Second + time.Millisecond)
  37. allowed(t, l, "foo", 2, now)
  38. denied(t, l, "foo", 1, now)
  39. hasTokens(t, l, "foo", 0)
  40. allowed(t, l, "bar", 2, now)
  41. denied(t, l, "bar", 1, now)
  42. hasTokens(t, l, "bar", 0)
  43. // qux can burst 10, evicts foo so it can immediately burst 10 again too
  44. allowed(t, l, "qux", 10, now)
  45. denied(t, l, "qux", 1, now)
  46. notInLimiter(t, l, "foo")
  47. denied(t, l, "bar", 1, now) // refresh bar so foo lookup doesn't evict it - still throttled
  48. allowed(t, l, "foo", 10, now)
  49. denied(t, l, "foo", 1, now)
  50. hasTokens(t, l, "foo", 0)
  51. }
  52. func TestLimiterOverdraft(t *testing.T) {
  53. // 1qps, burst of 10, overdraft of 2, 2 keys tracked
  54. l := &Limiter[string]{
  55. Size: 2,
  56. Max: 10,
  57. Overdraft: 2,
  58. RefillInterval: testRefillInterval,
  59. }
  60. // Consume entire burst, go 1 into debt
  61. now := time.Now().Truncate(testRefillInterval).Add(time.Millisecond)
  62. allowed(t, l, "foo", 10, now)
  63. denied(t, l, "foo", 1, now)
  64. hasTokens(t, l, "foo", -1)
  65. allowed(t, l, "bar", 10, now)
  66. denied(t, l, "bar", 1, now)
  67. hasTokens(t, l, "bar", -1)
  68. // Refill 1 token for both foo and bar.
  69. // Still denied, still in debt.
  70. now = now.Add(time.Second)
  71. denied(t, l, "foo", 1, now)
  72. hasTokens(t, l, "foo", -1)
  73. denied(t, l, "bar", 1, now)
  74. hasTokens(t, l, "bar", -1)
  75. // Refill 2 tokens for foo and bar (1 available after debt), try
  76. // to consume 4. Overdraft is capped to 2.
  77. now = now.Add(2 * time.Second)
  78. allowed(t, l, "foo", 1, now)
  79. denied(t, l, "foo", 3, now)
  80. hasTokens(t, l, "foo", -2)
  81. allowed(t, l, "bar", 1, now)
  82. denied(t, l, "bar", 3, now)
  83. hasTokens(t, l, "bar", -2)
  84. // Refill 1, not enough to allow.
  85. now = now.Add(time.Second)
  86. denied(t, l, "foo", 1, now)
  87. hasTokens(t, l, "foo", -2)
  88. denied(t, l, "bar", 1, now)
  89. hasTokens(t, l, "bar", -2)
  90. // qux evicts foo, foo can immediately burst 10 again.
  91. allowed(t, l, "qux", 1, now)
  92. hasTokens(t, l, "qux", 9)
  93. notInLimiter(t, l, "foo")
  94. allowed(t, l, "foo", 10, now)
  95. denied(t, l, "foo", 1, now)
  96. hasTokens(t, l, "foo", -1)
  97. }
  98. func TestDumpHTML(t *testing.T) {
  99. l := &Limiter[string]{
  100. Size: 3,
  101. Max: 10,
  102. Overdraft: 10,
  103. RefillInterval: testRefillInterval,
  104. }
  105. now := time.Now().Truncate(testRefillInterval).Add(time.Millisecond)
  106. allowed(t, l, "foo", 10, now)
  107. denied(t, l, "foo", 2, now)
  108. allowed(t, l, "bar", 4, now)
  109. allowed(t, l, "qux", 1, now)
  110. var out bytes.Buffer
  111. l.DumpHTML(&out, false)
  112. want := strings.Join([]string{
  113. "<table>",
  114. "<tr><th>Key</th><th>Tokens</th></tr>",
  115. "<tr><td>qux</td><td>9</td></tr>",
  116. "<tr><td>bar</td><td>6</td></tr>",
  117. "<tr><td>foo</td><td><b>-2</b></td></tr>",
  118. "</table>",
  119. }, "")
  120. if diff := cmp.Diff(out.String(), want); diff != "" {
  121. t.Fatalf("wrong DumpHTML output (-got+want):\n%s", diff)
  122. }
  123. out.Reset()
  124. l.DumpHTML(&out, true)
  125. want = strings.Join([]string{
  126. "<table>",
  127. "<tr><th>Key</th><th>Tokens</th></tr>",
  128. "<tr><td>foo</td><td>-2</td></tr>",
  129. "</table>",
  130. }, "")
  131. if diff := cmp.Diff(out.String(), want); diff != "" {
  132. t.Fatalf("wrong DumpHTML output (-got+want):\n%s", diff)
  133. }
  134. // Check that DumpHTML updates tokens even if the key wasn't hit
  135. // organically.
  136. now = now.Add(3 * time.Second)
  137. out.Reset()
  138. l.dumpHTML(&out, false, now)
  139. want = strings.Join([]string{
  140. "<table>",
  141. "<tr><th>Key</th><th>Tokens</th></tr>",
  142. "<tr><td>qux</td><td>10</td></tr>",
  143. "<tr><td>bar</td><td>9</td></tr>",
  144. "<tr><td>foo</td><td>1</td></tr>",
  145. "</table>",
  146. }, "")
  147. if diff := cmp.Diff(out.String(), want); diff != "" {
  148. t.Fatalf("wrong DumpHTML output (-got+want):\n%s", diff)
  149. }
  150. }
  151. func allowed(t *testing.T, l *Limiter[string], key string, count int, now time.Time) {
  152. t.Helper()
  153. for i := 0; i < count; i++ {
  154. if !l.allow(key, now) {
  155. toks, ok := l.tokensForTest(key)
  156. t.Errorf("after %d times: allow(%q, %q) = false, want true (%d tokens available, in cache = %v)", i, key, now, toks, ok)
  157. }
  158. }
  159. }
  160. func denied(t *testing.T, l *Limiter[string], key string, count int, now time.Time) {
  161. t.Helper()
  162. for i := 0; i < count; i++ {
  163. if l.allow(key, now) {
  164. toks, ok := l.tokensForTest(key)
  165. t.Errorf("after %d times: allow(%q, %q) = true, want false (%d tokens available, in cache = %v)", i, key, now, toks, ok)
  166. }
  167. }
  168. }
  169. func hasTokens(t *testing.T, l *Limiter[string], key string, want int64) {
  170. t.Helper()
  171. got, ok := l.tokensForTest(key)
  172. if !ok {
  173. t.Errorf("key %q missing from limiter", key)
  174. } else if got != want {
  175. t.Errorf("key %q has %d tokens, want %d", key, got, want)
  176. }
  177. }
  178. func notInLimiter(t *testing.T, l *Limiter[string], key string) {
  179. t.Helper()
  180. if tokens, ok := l.tokensForTest(key); ok {
  181. t.Errorf("key %q unexpectedly tracked by limiter, with %d tokens", key, tokens)
  182. }
  183. }