Bound the HTTP timeout regression tests

This commit is contained in:
2026-11-19 15:16:33 +00:00
parent cf50650e55
commit ac858be2a2
2 changed files with 25 additions and 25 deletions

View File

@@ -6,6 +6,10 @@ refer to the [implementation plan](implementation.md).
## R1: Bound the HTTP timeout regression tests (Stage 4) ## R1: Bound the HTTP timeout regression tests (Stage 4)
**Complete.** The tests use independent parent deadlines and capture the
target invocation's error without sharing a channel. All three passed
twice under the race detector.
In [handler_test.go](../../internal/adapter/http/handler_test.go), In [handler_test.go](../../internal/adapter/http/handler_test.go),
`TestHandlerDeadlineDuringBuiltInGeneration` runs the handler synchronously `TestHandlerDeadlineDuringBuiltInGeneration` runs the handler synchronously
while the fake provider waits on a channel that is released only after the while the fake provider waits on a channel that is released only after the

View File

@@ -1215,11 +1215,9 @@ func TestHandlerDeadlineCancelsBuiltInRetryWait(t *testing.T) {
})) }))
defer provider.Close() defer provider.Close()
h := newRetryHandler(t, provider.URL+"/v1", intPointer(1), 0, nil) h := newRetryHandler(t, provider.URL+"/v1", intPointer(1), 0, nil)
failures := observeRunnerFailures(h) w, err := serveObservedTimeoutRequest(h, `{"prompt_id":"p","run_timeout_seconds":1}`)
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p","run_timeout_seconds":1}`)))
assertHTTPErrorCode(t, w, http.StatusGatewayTimeout, "deadline_exceeded") assertHTTPErrorCode(t, w, http.StatusGatewayTimeout, "deadline_exceeded")
if err := <-failures; !errors.Is(err, promptkit.ErrOperationTimeout) { if !errors.Is(err, promptkit.ErrOperationTimeout) {
t.Fatalf("engine did not attribute retry wait to operation timeout: %v", err) t.Fatalf("engine did not attribute retry wait to operation timeout: %v", err)
} }
if atomic.LoadInt32(&hits) != 1 { if atomic.LoadInt32(&hits) != 1 {
@@ -1237,15 +1235,12 @@ func TestHandlerDeadlineDuringBuiltInGeneration(t *testing.T) {
defer provider.Close() defer provider.Close()
defer close(release) defer close(release)
h := newRetryHandler(t, provider.URL+"/v1", intPointer(0), 0, nil) h := newRetryHandler(t, provider.URL+"/v1", intPointer(0), 0, nil)
failures := observeRunnerFailures(h) w, err := serveObservedTimeoutRequest(h, `{"prompt_id":"p","run_timeout_seconds":1,"include_execution_report":true}`)
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p","run_timeout_seconds":1,"include_execution_report":true}`)))
select { select {
case <-started: case <-started:
default: default:
t.Fatal("provider call did not start") t.Fatal("provider call did not start")
} }
err := <-failures
if !errors.Is(err, promptkit.ErrOperationTimeout) || !errors.Is(err, context.DeadlineExceeded) { if !errors.Is(err, promptkit.ErrOperationTimeout) || !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("engine did not attribute in-flight generation to operation timeout: %v (deadline cause: %t)", err, errors.Is(err, context.DeadlineExceeded)) t.Errorf("engine did not attribute in-flight generation to operation timeout: %v (deadline cause: %t)", err, errors.Is(err, context.DeadlineExceeded))
} }
@@ -1285,22 +1280,22 @@ func TestHandlerDeadlineCancelsCapacityWaitAndReleasesAdmission(t *testing.T) {
}() }()
queue := 1 queue := 1
h := newRetryHandler(t, provider.URL+"/v1", intPointer(0), 1, &queue) h := newRetryHandler(t, provider.URL+"/v1", intPointer(0), 1, &queue)
failures := observeRunnerFailures(h) firstCtx, cancelFirst := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFirst()
first := httptest.NewRecorder() first := httptest.NewRecorder()
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer close(done) defer close(done)
h.ServeHTTP(first, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p"}`))) h.ServeHTTP(first, httptest.NewRequestWithContext(firstCtx, http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p"}`)))
}() }()
select { select {
case <-started: case <-started:
case <-time.After(3 * time.Second): case <-time.After(3 * time.Second):
t.Fatal("first provider call did not start") t.Fatal("first provider call did not start")
} }
waiting := httptest.NewRecorder() waiting, err := serveObservedTimeoutRequest(h, `{"prompt_id":"p","run_timeout_seconds":1}`)
h.ServeHTTP(waiting, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p","run_timeout_seconds":1}`)))
assertHTTPErrorCode(t, waiting, http.StatusGatewayTimeout, "deadline_exceeded") assertHTTPErrorCode(t, waiting, http.StatusGatewayTimeout, "deadline_exceeded")
if err := <-failures; !errors.Is(err, promptkit.ErrOperationTimeout) { if !errors.Is(err, promptkit.ErrOperationTimeout) {
t.Fatalf("engine did not attribute capacity wait to operation timeout: %v", err) t.Fatalf("engine did not attribute capacity wait to operation timeout: %v", err)
} }
if atomic.LoadInt32(&hits) != 1 { if atomic.LoadInt32(&hits) != 1 {
@@ -1315,24 +1310,25 @@ func TestHandlerDeadlineCancelsCapacityWaitAndReleasesAdmission(t *testing.T) {
if first.Code != http.StatusOK { if first.Code != http.StatusOK {
t.Fatalf("first request: status=%d body=%s", first.Code, first.Body.String()) t.Fatalf("first request: status=%d body=%s", first.Code, first.Body.String())
} }
third := httptest.NewRecorder() third, _ := serveObservedTimeoutRequest(h, `{"prompt_id":"p"}`)
h.ServeHTTP(third, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p"}`)))
if third.Code != http.StatusOK || atomic.LoadInt32(&hits) != 2 { if third.Code != http.StatusOK || atomic.LoadInt32(&hits) != 2 {
t.Fatalf("admission was not released: status=%d hits=%d body=%s", third.Code, atomic.LoadInt32(&hits), third.Body.String()) t.Fatalf("admission was not released: status=%d hits=%d body=%s", third.Code, atomic.LoadInt32(&hits), third.Body.String())
} }
} }
func observeRunnerFailures(h *Handler) <-chan error { func serveObservedTimeoutRequest(h *Handler, body string) (*httptest.ResponseRecorder, error) {
failures := make(chan error, 1) // Bound the test independently of the shorter operation budget under test.
runner := h.runner ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
h.runner = runnerFunc(func(ctx context.Context, request promptkit.RunRequest) (*promptkit.RunResult, error) { defer cancel()
result, err := runner.Run(ctx, request) var runErr error
if err != nil { observed := NewHandlerWithOptions(runnerFunc(func(ctx context.Context, request promptkit.RunRequest) (*promptkit.RunResult, error) {
failures <- err result, err := h.runner.Run(ctx, request)
} runErr = err
return result, err return result, err
}) }), h.options)
return failures w := httptest.NewRecorder()
observed.ServeHTTP(w, httptest.NewRequestWithContext(ctx, http.MethodPost, "/v1/runs", strings.NewReader(body)))
return w, runErr
} }
func newRetryHandler(t *testing.T, endpoint string, retries *int, concurrency int, queue *int) *Handler { func newRetryHandler(t *testing.T, endpoint string, retries *int, concurrency int, queue *int) *Handler {