MCPcopy
hub / github.com/micro/go-micro / TestThrottlingMultipleConcurrentRequests

Function TestThrottlingMultipleConcurrentRequests

registry/cache/cache_test.go:353–418  ·  view source on GitHub ↗

TestThrottlingMultipleConcurrentRequests verifies that throttling works correctly with multiple concurrent requests when there's no stale cache

(t *testing.T)

Source from the content-addressed store, hash-verified

351// TestThrottlingMultipleConcurrentRequests verifies that throttling works
352// correctly with multiple concurrent requests when there's no stale cache
353func 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 }

Callers

nothing calls this directly

Calls 8

getCallCountMethod · 0.95
DoneMethod · 0.80
NewFunction · 0.70
AddMethod · 0.65
GetServiceMethod · 0.65
WaitMethod · 0.45
ErrorfMethod · 0.45
SleepMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…