(t *testing.T)
| 231 | } |
| 232 | |
| 233 | func TestSimultaneousRequests(t *testing.T) { |
| 234 | const ( |
| 235 | limit = 1 |
| 236 | burst = 5 |
| 237 | numRequests = 15 |
| 238 | ) |
| 239 | var ( |
| 240 | wg sync.WaitGroup |
| 241 | numOK = uint32(0) |
| 242 | ) |
| 243 | |
| 244 | // Very slow replenishing bucket. |
| 245 | lim := NewLimiter(limit, burst) |
| 246 | |
| 247 | // Tries to take a token, atomically updates the counter and decreases the wait |
| 248 | // group counter. |
| 249 | f := func() { |
| 250 | defer wg.Done() |
| 251 | if ok := lim.Allow(); ok { |
| 252 | atomic.AddUint32(&numOK, 1) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | wg.Add(numRequests) |
| 257 | for range numRequests { |
| 258 | go f() |
| 259 | } |
| 260 | wg.Wait() |
| 261 | if numOK != burst { |
| 262 | t.Errorf("numOK = %d, want %d", numOK, burst) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | func TestLongRunningQPS(t *testing.T) { |
| 267 | // The test runs for a few (fake) seconds executing many requests |
nothing calls this directly
no test coverage detected