(t *testing.T)
| 256 | } |
| 257 | |
| 258 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…