Commit 6dfba5c7 authored by Dmitry Vyukov's avatar Dmitry Vyukov

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 102cf2ae
...@@ -7,9 +7,11 @@ package race_test ...@@ -7,9 +7,11 @@ package race_test
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"testing" "testing"
"time" "time"
) )
...@@ -41,29 +43,34 @@ func TestNoRaceIOFile(t *testing.T) { ...@@ -41,29 +43,34 @@ func TestNoRaceIOFile(t *testing.T) {
_ = x _ = x
} }
var (
regHandler sync.Once
handlerData int
)
func TestNoRaceIOHttp(t *testing.T) { func TestNoRaceIOHttp(t *testing.T) {
x := 0 regHandler.Do(func() {
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
x = 41 handlerData++
fmt.Fprintf(w, "test") fmt.Fprintf(w, "test")
x = 42 handlerData++
})
}) })
err := http.ListenAndServe("127.0.0.1:23651", nil) ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil { if err != nil {
t.Fatalf("http.ListenAndServe: %v", err) t.Fatalf("net.Listen: %v", err)
} }
}() defer ln.Close()
time.Sleep(1e7) go http.Serve(ln, nil)
x = 1 handlerData++
_, err := http.Get("http://127.0.0.1:23651") _, err = http.Get("http://" + ln.Addr().String())
if err != nil { if err != nil {
t.Fatalf("http.Get: %v", err) t.Fatalf("http.Get: %v", err)
} }
x = 2 handlerData++
_, err = http.Get("http://127.0.0.1:23651") _, err = http.Get("http://" + ln.Addr().String())
if err != nil { if err != nil {
t.Fatalf("http.Get: %v", err) t.Fatalf("http.Get: %v", err)
} }
x = 3 handlerData++
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment