|
|
@@ -27,6 +27,7 @@ func TestNoTimeout(t *testing.T) {
|
|
|
t.Fatalf("error received from client: %v %v", err, resp)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
func TestCancel(t *testing.T) {
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
go func() {
|
|
|
@@ -59,6 +60,44 @@ func TestCancelAfterRequest(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestCancelAfterHangingRequest(t *testing.T) {
|
|
|
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
+ w.(http.Flusher).Flush()
|
|
|
+ <-w.(http.CloseNotifier).CloseNotify()
|
|
|
+ })
|
|
|
+
|
|
|
+ serv := httptest.NewServer(handler)
|
|
|
+ defer serv.Close()
|
|
|
+
|
|
|
+ ctx, cancel := context.WithCancel(context.Background())
|
|
|
+ resp, err := Get(ctx, nil, serv.URL)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("unexpected error in Get: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Cancel befer reading the body.
|
|
|
+ // Reading Request.Body should fail, since the request was
|
|
|
+ // canceled before anything was written.
|
|
|
+ cancel()
|
|
|
+
|
|
|
+ done := make(chan struct{})
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ b, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if len(b) != 0 || err == nil {
|
|
|
+ t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
|
|
|
+ }
|
|
|
+ close(done)
|
|
|
+ }()
|
|
|
+
|
|
|
+ select {
|
|
|
+ case <-time.After(1 * time.Second):
|
|
|
+ t.Errorf("Test timed out")
|
|
|
+ case <-done:
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func doRequest(ctx context.Context) (*http.Response, error) {
|
|
|
var okHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
time.Sleep(requestDuration)
|