package traefikllmgateway import ( "context" "io" "net/http" "strings" "testing" "time" ) // A reader unblocked by the watchdog's cancel must find the failure already recorded. func TestWatchdogFire_RecordsTimeoutBeforeCancel(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() cancelledAtRecord := make(chan bool, 1) rec := func(_ *http.Response, _ error) { time.Sleep(20 * time.Millisecond) cancelledAtRecord <- ctx.Err() != nil } wb := newWatchdogBody(io.NopCloser(strings.NewReader("")), cancel, time.Hour, "p1", rec) wb.fire() if <-cancelledAtRecord { t.Fatal("context was cancelled before onTimeout recorded the failure") } if ctx.Err() == nil { t.Fatal("context not cancelled after fire") } }