Skip to content

Commit 8d5ff2e

Browse files
committed
runtime: move test programs out of source code, coalesce
Now there are just three programs to compile instead of many, and repeated tests can reuse the compilation result instead of rebuilding it. Combined, these changes reduce the time spent testing runtime during all.bash on my laptop from about 60 to about 30 seconds. (All.bash itself runs in 5½ minutes.) For #10571. Change-Id: Ie2c1798b847f1a635a860d11dcdab14375319ae9 Reviewed-on: https://go-review.googlesource.com/18085 Reviewed-by: Austin Clements <austin@google.com> Run-TryBot: Austin Clements <austin@google.com>
1 parent a699320 commit 8d5ff2e

24 files changed

+991
-817
lines changed

src/runtime/crash_cgo_test.go

+12-378
Large diffs are not rendered by default.

src/runtime/crash_test.go

+81-318
Large diffs are not rendered by default.

src/runtime/gc_test.go

+1-47
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,13 @@ func TestGcSys(t *testing.T) {
1919
if os.Getenv("GOGC") == "off" {
2020
t.Skip("skipping test; GOGC=off in environment")
2121
}
22-
data := struct{ Short bool }{testing.Short()}
23-
got := executeTest(t, testGCSysSource, &data)
22+
got := runTestProg(t, "testprog", "GCSys")
2423
want := "OK\n"
2524
if got != want {
2625
t.Fatalf("expected %q, but got %q", want, got)
2726
}
2827
}
2928

30-
const testGCSysSource = `
31-
package main
32-
33-
import (
34-
"fmt"
35-
"runtime"
36-
)
37-
38-
func main() {
39-
runtime.GOMAXPROCS(1)
40-
memstats := new(runtime.MemStats)
41-
runtime.GC()
42-
runtime.ReadMemStats(memstats)
43-
sys := memstats.Sys
44-
45-
runtime.MemProfileRate = 0 // disable profiler
46-
47-
itercount := 1000000
48-
{{if .Short}}
49-
itercount = 100000
50-
{{end}}
51-
for i := 0; i < itercount; i++ {
52-
workthegc()
53-
}
54-
55-
// Should only be using a few MB.
56-
// We allocated 100 MB or (if not short) 1 GB.
57-
runtime.ReadMemStats(memstats)
58-
if sys > memstats.Sys {
59-
sys = 0
60-
} else {
61-
sys = memstats.Sys - sys
62-
}
63-
if sys > 16<<20 {
64-
fmt.Printf("using too much memory: %d bytes\n", sys)
65-
return
66-
}
67-
fmt.Printf("OK\n")
68-
}
69-
70-
func workthegc() []byte {
71-
return make([]byte, 1029)
72-
}
73-
`
74-
7529
func TestGcDeepNesting(t *testing.T) {
7630
type T [2][2][2][2][2][2][2][2][2][2]*int
7731
a := new(T)

src/runtime/proc_test.go

+1-36
Original file line numberDiff line numberDiff line change
@@ -329,48 +329,13 @@ func TestPreemptionGC(t *testing.T) {
329329
}
330330

331331
func TestGCFairness(t *testing.T) {
332-
output := executeTest(t, testGCFairnessSource, nil)
332+
output := runTestProg(t, "testprog", "GCFairness")
333333
want := "OK\n"
334334
if output != want {
335335
t.Fatalf("want %s, got %s\n", want, output)
336336
}
337337
}
338338

339-
const testGCFairnessSource = `
340-
package main
341-
342-
import (
343-
"fmt"
344-
"os"
345-
"runtime"
346-
"time"
347-
)
348-
349-
func main() {
350-
runtime.GOMAXPROCS(1)
351-
f, err := os.Open("/dev/null")
352-
if os.IsNotExist(err) {
353-
// This test tests what it is intended to test only if writes are fast.
354-
// If there is no /dev/null, we just don't execute the test.
355-
fmt.Println("OK")
356-
return
357-
}
358-
if err != nil {
359-
fmt.Println(err)
360-
os.Exit(1)
361-
}
362-
for i := 0; i < 2; i++ {
363-
go func() {
364-
for {
365-
f.Write([]byte("."))
366-
}
367-
}()
368-
}
369-
time.Sleep(10 * time.Millisecond)
370-
fmt.Println("OK")
371-
}
372-
`
373-
374339
func TestPingPongHog(t *testing.T) {
375340
if testing.Short() {
376341
t.Skip("skipping in -short mode")

src/runtime/string_test.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,14 @@ func TestStringW(t *testing.T) {
125125
}
126126

127127
func TestLargeStringConcat(t *testing.T) {
128-
output := executeTest(t, largeStringConcatSource, nil)
128+
output := runTestProg(t, "testprog", "stringconcat")
129129
want := "panic: " + strings.Repeat("0", 1<<10) + strings.Repeat("1", 1<<10) +
130130
strings.Repeat("2", 1<<10) + strings.Repeat("3", 1<<10)
131131
if !strings.HasPrefix(output, want) {
132132
t.Fatalf("output does not start with %q:\n%s", want, output)
133133
}
134134
}
135135

136-
var largeStringConcatSource = `
137-
package main
138-
import "strings"
139-
func main() {
140-
s0 := strings.Repeat("0", 1<<10)
141-
s1 := strings.Repeat("1", 1<<10)
142-
s2 := strings.Repeat("2", 1<<10)
143-
s3 := strings.Repeat("3", 1<<10)
144-
s := s0 + s1 + s2 + s3
145-
panic(s)
146-
}
147-
`
148-
149136
func TestGostringnocopy(t *testing.T) {
150137
max := *runtime.Maxstring
151138
b := make([]byte, max+10)

src/runtime/syscall_windows_test.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ func TestOutputDebugString(t *testing.T) {
500500
}
501501

502502
func TestRaiseException(t *testing.T) {
503-
o := executeTest(t, raiseExceptionSource, nil)
503+
o := runTestProg(t, "testprog", "RaiseException")
504504
if strings.Contains(o, "RaiseException should not return") {
505505
t.Fatalf("RaiseException did not crash program: %v", o)
506506
}
@@ -509,35 +509,13 @@ func TestRaiseException(t *testing.T) {
509509
}
510510
}
511511

512-
const raiseExceptionSource = `
513-
package main
514-
import "syscall"
515-
func main() {
516-
const EXCEPTION_NONCONTINUABLE = 1
517-
mod := syscall.MustLoadDLL("kernel32.dll")
518-
proc := mod.MustFindProc("RaiseException")
519-
proc.Call(0xbad, EXCEPTION_NONCONTINUABLE, 0, 0)
520-
println("RaiseException should not return")
521-
}
522-
`
523-
524512
func TestZeroDivisionException(t *testing.T) {
525-
o := executeTest(t, zeroDivisionExceptionSource, nil)
513+
o := runTestProg(t, "testprog", "ZeroDivisionException")
526514
if !strings.Contains(o, "panic: runtime error: integer divide by zero") {
527515
t.Fatalf("No stack trace: %v", o)
528516
}
529517
}
530518

531-
const zeroDivisionExceptionSource = `
532-
package main
533-
func main() {
534-
x := 1
535-
y := 0
536-
z := x / y
537-
println(z)
538-
}
539-
`
540-
541519
func TestWERDialogue(t *testing.T) {
542520
if os.Getenv("TESTING_WER_DIALOGUE") == "1" {
543521
defer os.Exit(0)
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
"runtime"
10+
)
11+
12+
func init() {
13+
register("Crash", Crash)
14+
}
15+
16+
func test(name string) {
17+
defer func() {
18+
if x := recover(); x != nil {
19+
fmt.Printf(" recovered")
20+
}
21+
fmt.Printf(" done\n")
22+
}()
23+
fmt.Printf("%s:", name)
24+
var s *string
25+
_ = *s
26+
fmt.Print("SHOULD NOT BE HERE")
27+
}
28+
29+
func testInNewThread(name string) {
30+
c := make(chan bool)
31+
go func() {
32+
runtime.LockOSThread()
33+
test(name)
34+
c <- true
35+
}()
36+
<-c
37+
}
38+
39+
func Crash() {
40+
runtime.LockOSThread()
41+
test("main")
42+
testInNewThread("new-thread")
43+
testInNewThread("second-new-thread")
44+
test("main-again")
45+
}

0 commit comments

Comments
 (0)