summaryrefslogtreecommitdiff
path: root/libgo/go/net/http/httptest
diff options
context:
space:
mode:
authorian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2012-03-06 17:57:23 +0000
committerian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2012-03-06 17:57:23 +0000
commit220a902afa4e096172926d498e1efac23e80deb7 (patch)
tree4ce83ca433796a728e9fdd00af105bce158532b5 /libgo/go/net/http/httptest
parent506056fd6ecd06499e2ee7f6e37dbd5fbf7f4de6 (diff)
downloadgcc-220a902afa4e096172926d498e1efac23e80deb7.tar.gz
libgo: Update to weekly.2012-03-04 release.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@185010 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/net/http/httptest')
-rw-r--r--libgo/go/net/http/httptest/server.go36
1 files changed, 35 insertions, 1 deletions
diff --git a/libgo/go/net/http/httptest/server.go b/libgo/go/net/http/httptest/server.go
index 8d911f7575b..57cf0c9417d 100644
--- a/libgo/go/net/http/httptest/server.go
+++ b/libgo/go/net/http/httptest/server.go
@@ -13,6 +13,7 @@ import (
"net"
"net/http"
"os"
+ "sync"
)
// A Server is an HTTP server listening on a system-chosen port on the
@@ -25,6 +26,10 @@ type Server struct {
// Config may be changed after calling NewUnstartedServer and
// before Start or StartTLS.
Config *http.Server
+
+ // wg counts the number of outstanding HTTP requests on this server.
+ // Close blocks until all requests are finished.
+ wg sync.WaitGroup
}
// historyListener keeps track of all connections that it's ever
@@ -93,6 +98,7 @@ func (s *Server) Start() {
}
s.Listener = &historyListener{s.Listener, make([]net.Conn, 0)}
s.URL = "http://" + s.Listener.Addr().String()
+ s.wrapHandler()
go s.Config.Serve(s.Listener)
if *serve != "" {
fmt.Fprintln(os.Stderr, "httptest: serving on", s.URL)
@@ -118,9 +124,21 @@ func (s *Server) StartTLS() {
s.Listener = &historyListener{tlsListener, make([]net.Conn, 0)}
s.URL = "https://" + s.Listener.Addr().String()
+ s.wrapHandler()
go s.Config.Serve(s.Listener)
}
+func (s *Server) wrapHandler() {
+ h := s.Config.Handler
+ if h == nil {
+ h = http.DefaultServeMux
+ }
+ s.Config.Handler = &waitGroupHandler{
+ s: s,
+ h: h,
+ }
+}
+
// NewTLSServer starts and returns a new Server using TLS.
// The caller should call Close when finished, to shut it down.
func NewTLSServer(handler http.Handler) *Server {
@@ -129,9 +147,11 @@ func NewTLSServer(handler http.Handler) *Server {
return ts
}
-// Close shuts down the server.
+// Close shuts down the server and blocks until all outstanding
+// requests on this server have completed.
func (s *Server) Close() {
s.Listener.Close()
+ s.wg.Wait()
}
// CloseClientConnections closes any currently open HTTP connections
@@ -146,6 +166,20 @@ func (s *Server) CloseClientConnections() {
}
}
+// waitGroupHandler wraps a handler, incrementing and decrementing a
+// sync.WaitGroup on each request, to enable Server.Close to block
+// until outstanding requests are finished.
+type waitGroupHandler struct {
+ s *Server
+ h http.Handler // non-nil
+}
+
+func (h *waitGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ h.s.wg.Add(1)
+ defer h.s.wg.Done() // a defer, in case ServeHTTP below panics
+ h.h.ServeHTTP(w, r)
+}
+
// localhostCert is a PEM-encoded TLS cert with SAN DNS names
// "127.0.0.1" and "[::1]", expiring at the last second of 2049 (the end
// of ASN.1 time).