1
0
mirror of https://github.com/golang/go synced 2024-11-16 20:14:48 -07:00

net/http/fcgi: Add test demonstrating race condition

This commit is contained in:
Hilko Bengen 2020-12-03 02:03:01 +01:00
parent 7214ac877c
commit b3976111ae

View File

@ -11,6 +11,7 @@ import (
"net/http"
"strings"
"testing"
"time"
)
var sizeTests = []struct {
@ -395,3 +396,55 @@ func TestResponseWriterSniffsContentType(t *testing.T) {
})
}
}
type signallingNopCloser struct {
io.Reader
closed chan bool
}
func (*signallingNopCloser) Write(buf []byte) (int, error) {
return len(buf), nil
}
func (rc *signallingNopCloser) Close() error {
close(rc.closed)
return nil
}
// Test whether server properly closes connection when processing slow
// requests
func TestSlowRequest(t *testing.T) {
pr, pw := io.Pipe()
go func(w io.Writer) {
for _, buf := range [][]byte{
streamBeginTypeStdin,
makeRecord(typeStdin, 1, nil),
} {
pw.Write(buf)
time.Sleep(100 * time.Millisecond)
}
}(pw)
rc := &signallingNopCloser{pr, make(chan bool)}
handlerDone := make(chan bool)
c := newChild(rc, http.HandlerFunc(func(
w http.ResponseWriter,
r *http.Request,
) {
w.WriteHeader(200)
close(handlerDone)
}))
go c.serve()
defer c.cleanUp()
timeout := time.After(2 * time.Second)
<-handlerDone
select {
case <-rc.closed:
t.Log("FastCGI child closed connection")
case <-timeout:
t.Error("FastCGI child did not close socket after handling request")
}
}