Commit 75104237 authored by Rémy Oudompheng's avatar Rémy Oudompheng

all: make tests able to run multiple times.

It is now possible to run "go test -cpu=1,2,4 std"
successfully.

Fixes #3185.

R=golang-dev, dave, minux.ma, bradfitz
CC=golang-dev
https://golang.org/cl/7196052
parent c48f7d6b
...@@ -52,6 +52,14 @@ func dotest() bool { ...@@ -52,6 +52,14 @@ func dotest() bool {
return true return true
} }
func endtest() {
if pclineTempDir != "" {
os.RemoveAll(pclineTempDir)
pclineTempDir = ""
pclinetestBinary = ""
}
}
func getTable(t *testing.T) *Table { func getTable(t *testing.T) *Table {
f, tab := crack(os.Args[0], t) f, tab := crack(os.Args[0], t)
f.Close() f.Close()
...@@ -95,6 +103,7 @@ func TestLineFromAline(t *testing.T) { ...@@ -95,6 +103,7 @@ func TestLineFromAline(t *testing.T) {
if !dotest() { if !dotest() {
return return
} }
defer endtest()
tab := getTable(t) tab := getTable(t)
...@@ -142,6 +151,7 @@ func TestLineAline(t *testing.T) { ...@@ -142,6 +151,7 @@ func TestLineAline(t *testing.T) {
if !dotest() { if !dotest() {
return return
} }
defer endtest()
tab := getTable(t) tab := getTable(t)
...@@ -183,7 +193,7 @@ func TestPCLine(t *testing.T) { ...@@ -183,7 +193,7 @@ func TestPCLine(t *testing.T) {
if !dotest() { if !dotest() {
return return
} }
defer os.RemoveAll(pclineTempDir) defer endtest()
f, tab := crack(pclinetestBinary, t) f, tab := crack(pclinetestBinary, t)
text := f.Section(".text") text := f.Section(".text")
......
...@@ -89,7 +89,10 @@ var convLargeTests = []convertTest{ ...@@ -89,7 +89,10 @@ var convLargeTests = []convertTest{
func TestConvertLarge(t *testing.T) { func TestConvertLarge(t *testing.T) {
for i, tt := range convLargeTests { for i, tt := range convLargeTests {
e := &entry{elems: tt.in} e := new(entry)
for _, ce := range tt.in {
e.elems = append(e.elems, makeRawCE(ce.w, ce.ccc))
}
elems, err := convertLargeWeights(e.elems) elems, err := convertLargeWeights(e.elems)
if tt.err { if tt.err {
if err == nil { if err == nil {
......
...@@ -18,6 +18,7 @@ func RemoveAll() { ...@@ -18,6 +18,7 @@ func RemoveAll() {
} }
func TestInt(t *testing.T) { func TestInt(t *testing.T) {
RemoveAll()
reqs := NewInt("requests") reqs := NewInt("requests")
if reqs.i != 0 { if reqs.i != 0 {
t.Errorf("reqs.i = %v, want 0", reqs.i) t.Errorf("reqs.i = %v, want 0", reqs.i)
...@@ -43,6 +44,7 @@ func TestInt(t *testing.T) { ...@@ -43,6 +44,7 @@ func TestInt(t *testing.T) {
} }
func TestFloat(t *testing.T) { func TestFloat(t *testing.T) {
RemoveAll()
reqs := NewFloat("requests-float") reqs := NewFloat("requests-float")
if reqs.f != 0.0 { if reqs.f != 0.0 {
t.Errorf("reqs.f = %v, want 0", reqs.f) t.Errorf("reqs.f = %v, want 0", reqs.f)
...@@ -68,6 +70,7 @@ func TestFloat(t *testing.T) { ...@@ -68,6 +70,7 @@ func TestFloat(t *testing.T) {
} }
func TestString(t *testing.T) { func TestString(t *testing.T) {
RemoveAll()
name := NewString("my-name") name := NewString("my-name")
if name.s != "" { if name.s != "" {
t.Errorf("name.s = %q, want \"\"", name.s) t.Errorf("name.s = %q, want \"\"", name.s)
...@@ -84,6 +87,7 @@ func TestString(t *testing.T) { ...@@ -84,6 +87,7 @@ func TestString(t *testing.T) {
} }
func TestMapCounter(t *testing.T) { func TestMapCounter(t *testing.T) {
RemoveAll()
colors := NewMap("bike-shed-colors") colors := NewMap("bike-shed-colors")
colors.Add("red", 1) colors.Add("red", 1)
...@@ -123,6 +127,7 @@ func TestMapCounter(t *testing.T) { ...@@ -123,6 +127,7 @@ func TestMapCounter(t *testing.T) {
} }
func TestFunc(t *testing.T) { func TestFunc(t *testing.T) {
RemoveAll()
var x interface{} = []string{"a", "b"} var x interface{} = []string{"a", "b"}
f := Func(func() interface{} { return x }) f := Func(func() interface{} { return x })
if s, exp := f.String(), `["a","b"]`; s != exp { if s, exp := f.String(), `["a","b"]`; s != exp {
......
...@@ -15,17 +15,6 @@ import ( ...@@ -15,17 +15,6 @@ import (
"time" "time"
) )
var (
test_bool = Bool("test_bool", false, "bool value")
test_int = Int("test_int", 0, "int value")
test_int64 = Int64("test_int64", 0, "int64 value")
test_uint = Uint("test_uint", 0, "uint value")
test_uint64 = Uint64("test_uint64", 0, "uint64 value")
test_string = String("test_string", "0", "string value")
test_float64 = Float64("test_float64", 0, "float64 value")
test_duration = Duration("test_duration", 0, "time.Duration value")
)
func boolString(s string) string { func boolString(s string) string {
if s == "0" { if s == "0" {
return "false" return "false"
...@@ -34,6 +23,16 @@ func boolString(s string) string { ...@@ -34,6 +23,16 @@ func boolString(s string) string {
} }
func TestEverything(t *testing.T) { func TestEverything(t *testing.T) {
ResetForTesting(nil)
Bool("test_bool", false, "bool value")
Int("test_int", 0, "int value")
Int64("test_int64", 0, "int64 value")
Uint("test_uint", 0, "uint value")
Uint64("test_uint64", 0, "uint64 value")
String("test_string", "0", "string value")
Float64("test_float64", 0, "float64 value")
Duration("test_duration", 0, "time.Duration value")
m := make(map[string]*Flag) m := make(map[string]*Flag)
desired := "0" desired := "0"
visitor := func(f *Flag) { visitor := func(f *Flag) {
......
...@@ -34,9 +34,11 @@ import ( ...@@ -34,9 +34,11 @@ import (
const testdata = "testdata" const testdata = "testdata"
var fsetErrs *token.FileSet
// getFile assumes that each filename occurs at most once // getFile assumes that each filename occurs at most once
func getFile(filename string) (file *token.File) { func getFile(filename string) (file *token.File) {
fset.Iterate(func(f *token.File) bool { fsetErrs.Iterate(func(f *token.File) bool {
if f.Name() == filename { if f.Name() == filename {
if file != nil { if file != nil {
panic(filename + " used multiple times") panic(filename + " used multiple times")
...@@ -125,7 +127,7 @@ func compareErrors(t *testing.T, expected map[token.Pos]string, found scanner.Er ...@@ -125,7 +127,7 @@ func compareErrors(t *testing.T, expected map[token.Pos]string, found scanner.Er
if len(expected) > 0 { if len(expected) > 0 {
t.Errorf("%d errors not reported:", len(expected)) t.Errorf("%d errors not reported:", len(expected))
for pos, msg := range expected { for pos, msg := range expected {
t.Errorf("%s: %s\n", fset.Position(pos), msg) t.Errorf("%s: %s\n", fsetErrs.Position(pos), msg)
} }
} }
} }
...@@ -137,7 +139,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) { ...@@ -137,7 +139,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) {
return return
} }
_, err = ParseFile(fset, filename, src, DeclarationErrors) _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors)
found, ok := err.(scanner.ErrorList) found, ok := err.(scanner.ErrorList)
if err != nil && !ok { if err != nil && !ok {
t.Error(err) t.Error(err)
...@@ -153,6 +155,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) { ...@@ -153,6 +155,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) {
} }
func TestErrors(t *testing.T) { func TestErrors(t *testing.T) {
fsetErrs = token.NewFileSet()
list, err := ioutil.ReadDir(testdata) list, err := ioutil.ReadDir(testdata)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -230,12 +230,17 @@ func checkFiles(t *testing.T, testname string, testfiles []string) { ...@@ -230,12 +230,17 @@ func checkFiles(t *testing.T, testname string, testfiles []string) {
} }
} }
var testBuiltinsDeclared = false
func TestCheck(t *testing.T) { func TestCheck(t *testing.T) {
// Declare builtins for testing. // Declare builtins for testing.
// Not done in an init func to avoid an init race with // Not done in an init func to avoid an init race with
// the construction of the Universe var. // the construction of the Universe var.
if !testBuiltinsDeclared {
testBuiltinsDeclared = true
def(&Func{Name: "assert", Type: &builtin{_Assert, "assert", 1, false, true}}) def(&Func{Name: "assert", Type: &builtin{_Assert, "assert", 1, false, true}})
def(&Func{Name: "trace", Type: &builtin{_Trace, "trace", 0, true, true}}) def(&Func{Name: "trace", Type: &builtin{_Trace, "trace", 0, true, true}})
}
// For easy debugging w/o changing the testing code, // For easy debugging w/o changing the testing code,
// if there is a local test file, only test that file. // if there is a local test file, only test that file.
......
...@@ -15,7 +15,8 @@ type respWriteTest struct { ...@@ -15,7 +15,8 @@ type respWriteTest struct {
Raw string Raw string
} }
var respWriteTests = []respWriteTest{ func TestResponseWrite(t *testing.T) {
respWriteTests := []respWriteTest{
// HTTP/1.0, identity coding; no trailer // HTTP/1.0, identity coding; no trailer
{ {
Response{ Response{
...@@ -89,9 +90,8 @@ var respWriteTests = []respWriteTest{ ...@@ -89,9 +90,8 @@ var respWriteTests = []respWriteTest{
"Foo: Bar Baz\r\n" + "Foo: Bar Baz\r\n" +
"\r\n", "\r\n",
}, },
} }
func TestResponseWrite(t *testing.T) {
for i := range respWriteTests { for i := range respWriteTests {
tt := &respWriteTests[i] tt := &respWriteTests[i]
var braw bytes.Buffer var braw bytes.Buffer
......
...@@ -184,10 +184,11 @@ var vtests = []struct { ...@@ -184,10 +184,11 @@ var vtests = []struct {
} }
func TestHostHandlers(t *testing.T) { func TestHostHandlers(t *testing.T) {
mux := NewServeMux()
for _, h := range handlers { for _, h := range handlers {
Handle(h.pattern, stringHandler(h.msg)) mux.Handle(h.pattern, stringHandler(h.msg))
} }
ts := httptest.NewServer(nil) ts := httptest.NewServer(mux)
defer ts.Close() defer ts.Close()
conn, err := net.Dial("tcp", ts.Listener.Addr().String()) conn, err := net.Dial("tcp", ts.Listener.Addr().String())
......
...@@ -144,6 +144,8 @@ func TestPipes(t *testing.T) { ...@@ -144,6 +144,8 @@ func TestPipes(t *testing.T) {
check("Wait", err) check("Wait", err)
} }
var testedAlreadyLeaked = false
func TestExtraFiles(t *testing.T) { func TestExtraFiles(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("no operating system support; skipping") t.Skip("no operating system support; skipping")
...@@ -151,12 +153,15 @@ func TestExtraFiles(t *testing.T) { ...@@ -151,12 +153,15 @@ func TestExtraFiles(t *testing.T) {
// Ensure that file descriptors have not already been leaked into // Ensure that file descriptors have not already been leaked into
// our environment. // our environment.
if !testedAlreadyLeaked {
testedAlreadyLeaked = true
for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ { for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ {
err := os.NewFile(fd, "").Close() err := os.NewFile(fd, "").Close()
if err == nil { if err == nil {
t.Logf("Something already leaked - closed fd %d", fd) t.Logf("Something already leaked - closed fd %d", fd)
} }
} }
}
// Force network usage, to verify the epoll (or whatever) fd // Force network usage, to verify the epoll (or whatever) fd
// doesn't leak to the child, // doesn't leak to the child,
......
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