TestThrottlingMultipleConcurrentRequests verifies that throttling works correctly with multiple concurrent requests when there's no stale cache
(t *testing.T)
| 351 | // TestThrottlingMultipleConcurrentRequests verifies that throttling works |
| 352 | // correctly with multiple concurrent requests when there's no stale cache |
| 353 | func TestThrottlingMultipleConcurrentRequests(t *testing.T) { |
| 354 | mock := &mockRegistry{ |
| 355 | err: errors.New("etcd overloaded"), |
| 356 | delay: 50 * time.Millisecond, |
| 357 | } |
| 358 | |
| 359 | c := New(mock, func(o *Options) { |
| 360 | o.TTL = time.Minute |
| 361 | o.MinimumRetryInterval = 1 * time.Second |
| 362 | o.Logger = logger.DefaultLogger |
| 363 | }).(*cache) |
| 364 | |
| 365 | // First batch - all concurrent requests should result in single call (singleflight) |
| 366 | const concurrency = 20 |
| 367 | var wg sync.WaitGroup |
| 368 | wg.Add(concurrency) |
| 369 | |
| 370 | for i := 0; i < concurrency; i++ { |
| 371 | go func() { |
| 372 | defer wg.Done() |
| 373 | c.GetService("test.service") |
| 374 | }() |
| 375 | } |
| 376 | |
| 377 | wg.Wait() |
| 378 | |
| 379 | callCount1 := mock.getCallCount() |
| 380 | if callCount1 != 1 { |
| 381 | t.Errorf("Expected 1 call (singleflight), got %d", callCount1) |
| 382 | } |
| 383 | |
| 384 | // Second batch immediately after - should all be throttled (no new calls) |
| 385 | wg.Add(concurrency) |
| 386 | for i := 0; i < concurrency; i++ { |
| 387 | go func() { |
| 388 | defer wg.Done() |
| 389 | c.GetService("test.service") |
| 390 | }() |
| 391 | } |
| 392 | |
| 393 | wg.Wait() |
| 394 | |
| 395 | callCount2 := mock.getCallCount() |
| 396 | if callCount2 != 1 { |
| 397 | t.Errorf("Expected throttling (still 1 call), got %d", callCount2) |
| 398 | } |
| 399 | |
| 400 | // Wait for retry interval |
| 401 | time.Sleep(1100 * time.Millisecond) |
| 402 | |
| 403 | // Third batch - should result in one more call |
| 404 | wg.Add(concurrency) |
| 405 | for i := 0; i < concurrency; i++ { |
| 406 | go func() { |
| 407 | defer wg.Done() |
| 408 | c.GetService("test.service") |
| 409 | }() |
| 410 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…