Merge pull request #801 from iamqizhao/race-fix

Improve a transport test
This commit is contained in:
Qi Zhao
2016-07-30 11:40:24 -07:00
committed by GitHub
2 changed files with 27 additions and 20 deletions

View File

@ -2156,8 +2156,8 @@ func testClientRequestBodyError_Cancel(t *testing.T, e env) {
te.withServerTester(func(st *serverTester) { te.withServerTester(func(st *serverTester) {
st.writeHeadersGRPC(1, "/grpc.testing.TestService/UnaryCall") st.writeHeadersGRPC(1, "/grpc.testing.TestService/UnaryCall")
// Say we have 5 bytes coming, but cancel it instead. // Say we have 5 bytes coming, but cancel it instead.
st.writeData(1, false, []byte{0, 0, 0, 0, 5})
st.writeRSTStream(1, http2.ErrCodeCancel) st.writeRSTStream(1, http2.ErrCodeCancel)
st.writeData(1, false, []byte{0, 0, 0, 0, 5})
// Verify we didn't a call yet. // Verify we didn't a call yet.
select { select {

View File

@ -111,20 +111,28 @@ func (h *testStreamHandler) handleStreamMisbehave(t *testing.T, s *Stream) {
if !ok { if !ok {
t.Fatalf("Failed to convert %v to *http2Server", s.ServerTransport()) t.Fatalf("Failed to convert %v to *http2Server", s.ServerTransport())
} }
size := 1
if s.Method() == "foo.MaxFrame" {
size = http2MaxFrameLen
}
// Drain the client side stream flow control window.
var sent int var sent int
for sent <= initialWindowSize { p := make([]byte, http2MaxFrameLen)
for sent < initialWindowSize {
<-conn.writableChan <-conn.writableChan
if err := conn.framer.writeData(true, s.id, false, make([]byte, size)); err != nil { n := initialWindowSize - sent
// The last message may be smaller than http2MaxFrameLen
if n <= http2MaxFrameLen {
if s.Method() == "foo.Connection" {
// Violate connection level flow control window of client but do not
// violate any stream level windows.
p = make([]byte, n)
} else {
// Violate stream level flow control window of client.
p = make([]byte, n+1)
}
}
if err := conn.framer.writeData(true, s.id, false, p); err != nil {
conn.writableChan <- 0 conn.writableChan <- 0
break break
} }
conn.writableChan <- 0 conn.writableChan <- 0
sent += size sent += len(p)
} }
} }
@ -553,6 +561,7 @@ func TestServerContextCanceledOnClosedConnection(t *testing.T) {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Fatalf("Failed to cancel the context of the sever side stream.") t.Fatalf("Failed to cancel the context of the sever side stream.")
} }
server.stop()
} }
func TestServerWithMisbehavedClient(t *testing.T) { func TestServerWithMisbehavedClient(t *testing.T) {
@ -659,7 +668,7 @@ func TestClientWithMisbehavedServer(t *testing.T) {
server, ct := setUp(t, 0, math.MaxUint32, misbehaved) server, ct := setUp(t, 0, math.MaxUint32, misbehaved)
callHdr := &CallHdr{ callHdr := &CallHdr{
Host: "localhost", Host: "localhost",
Method: "foo", Method: "foo.Stream",
} }
conn, ok := ct.(*http2Client) conn, ok := ct.(*http2Client)
if !ok { if !ok {
@ -670,7 +679,8 @@ func TestClientWithMisbehavedServer(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to open stream: %v", err) t.Fatalf("Failed to open stream: %v", err)
} }
if err := ct.Write(s, expectedRequest, &Options{Last: true, Delay: false}); err != nil { d := make([]byte, 1)
if err := ct.Write(s, d, &Options{Last: true, Delay: false}); err != nil {
t.Fatalf("Failed to write: %v", err) t.Fatalf("Failed to write: %v", err)
} }
// Read without window update. // Read without window update.
@ -692,18 +702,15 @@ func TestClientWithMisbehavedServer(t *testing.T) {
} }
// Test the logic for the violation of the connection flow control window size restriction. // Test the logic for the violation of the connection flow control window size restriction.
// //
// Generate enough streams to drain the connection window. // Generate enough streams to drain the connection window. Make the server flood the traffic
callHdr = &CallHdr{ // to violate flow control window size of the connection.
Host: "localhost", callHdr.Method = "foo.Connection"
Method: "foo.MaxFrame", for i := 0; i < int(initialConnWindowSize/initialWindowSize+10); i++ {
}
// Make the server flood the traffic to violate flow control window size of the connection.
for {
s, err := ct.NewStream(context.Background(), callHdr) s, err := ct.NewStream(context.Background(), callHdr)
if err != nil { if err != nil {
break break
} }
if err := ct.Write(s, expectedRequest, &Options{Last: true, Delay: false}); err != nil { if err := ct.Write(s, d, &Options{Last: true, Delay: false}); err != nil {
break break
} }
} }
@ -732,7 +739,7 @@ func TestEncodingRequiredStatus(t *testing.T) {
Last: true, Last: true,
Delay: false, Delay: false,
} }
if err := ct.Write(s, expectedRequest, &opts); err != nil || err == io.EOF { if err := ct.Write(s, expectedRequest, &opts); err != nil && err != io.EOF {
t.Fatalf("Failed to write the request: %v", err) t.Fatalf("Failed to write the request: %v", err)
} }
p := make([]byte, http2MaxFrameLen) p := make([]byte, http2MaxFrameLen)