@@ -164,6 +164,54 @@ func TestDoContextCancellation(t *testing.T) {
164164 }
165165}
166166
167+ func TestDoContextCancellationSlowServer (t * testing.T ) {
168+ serverDone := make (chan struct {})
169+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
170+ w .WriteHeader (http .StatusOK )
171+ if f , ok := w .(http.Flusher ); ok {
172+ f .Flush ()
173+ }
174+ <- serverDone
175+ }))
176+ defer func () {
177+ close (serverDone )
178+ ts .Close ()
179+ }()
180+
181+ client , err := NewClient (Config {
182+ Address : ts .URL ,
183+ })
184+ if err != nil {
185+ t .Fatalf ("failed to create client: %v" , err )
186+ }
187+
188+ req , err := http .NewRequest (http .MethodGet , ts .URL , nil )
189+ if err != nil {
190+ t .Fatalf ("failed to create request: %v" , err )
191+ }
192+
193+ ctx , cancel := context .WithTimeout (context .Background (), 50 * time .Millisecond )
194+ defer cancel ()
195+
196+ start := time .Now ()
197+ resp , body , err := client .Do (ctx , req )
198+ elapsed := time .Since (start )
199+
200+ if ! errors .Is (err , context .DeadlineExceeded ) {
201+ t .Errorf ("expected error %v, got: %v" , context .DeadlineExceeded , err )
202+ }
203+ if body != nil {
204+ t .Errorf ("expected no body due to cancellation, got: %q" , string (body ))
205+ }
206+ if elapsed > 500 * time .Millisecond {
207+ t .Errorf ("client.Do did not return promptly on cancellation: took %v" , elapsed )
208+ }
209+
210+ if resp != nil && resp .Body != nil {
211+ resp .Body .Close ()
212+ }
213+ }
214+
167215// Serve any http request with a response of N KB of spaces.
168216type serveSpaces struct {
169217 sizeKB int
0 commit comments