mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: fix TestSimpleHTTPClientDoCancelContextResponseBodyClosed
This fixes the bug that the test may hang forever because RoundTrip is blocked. fixes #2449
This commit is contained in:
parent
e20b487904
commit
7716bdf981
@ -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,11 +114,16 @@ 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
|
case <-t.finishCancel:
|
||||||
return nil, errors.New("cancelled")
|
return nil, errors.New("cancelled")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *fakeTransport) CancelRequest(*http.Request) {
|
func (t *fakeTransport) CancelRequest(*http.Request) {
|
||||||
@ -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
|
// wait that simpleHTTPClient knows the context is already timed out,
|
||||||
// knows the context is already timed out
|
// and calls CancelRequest
|
||||||
<-tr.startCancel
|
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{})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user