MCPcopy
hub / github.com/grafana/k6 / TestCancelledRequest

Function TestCancelledRequest

lib/netext/httpext/tracer_test.go:258–293  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

256}
257
258func TestCancelledRequest(t *testing.T) {
259 t.Parallel()
260 srv := httptest.NewTLSServer(httpbin.New().Handler())
261 t.Cleanup(srv.Close)
262
263 cancelTest := func(t *testing.T) {
264 tracer := &Tracer{}
265 req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/delay/1", nil)
266 require.NoError(t, err)
267
268 ctx, cancel := context.WithCancel(httptrace.WithClientTrace(req.Context(), tracer.Trace()))
269 req = req.WithContext(ctx)
270 go func() {
271 time.Sleep(time.Duration(rand.Int31n(50)) * time.Millisecond)
272 cancel()
273 }()
274
275 resp, err := srv.Client().Transport.RoundTrip(req) //nolint:bodyclose
276 _ = tracer.Done()
277 if resp == nil && err == nil {
278 t.Errorf("Expected either a RoundTrip response or error but got %#v and %#v", resp, err)
279 }
280 }
281
282 // This Run will not return until the parallel subtests complete.
283 t.Run("group", func(t *testing.T) {
284 t.Parallel()
285 for i := range 200 {
286 t.Run(fmt.Sprintf("TestCancelledRequest_%d", i),
287 func(t *testing.T) {
288 t.Parallel()
289 cancelTest(t)
290 })
291 }
292 })
293}

Callers

nothing calls this directly

Calls 9

TraceMethod · 0.95
DoneMethod · 0.95
DurationMethod · 0.80
ErrorfMethod · 0.80
CleanupMethod · 0.65
ContextMethod · 0.65
SleepMethod · 0.65
RunMethod · 0.65
RoundTripMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…