1
0
mirror of https://github.com/golang/go synced 2024-11-19 23:24:45 -07:00

http: fix racy test with a simpler version

This was seen breaking the slow arm5 builder:
http://godashboard.appspot.com/log/ec54ee302a70c07093e8f5c47454b3eb48768b77e1d8cc7943d8951f8b6696be

The test was unnecessarily complex and didn't benefit from
using httptest.Server. That just got in the way.

R=rsc
CC=golang-dev
https://golang.org/cl/4439088
This commit is contained in:
Brad Fitzpatrick 2011-05-02 13:01:36 -07:00
parent 623e7de187
commit f279a93929

View File

@ -618,49 +618,29 @@ func TestServerExpect(t *testing.T) {
}
func TestServerConsumesRequestBody(t *testing.T) {
log := make(chan string, 100)
conn := new(testConn)
body := strings.Repeat("x", 1<<20)
conn.readBuf.Write([]byte(fmt.Sprintf(
"POST / HTTP/1.1\r\n"+
"Host: test\r\n"+
"Content-Length: %d\r\n"+
"\r\n",len(body))))
conn.readBuf.Write([]byte(body))
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
log <- "got_request"
w.WriteHeader(StatusOK)
log <- "wrote_header"
done := make(chan bool)
ls := &oneConnListener{conn}
go Serve(ls, HandlerFunc(func(rw ResponseWriter, req *Request) {
if conn.readBuf.Len() < len(body)/2 {
t.Errorf("on request, read buffer length is %d; expected about 1MB", conn.readBuf.Len())
}
rw.WriteHeader(200)
if g, e := conn.readBuf.Len(), 0; g != e {
t.Errorf("after WriteHeader, read buffer length is %d; want %d", g, e)
}
done <- true
}))
defer ts.Close()
conn, err := net.Dial("tcp", ts.Listener.Addr().String())
if err != nil {
t.Fatalf("Dial: %v", err)
}
defer conn.Close()
bufr := bufio.NewReader(conn)
gotres := make(chan bool)
go func() {
line, err := bufr.ReadString('\n')
if err != nil {
t.Fatal(err)
}
log <- line
gotres <- true
}()
size := 1 << 20
log <- "writing_request"
fmt.Fprintf(conn, "POST / HTTP/1.0\r\nContent-Length: %d\r\n\r\n", size)
time.Sleep(25e6) // give server chance to misbehave & speak out of turn
log <- "slept_after_req_headers"
conn.Write([]byte(strings.Repeat("a", size)))
<-gotres
expected := []string{
"writing_request", "got_request",
"slept_after_req_headers", "wrote_header",
"HTTP/1.0 200 OK\r\n"}
for step, e := range expected {
if g := <-log; e != g {
t.Errorf("on step %d expected %q, got %q", step, e, g)
}
}
<-done
}
func TestTimeoutHandler(t *testing.T) {