Skip to content

Commit

Permalink
contrib/net/http: fix flaky TestRoundTripperNetworkError (#1292)
Browse files Browse the repository at this point in the history
The test was intended to cause an HTTP request to time out on the client
side, but this timeout was induced with a short sleep in the HTTP
handler. This caused the test to sometimes fail in CI. Removing the
sleep made the test fail consistently.

To force the client to time out, make the handler wait on a channel that
doesn't close until the end of the test.

This fix can be verified locally by setting the client timeout to a very
long value like 1 minute. The test will still pass.

Fixes #1291.
  • Loading branch information
nsrip-dd committed May 13, 2022
1 parent 46ca059 commit 86a56ed
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions contrib/net/http/roundtripper_test.go
Expand Up @@ -123,13 +123,14 @@ func TestRoundTripperNetworkError(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

done := make(chan struct{})
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header))
assert.NoError(t, err)
time.Sleep(10 * time.Millisecond)
w.Write([]byte("Timeout"))
<-done
}))
defer s.Close()
defer close(done)

rt := WrapRoundTripper(http.DefaultTransport,
WithBefore(func(req *http.Request, span ddtrace.Span) {
Expand Down

0 comments on commit 86a56ed

Please sign in to comment.