1 // Copyright 2012 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.
27 func TestMain(m *testing.M) {
29 for _, file := range toRemove {
38 target map[string]buildexe
41 type buildexe struct {
46 func runTestProg(t *testing.T, binary, name string, env ...string) string {
47 testenv.MustHaveGoBuild(t)
49 exe, err := buildTestProg(t, binary)
54 cmd := testenv.CleanCmdEnv(exec.Command(exe, name))
55 cmd.Env = append(cmd.Env, env...)
59 if err := cmd.Start(); err != nil {
60 t.Fatalf("starting %s %s: %v", binary, name, err)
63 // If the process doesn't complete within 1 minute,
64 // assume it is hanging and kill it to get a stack trace.
66 done := make(chan bool)
69 // This GOARCH/GOOS test is copied from cmd/dist/test.go.
70 // TODO(iant): Have cmd/dist update the environment variable.
71 if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
74 if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
75 if sc, err := strconv.Atoi(s); err == nil {
82 case <-time.After(time.Duration(scale) * time.Minute):
87 if err := cmd.Wait(); err != nil {
88 t.Logf("%s %s exit status: %v", binary, name, err)
95 func buildTestProg(t *testing.T, binary string, flags ...string) (string, error) {
99 defer testprog.Unlock()
100 if testprog.dir == "" {
101 dir, err := ioutil.TempDir("", "go-build")
103 t.Fatalf("failed to create temp directory: %v", err)
106 toRemove = append(toRemove, dir)
109 if testprog.target == nil {
110 testprog.target = make(map[string]buildexe)
114 name += "_" + strings.Join(flags, "_")
116 target, ok := testprog.target[name]
118 return target.exe, target.err
121 exe := filepath.Join(testprog.dir, name+".exe")
122 cmd := exec.Command(testenv.GoToolPath(t), append([]string{"build", "-o", exe}, flags...)...)
123 cmd.Dir = "testdata/" + binary
124 out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
126 target.err = fmt.Errorf("building %s %v: %v\n%s", binary, flags, err, out)
127 testprog.target[name] = target
128 return "", target.err
131 testprog.target[name] = target
136 staleRuntimeOnce sync.Once // guards init of staleRuntimeErr
137 staleRuntimeErr error
140 func checkStaleRuntime(t *testing.T) {
141 staleRuntimeOnce.Do(func() {
142 // 'go run' uses the installed copy of runtime.a, which may be out of date.
143 out, err := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "list", "-f", "{{.Stale}}", "runtime")).CombinedOutput()
145 staleRuntimeErr = fmt.Errorf("failed to execute 'go list': %v\n%v", err, string(out))
148 if string(out) != "false\n" {
149 t.Logf("go list -f {{.Stale}} runtime:\n%s", out)
150 out, err := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "list", "-f", "{{.StaleReason}}", "runtime")).CombinedOutput()
152 t.Logf("go list -f {{.StaleReason}} failed: %v", err)
154 t.Logf("go list -f {{.StaleReason}} runtime:\n%s", out)
155 staleRuntimeErr = fmt.Errorf("Stale runtime.a. Run 'go install runtime'.")
158 if staleRuntimeErr != nil {
159 t.Fatal(staleRuntimeErr)
163 func testCrashHandler(t *testing.T, cgo bool) {
164 type crashTest struct {
169 output = runTestProg(t, "testprogcgo", "Crash")
171 output = runTestProg(t, "testprog", "Crash")
173 want := "main: recovered done\nnew-thread: recovered done\nsecond-new-thread: recovered done\nmain-again: recovered done\n"
175 t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
179 func TestCrashHandler(t *testing.T) {
180 testCrashHandler(t, false)
183 func testDeadlock(t *testing.T, name string) {
184 output := runTestProg(t, "testprog", name)
185 want := "fatal error: all goroutines are asleep - deadlock!\n"
186 if !strings.HasPrefix(output, want) {
187 t.Fatalf("output does not start with %q:\n%s", want, output)
191 func TestSimpleDeadlock(t *testing.T) {
192 testDeadlock(t, "SimpleDeadlock")
195 func TestInitDeadlock(t *testing.T) {
196 testDeadlock(t, "InitDeadlock")
199 func TestLockedDeadlock(t *testing.T) {
200 testDeadlock(t, "LockedDeadlock")
203 func TestLockedDeadlock2(t *testing.T) {
204 testDeadlock(t, "LockedDeadlock2")
207 func TestGoexitDeadlock(t *testing.T) {
208 output := runTestProg(t, "testprog", "GoexitDeadlock")
209 want := "no goroutines (main called runtime.Goexit) - deadlock!"
210 if !strings.Contains(output, want) {
211 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
215 func TestStackOverflow(t *testing.T) {
216 output := runTestProg(t, "testprog", "StackOverflow")
217 want := "runtime: goroutine stack exceeds 1474560-byte limit\nfatal error: stack overflow"
218 if !strings.HasPrefix(output, want) {
219 t.Fatalf("output does not start with %q:\n%s", want, output)
223 func TestThreadExhaustion(t *testing.T) {
224 output := runTestProg(t, "testprog", "ThreadExhaustion")
225 want := "runtime: program exceeds 10-thread limit\nfatal error: thread exhaustion"
226 if !strings.HasPrefix(output, want) {
227 t.Fatalf("output does not start with %q:\n%s", want, output)
231 func TestRecursivePanic(t *testing.T) {
232 output := runTestProg(t, "testprog", "RecursivePanic")
237 if !strings.HasPrefix(output, want) {
238 t.Fatalf("output does not start with %q:\n%s", want, output)
243 func TestGoexitCrash(t *testing.T) {
244 output := runTestProg(t, "testprog", "GoexitExit")
245 want := "no goroutines (main called runtime.Goexit) - deadlock!"
246 if !strings.Contains(output, want) {
247 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
251 func TestGoexitDefer(t *testing.T) {
252 c := make(chan struct{})
257 t.Errorf("non-nil recover during Goexit")
263 // Note: if the defer fails to run, we will get a deadlock here
267 func TestGoNil(t *testing.T) {
268 output := runTestProg(t, "testprog", "GoNil")
269 want := "go of nil func value"
270 if !strings.Contains(output, want) {
271 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
275 func TestMainGoroutineID(t *testing.T) {
276 output := runTestProg(t, "testprog", "MainGoroutineID")
277 want := "panic: test\n\ngoroutine 1 [running]:\n"
278 if !strings.HasPrefix(output, want) {
279 t.Fatalf("output does not start with %q:\n%s", want, output)
283 func TestNoHelperGoroutines(t *testing.T) {
284 output := runTestProg(t, "testprog", "NoHelperGoroutines")
285 matches := regexp.MustCompile(`goroutine [0-9]+ \[`).FindAllStringSubmatch(output, -1)
286 if len(matches) != 1 || matches[0][0] != "goroutine 1 [" {
287 t.Fatalf("want to see only goroutine 1, see:\n%s", output)
291 func TestBreakpoint(t *testing.T) {
292 output := runTestProg(t, "testprog", "Breakpoint")
293 // If runtime.Breakpoint() is inlined, then the stack trace prints
294 // "runtime.Breakpoint(...)" instead of "runtime.Breakpoint()".
295 want := "runtime.Breakpoint("
296 if !strings.Contains(output, want) {
297 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
301 func TestGoexitInPanic(t *testing.T) {
302 // see issue 8774: this code used to trigger an infinite recursion
303 output := runTestProg(t, "testprog", "GoexitInPanic")
304 want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
305 if !strings.HasPrefix(output, want) {
306 t.Fatalf("output does not start with %q:\n%s", want, output)
310 // Issue 14965: Runtime panics should be of type runtime.Error
311 func TestRuntimePanicWithRuntimeError(t *testing.T) {
312 testCases := [...]func(){
314 var m map[uint64]bool
318 ch := make(chan struct{})
323 var ch = make(chan struct{})
328 var s = make([]int, 2)
333 _ = make(chan bool, n)
336 close((chan bool)(nil))
340 for i, fn := range testCases {
341 got := panicValue(fn)
342 if _, ok := got.(runtime.Error); !ok {
343 t.Errorf("test #%d: recovered value %v(type %T) does not implement runtime.Error", i, got, got)
348 func panicValue(fn func()) (recovered interface{}) {
350 recovered = recover()
356 func TestPanicAfterGoexit(t *testing.T) {
357 // an uncaught panic should still work after goexit
358 output := runTestProg(t, "testprog", "PanicAfterGoexit")
359 want := "panic: hello"
360 if !strings.HasPrefix(output, want) {
361 t.Fatalf("output does not start with %q:\n%s", want, output)
365 func TestRecoveredPanicAfterGoexit(t *testing.T) {
366 output := runTestProg(t, "testprog", "RecoveredPanicAfterGoexit")
367 want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
368 if !strings.HasPrefix(output, want) {
369 t.Fatalf("output does not start with %q:\n%s", want, output)
373 func TestRecoverBeforePanicAfterGoexit(t *testing.T) {
374 // 1. defer a function that recovers
375 // 2. defer a function that panics
377 // Goexit should run the #2 defer. Its panic
378 // should be caught by the #1 defer, and execution
379 // should resume in the caller. Like the Goexit
393 func TestNetpollDeadlock(t *testing.T) {
395 output := runTestProg(t, "testprognet", "NetpollDeadlock")
397 if !strings.HasSuffix(output, want) {
398 t.Fatalf("output does not start with %q:\n%s", want, output)
402 func TestPanicTraceback(t *testing.T) {
404 output := runTestProg(t, "testprog", "PanicTraceback")
405 want := "panic: hello"
406 if !strings.HasPrefix(output, want) {
407 t.Fatalf("output does not start with %q:\n%s", want, output)
410 // Check functions in the traceback.
411 fns := []string{"main.pt1.func1", "panic", "main.pt2.func1", "panic", "main.pt2", "main.pt1"}
412 for _, fn := range fns {
413 re := regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(fn) + `\(.*\n`)
414 idx := re.FindStringIndex(output)
416 t.Fatalf("expected %q function in traceback:\n%s", fn, output)
418 output = output[idx[1]:]
422 func testPanicDeadlock(t *testing.T, name string, want string) {
424 output := runTestProg(t, "testprog", name)
425 if !strings.HasPrefix(output, want) {
426 t.Fatalf("output does not start with %q:\n%s", want, output)
430 func TestPanicDeadlockGosched(t *testing.T) {
431 testPanicDeadlock(t, "GoschedInPanic", "panic: errorThatGosched\n\n")
434 func TestPanicDeadlockSyscall(t *testing.T) {
435 testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
438 func TestPanicLoop(t *testing.T) {
439 output := runTestProg(t, "testprog", "PanicLoop")
440 if want := "panic while printing panic value"; !strings.Contains(output, want) {
441 t.Errorf("output does not contain %q:\n%s", want, output)
445 func TestMemPprof(t *testing.T) {
446 testenv.MustHaveGoRun(t)
448 exe, err := buildTestProg(t, "testprog")
453 got, err := testenv.CleanCmdEnv(exec.Command(exe, "MemProf")).CombinedOutput()
457 fn := strings.TrimSpace(string(got))
460 for try := 0; try < 2; try++ {
461 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-alloc_space", "-top"))
462 // Check that pprof works both with and without explicit executable on command line.
464 cmd.Args = append(cmd.Args, exe, fn)
466 cmd.Args = append(cmd.Args, fn)
469 for i, e := range cmd.Env {
470 if strings.HasPrefix(e, "PPROF_TMPDIR=") {
471 cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
477 cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
480 top, err := cmd.CombinedOutput()
481 t.Logf("%s:\n%s", cmd.Args, top)
484 } else if !bytes.Contains(top, []byte("MemProf")) {
485 t.Error("missing MemProf in pprof output")
490 var concurrentMapTest = flag.Bool("run_concurrent_map_tests", false, "also run flaky concurrent map tests")
492 func TestConcurrentMapWrites(t *testing.T) {
493 if !*concurrentMapTest {
494 t.Skip("skipping without -run_concurrent_map_tests")
496 testenv.MustHaveGoRun(t)
497 output := runTestProg(t, "testprog", "concurrentMapWrites")
498 want := "fatal error: concurrent map writes"
499 if !strings.HasPrefix(output, want) {
500 t.Fatalf("output does not start with %q:\n%s", want, output)
503 func TestConcurrentMapReadWrite(t *testing.T) {
504 if !*concurrentMapTest {
505 t.Skip("skipping without -run_concurrent_map_tests")
507 testenv.MustHaveGoRun(t)
508 output := runTestProg(t, "testprog", "concurrentMapReadWrite")
509 want := "fatal error: concurrent map read and map write"
510 if !strings.HasPrefix(output, want) {
511 t.Fatalf("output does not start with %q:\n%s", want, output)
514 func TestConcurrentMapIterateWrite(t *testing.T) {
515 if !*concurrentMapTest {
516 t.Skip("skipping without -run_concurrent_map_tests")
518 testenv.MustHaveGoRun(t)
519 output := runTestProg(t, "testprog", "concurrentMapIterateWrite")
520 want := "fatal error: concurrent map iteration and map write"
521 if !strings.HasPrefix(output, want) {
522 t.Fatalf("output does not start with %q:\n%s", want, output)
530 func (p *point) negate() {
535 // Test for issue #10152.
536 func TestPanicInlined(t *testing.T) {
540 t.Fatalf("recover failed")
542 buf := make([]byte, 2048)
543 n := runtime.Stack(buf, false)
545 if !bytes.Contains(buf, []byte("(*point).negate(")) {
546 t.Fatalf("expecting stack trace to contain call to (*point).negate()")
554 // Test for issues #3934 and #20018.
555 // We want to delay exiting until a panic print is complete.
556 func TestPanicRace(t *testing.T) {
557 testenv.MustHaveGoRun(t)
559 exe, err := buildTestProg(t, "testprog")
564 // The test is intentionally racy, and in my testing does not
565 // produce the expected output about 0.05% of the time.
566 // So run the program in a loop and only fail the test if we
567 // get the wrong output ten times in a row.
570 for i := 0; i < tries; i++ {
571 got, err := testenv.CleanCmdEnv(exec.Command(exe, "PanicRace")).CombinedOutput()
573 t.Logf("try %d: program exited successfully, should have failed", i+1)
578 t.Logf("try %d:\n", i+1)
587 for _, want := range wants {
588 if !bytes.Contains(got, []byte(want)) {
589 t.Logf("did not find expected string %q", want)
594 // Test generated expected output.
597 t.Errorf("test ran %d times without producing expected output", tries)