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
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)
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("http.ListenAndServe: %v", err)
t.Fatalf("net.Listen: %v", err)
}
}()
time.Sleep(1e7)
x = 1
_, err := http.Get("http://127.0.0.1:23651")
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++
}
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