Fix rare 'go test -race' failure in ListenServe{Plain,TLS} (#2588)

master
Anis Elleuch 8 years ago committed by Harshavardhana
parent a3b4199e9b
commit e79d2381fc
  1. 12
      cmd/server-mux_test.go

@ -153,7 +153,7 @@ func TestServerCloseBlocking(t *testing.T) {
m.mu.Unlock() m.mu.Unlock()
} }
func TestListenAndServe(t *testing.T) { func TestListenAndServePlain(t *testing.T) {
wait := make(chan struct{}) wait := make(chan struct{})
addr := "127.0.0.1:" + strconv.Itoa(getFreePort()) addr := "127.0.0.1:" + strconv.Itoa(getFreePort())
errc := make(chan error) errc := make(chan error)
@ -171,6 +171,8 @@ func TestListenAndServe(t *testing.T) {
// Make sure we don't block by closing wait after a timeout // Make sure we don't block by closing wait after a timeout
tf := time.AfterFunc(time.Millisecond*500, func() { errc <- errors.New("Unable to connect to server") }) tf := time.AfterFunc(time.Millisecond*500, func() { errc <- errors.New("Unable to connect to server") })
wg := &sync.WaitGroup{}
wg.Add(1)
// Keep trying the server until it's accepting connections // Keep trying the server until it's accepting connections
go func() { go func() {
client := http.Client{Timeout: time.Millisecond * 10} client := http.Client{Timeout: time.Millisecond * 10}
@ -182,9 +184,12 @@ func TestListenAndServe(t *testing.T) {
} }
} }
wg.Done()
tf.Stop() // Cancel the timeout since we made a successful request tf.Stop() // Cancel the timeout since we made a successful request
}() }()
wg.Wait()
// Block until we get an error or wait closed // Block until we get an error or wait closed
select { select {
case err := <-errc: case err := <-errc:
@ -230,6 +235,8 @@ func TestListenAndServeTLS(t *testing.T) {
// Make sure we don't block by closing wait after a timeout // Make sure we don't block by closing wait after a timeout
tf := time.AfterFunc(time.Millisecond*500, func() { errc <- errors.New("Unable to connect to server") }) tf := time.AfterFunc(time.Millisecond*500, func() { errc <- errors.New("Unable to connect to server") })
wg := &sync.WaitGroup{}
wg.Add(1)
// Keep trying the server until it's accepting connections // Keep trying the server until it's accepting connections
go func() { go func() {
@ -248,9 +255,12 @@ func TestListenAndServeTLS(t *testing.T) {
} }
} }
wg.Done()
tf.Stop() // Cancel the timeout since we made a successful request tf.Stop() // Cancel the timeout since we made a successful request
}() }()
wg.Wait()
// Block until we get an error or wait closed // Block until we get an error or wait closed
select { select {
case err := <-errc: case err := <-errc:

Loading…
Cancel
Save