Test deadline behavior without context identity
This commit is contained in:
@@ -332,26 +332,30 @@ func TestHandlerRunTimeoutValidationAndContextMapping(t *testing.T) {
|
||||
defer cancel()
|
||||
parentDeadline, _ := parent.Deadline()
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
child bool
|
||||
name, body string
|
||||
parent context.Context
|
||||
wantDeadline time.Time
|
||||
}{
|
||||
{name: "omitted", body: `{"prompt_id":"p"}`},
|
||||
{name: "null", body: `{"prompt_id":"p","run_timeout_seconds":null}`},
|
||||
{name: "zero", body: `{"prompt_id":"p","run_timeout_seconds":0}`},
|
||||
{name: "positive", body: `{"prompt_id":"p","run_timeout_seconds":10}`, child: true},
|
||||
{name: "omitted inherits deadline", body: `{"prompt_id":"p"}`, parent: parent, wantDeadline: parentDeadline},
|
||||
{name: "null inherits deadline", body: `{"prompt_id":"p","run_timeout_seconds":null}`, parent: parent, wantDeadline: parentDeadline},
|
||||
{name: "zero inherits deadline", body: `{"prompt_id":"p","run_timeout_seconds":0}`, parent: parent, wantDeadline: parentDeadline},
|
||||
{name: "positive preserves earlier deadline", body: `{"prompt_id":"p","run_timeout_seconds":10}`, parent: parent, wantDeadline: parentDeadline},
|
||||
{name: "omitted adds no deadline", body: `{"prompt_id":"p"}`, parent: context.Background()},
|
||||
{name: "null adds no deadline", body: `{"prompt_id":"p","run_timeout_seconds":null}`, parent: context.Background()},
|
||||
{name: "zero adds no deadline", body: `{"prompt_id":"p","run_timeout_seconds":0}`, parent: context.Background()},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
called := false
|
||||
h := NewHandler(runnerFunc(func(ctx context.Context, req promptkit.RunRequest) (*promptkit.RunResult, error) {
|
||||
called = true
|
||||
deadline, ok := ctx.Deadline()
|
||||
if !ok || !deadline.Equal(parentDeadline) || (ctx != parent) != tc.child {
|
||||
t.Fatalf("unexpected execution context: deadline=%v child=%t", deadline, ctx != parent)
|
||||
if ok != !tc.wantDeadline.IsZero() || (ok && !deadline.Equal(tc.wantDeadline)) {
|
||||
t.Fatalf("execution deadline: got %v (present=%t), want %v", deadline, ok, tc.wantDeadline)
|
||||
}
|
||||
return &promptkit.RunResult{}, nil
|
||||
}))
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(tc.body)).WithContext(parent))
|
||||
h.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/v1/runs", strings.NewReader(tc.body)).WithContext(tc.parent))
|
||||
if !called || w.Code != http.StatusOK {
|
||||
t.Fatalf("context mapping: called=%t status=%d body=%s", called, w.Code, w.Body.String())
|
||||
}
|
||||
@@ -361,6 +365,9 @@ func TestHandlerRunTimeoutValidationAndContextMapping(t *testing.T) {
|
||||
canceled, cancelParent := context.WithCancel(context.Background())
|
||||
cancelParent()
|
||||
h := NewHandler(runnerFunc(func(ctx context.Context, req promptkit.RunRequest) (*promptkit.RunResult, error) {
|
||||
if ctx.Err() != context.Canceled {
|
||||
t.Fatalf("parent cancellation did not reach execution: %v", ctx.Err())
|
||||
}
|
||||
return nil, ctx.Err()
|
||||
}))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
Reference in New Issue
Block a user