fix flake: Router test "address already in use"
@@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strings"
+ "sync"
"github.com/golang/glog"
"golang.org/x/net/websocket"
@@ -111,6 +112,7 @@ type TestHttpService struct {
NamespaceListResponse string
listeners []net.Listener
+ wg sync.WaitGroup
}
const (
@@ -290,21 +292,16 @@ func (s *TestHttpService) handleWebSocket(ws *websocket.Conn) {
// Stop stops the service by closing any registered listeners
func (s *TestHttpService) Stop() {
- if s.listeners != nil && len(s.listeners) > 0 {
for _, l := range s.listeners {
- if l != nil {
glog.Infof("Stopping listener at %s\n", l.Addr().String())
l.Close()
}
- }
- }
+ s.wg.Wait()
}
// Start will start the http service to simulate the master and client urls. It sets up the appropriate watch
// endpoints and serves the secure and non-secure traffic.
func (s *TestHttpService) Start() error {
- s.listeners = make([]net.Listener, 3)
-
if err := s.startMaster(); err != nil {
return err
}
@@ -424,12 +421,13 @@ func (s *TestHttpService) startServing(addr string, handler http.Handler) error
glog.Infof("Started, serving at %s\n", listener.Addr().String())
+ s.wg.Add(1)
go func() {
err := http.Serve(listener, handler)
-
- if err != nil {
+ if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
glog.Errorf("HTTP server failed: %v", err)
}
+ s.wg.Done()
}()
return nil
@@ -456,10 +454,13 @@ func (s *TestHttpService) startServingTLS(addr string, cert []byte, key []byte,
s.listeners = append(s.listeners, listener)
glog.Infof("Started, serving TLS at %s\n", listener.Addr().String())
+ s.wg.Add(1)
go func() {
- if err := http.Serve(listener, handler); err != nil {
+ err := http.Serve(listener, handler)
+ if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
glog.Errorf("HTTPS server failed: %v", err)
}
+ s.wg.Done()
}()
return nil
