TestThrottlingWithoutStaleCache verifies that the cache throttles requests when there's no stale cache and the registry is failing
(t *testing.T)
| 300 | // TestThrottlingWithoutStaleCache verifies that the cache throttles requests |
| 301 | // when there's no stale cache and the registry is failing |
| 302 | func TestThrottlingWithoutStaleCache(t *testing.T) { |
| 303 | mock := &mockRegistry{ |
| 304 | err: errors.New("etcd connection failed"), |
| 305 | } |
| 306 | |
| 307 | // Create cache with short retry interval for testing |
| 308 | c := New(mock, func(o *Options) { |
| 309 | o.TTL = time.Minute |
| 310 | o.MinimumRetryInterval = 2 * time.Second |
| 311 | o.Logger = logger.DefaultLogger |
| 312 | }).(*cache) |
| 313 | |
| 314 | // First request - should fail and record the attempt |
| 315 | _, err := c.GetService("test.service") |
| 316 | if err == nil { |
| 317 | t.Fatal("Expected error on first request, got nil") |
| 318 | } |
| 319 | |
| 320 | callCount1 := mock.getCallCount() |
| 321 | if callCount1 != 1 { |
| 322 | t.Fatalf("Expected 1 call on first attempt, got %d", callCount1) |
| 323 | } |
| 324 | |
| 325 | // Immediate second request - should be throttled (no registry call) |
| 326 | _, err = c.GetService("test.service") |
| 327 | if err == nil { |
| 328 | t.Fatal("Expected error on throttled request, got nil") |
| 329 | } |
| 330 | |
| 331 | callCount2 := mock.getCallCount() |
| 332 | if callCount2 != 1 { |
| 333 | t.Errorf("Expected throttling (still 1 call), got %d calls", callCount2) |
| 334 | } |
| 335 | |
| 336 | // Wait for retry interval to pass |
| 337 | time.Sleep(2100 * time.Millisecond) |
| 338 | |
| 339 | // Third request - should be allowed (makes another registry call) |
| 340 | _, err = c.GetService("test.service") |
| 341 | if err == nil { |
| 342 | t.Fatal("Expected error after retry interval, got nil") |
| 343 | } |
| 344 | |
| 345 | callCount3 := mock.getCallCount() |
| 346 | if callCount3 != 2 { |
| 347 | t.Errorf("Expected 2 calls after retry interval, got %d", callCount3) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // TestThrottlingMultipleConcurrentRequests verifies that throttling works |
| 352 | // correctly with multiple concurrent requests when there's no stale cache |
nothing calls this directly
no test coverage detected
searching dependent graphs…