MCPcopy Create free account
hub / github.com/conforma/cli / TestRetryTransport_WithCustomConfig

Function TestRetryTransport_WithCustomConfig

internal/http/retry_test.go:365–427  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

363}
364
365func TestRetryTransport_WithCustomConfig(t *testing.T) {
366 // Set custom retry configuration
367 customConfig := RetryConfig{
368 MaxWait: 200 * time.Millisecond, // Much shorter for testing
369 MaxRetry: 2,
370 Duration: 25 * time.Millisecond, // Much shorter for testing
371 Factor: 2.0,
372 Jitter: 0.1,
373 }
374 SetRetryConfig(customConfig)
375 defer func() {
376 // Restore default configuration
377 SetRetryConfig(RetryConfig{
378 MaxWait: 3 * time.Second,
379 MaxRetry: 3,
380 Duration: 1 * time.Second,
381 Factor: 2.0,
382 Jitter: 0.1,
383 })
384 }()
385
386 callCount := 0
387 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
388 callCount++
389 if callCount <= 2 {
390 w.WriteHeader(http.StatusTooManyRequests)
391 _, err := w.Write([]byte(`{"error": "Too Many Requests"}`))
392 if err != nil {
393 t.Errorf("Failed to write response: %v", err)
394 }
395 return
396 }
397 w.WriteHeader(http.StatusOK)
398 _, err := w.Write([]byte(`{"success": true}`))
399 if err != nil {
400 t.Errorf("Failed to write response: %v", err)
401 }
402 }))
403 defer server.Close()
404
405 // Create a transport that will retry on 429 with custom configuration
406 baseTransport := &http.Transport{}
407 retryTransport := NewRetryTransport(baseTransport)
408
409 // Create a request
410 req, err := http.NewRequest("GET", server.URL, nil)
411 require.NoError(t, err)
412
413 // Execute the request
414 start := time.Now()
415 resp, err := retryTransport.RoundTrip(req)
416 duration := time.Since(start)
417
418 // Verify the response
419 require.NoError(t, err)
420 assert.Equal(t, http.StatusOK, resp.StatusCode)
421
422 // Verify that we retried (should have called the server 3 times)

Callers

nothing calls this directly

Calls 6

SetRetryConfigFunction · 0.85
NewRetryTransportFunction · 0.85
ErrorfMethod · 0.65
CloseMethod · 0.65
WriteMethod · 0.45
RoundTripMethod · 0.45

Tested by

no test coverage detected