Browse Source

client: fix TestSimpleHTTPClientDoCancelContextResponseBodyClosed

This fixes the bug that the test may hang forever because RoundTrip is
blocked. fixes #2449
Yicheng Qin 10 years ago
parent
commit
7716bdf981
1 changed files with 12 additions and 6 deletions
  1. 12 6
      client/client_test.go

+ 12 - 6
client/client_test.go

@@ -26,6 +26,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/pkg/testutil"
 )
 )
 
 
 type actionAssertingHTTPClient struct {
 type actionAssertingHTTPClient struct {
@@ -113,10 +114,15 @@ func (t *fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
 	case err := <-t.errchan:
 	case err := <-t.errchan:
 		return nil, err
 		return nil, err
 	case <-t.startCancel:
 	case <-t.startCancel:
+		select {
+		// this simulates that the request is finished before cancel effects
+		case resp := <-t.respchan:
+			return resp, nil
 		// wait on finishCancel to simulate taking some amount of
 		// wait on finishCancel to simulate taking some amount of
 		// time while calling CancelRequest
 		// time while calling CancelRequest
-		<-t.finishCancel
-		return nil, errors.New("cancelled")
+		case <-t.finishCancel:
+			return nil, errors.New("cancelled")
+		}
 	}
 	}
 }
 }
 
 
@@ -203,12 +209,12 @@ func TestSimpleHTTPClientDoCancelContextResponseBodyClosed(t *testing.T) {
 
 
 	body := &checkableReadCloser{ReadCloser: ioutil.NopCloser(strings.NewReader("foo"))}
 	body := &checkableReadCloser{ReadCloser: ioutil.NopCloser(strings.NewReader("foo"))}
 	go func() {
 	go func() {
-		// wait for CancelRequest to be called, informing us that simpleHTTPClient
-		// knows the context is already timed out
-		<-tr.startCancel
+		// wait that simpleHTTPClient knows the context is already timed out,
+		// and calls CancelRequest
+		testutil.WaitSchedule()
 
 
+		// response is returned before cancel effects
 		tr.respchan <- &http.Response{Body: body}
 		tr.respchan <- &http.Response{Body: body}
-		tr.finishCancel <- struct{}{}
 	}()
 	}()
 
 
 	_, _, err := c.Do(ctx, &fakeAction{})
 	_, _, err := c.Do(ctx, &fakeAction{})