runtime/race: improve TestNoRaceIOHttp test

TestNoRaceIOHttp does all kinds of bad things:
1. Binds to a fixed port, so concurrent tests fail.
2. Registers HTTP handler multiple times, so repeated tests fail.
3. Relies on sleep to wait for listen.

Fix all of that.

Change-Id: I1210b7797ef5e92465b37dc407246d92a2a24fe8
Reviewed-on: https://go-review.googlesource.com/19953
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Dmitry Vyukov 2016-02-26 13:02:42 +01:00
parent 102cf2ae03
commit 6dfba5c7ce
1 changed files with 22 additions and 15 deletions

View File

@ -7,9 +7,11 @@ package race_test
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
@ -41,29 +43,34 @@ func TestNoRaceIOFile(t *testing.T) {
_ = x
}
var (
regHandler sync.Once
handlerData int
)
func TestNoRaceIOHttp(t *testing.T) {
x := 0
go func() {
regHandler.Do(func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
x = 41
handlerData++
fmt.Fprintf(w, "test")
x = 42
handlerData++
})
err := http.ListenAndServe("127.0.0.1:23651", nil)
if err != nil {
t.Fatalf("http.ListenAndServe: %v", err)
}
}()
time.Sleep(1e7)
x = 1
_, err := http.Get("http://127.0.0.1:23651")
})
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("net.Listen: %v", err)
}
defer ln.Close()
go http.Serve(ln, nil)
handlerData++
_, err = http.Get("http://" + ln.Addr().String())
if err != nil {
t.Fatalf("http.Get: %v", err)
}
x = 2
_, err = http.Get("http://127.0.0.1:23651")
handlerData++
_, err = http.Get("http://" + ln.Addr().String())
if err != nil {
t.Fatalf("http.Get: %v", err)
}
x = 3
handlerData++
}