(t *testing.T)
| 213 | } |
| 214 | |
| 215 | func TestAsyncResponsesDoesNotLeak(t *testing.T) { |
| 216 | tcs := []struct { |
| 217 | name string |
| 218 | finalRT func(requestCancel context.CancelFunc) RoundTripperFunc |
| 219 | cleanup func() |
| 220 | }{ |
| 221 | { |
| 222 | name: "happy path", |
| 223 | finalRT: func(_ context.CancelFunc) RoundTripperFunc { |
| 224 | return RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 225 | return &http.Response{ |
| 226 | Body: io.NopCloser(strings.NewReader("foo")), |
| 227 | }, nil |
| 228 | }) |
| 229 | }, |
| 230 | }, |
| 231 | { |
| 232 | name: "error path", |
| 233 | finalRT: func(_ context.CancelFunc) RoundTripperFunc { |
| 234 | return RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 235 | return nil, errors.New("foo") |
| 236 | }) |
| 237 | }, |
| 238 | }, |
| 239 | { |
| 240 | name: "combiner bails early", |
| 241 | finalRT: func(_ context.CancelFunc) RoundTripperFunc { |
| 242 | responseCounter := atomic.Int32{} |
| 243 | |
| 244 | return RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 245 | counter := responseCounter.Add(1) |
| 246 | if counter == 2 { |
| 247 | return &http.Response{ |
| 248 | StatusCode: http.StatusNotFound, // force the combiner to bail on the second response |
| 249 | Body: io.NopCloser(strings.NewReader("foo")), |
| 250 | }, nil |
| 251 | } |
| 252 | |
| 253 | return &http.Response{ |
| 254 | Body: io.NopCloser(strings.NewReader("foo")), |
| 255 | }, nil |
| 256 | }) |
| 257 | }, |
| 258 | }, |
| 259 | { |
| 260 | name: "context cancelled before returned responses", |
| 261 | finalRT: func(cancel context.CancelFunc) RoundTripperFunc { |
| 262 | go func() { |
| 263 | time.Sleep(1 * time.Second) |
| 264 | cancel() |
| 265 | }() |
| 266 | |
| 267 | return RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 268 | time.Sleep(3 * time.Second) |
| 269 | |
| 270 | return &http.Response{ |
| 271 | Body: io.NopCloser(strings.NewReader("foo")), |
| 272 | }, nil |
nothing calls this directly
no test coverage detected