(t *testing.T)
| 77 | } |
| 78 | |
| 79 | func TestProxyHandlerBreakerTimeout(t *testing.T) { |
| 80 | // This test sends a request which will take a long time to complete. |
| 81 | // Then another one with a very short context timeout. |
| 82 | // Verifies that the second one fails with timeout. |
| 83 | seen := make(chan struct{}) |
| 84 | resp := make(chan struct{}) |
| 85 | defer close(resp) // Allow all requests to pass through. |
| 86 | blockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 87 | seen <- struct{}{} |
| 88 | <-resp |
| 89 | }) |
| 90 | breaker := proxy.NewBreaker(proxy.BreakerParams{ |
| 91 | QueueDepth: 1, MaxConcurrency: 1, InitialCapacity: 1, |
| 92 | }) |
| 93 | h := proxy.Handler(breaker, blockHandler) |
| 94 | |
| 95 | go func() { |
| 96 | h(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, userContainerHost, nil)) |
| 97 | }() |
| 98 | |
| 99 | // Wait until the first request has entered the handler. |
| 100 | <-seen |
| 101 | |
| 102 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) |
| 103 | defer cancel() |
| 104 | |
| 105 | rec := httptest.NewRecorder() |
| 106 | h(rec, httptest.NewRequest(http.MethodGet, userContainerHost, nil).WithContext(ctx)) |
| 107 | |
| 108 | require.Equal(t, http.StatusServiceUnavailable, rec.Code) |
| 109 | require.True(t, strings.Contains(rec.Body.String(), context.DeadlineExceeded.Error())) |
| 110 | } |
nothing calls this directly
no test coverage detected