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 {
51 testenv.MustHaveGoBuild(t)
53 exe, err := buildTestProg(t, binary)
58 cmd := testenv.CleanCmdEnv(exec.Command(exe, name))
59 cmd.Env = append(cmd.Env, env...)
61 cmd.Env = append(cmd.Env, "RUNTIME_TEST_SHORT=1")
66 if err := cmd.Start(); err != nil {
67 t.Fatalf("starting %s %s: %v", binary, name, err)
70 // If the process doesn't complete within 1 minute,
71 // assume it is hanging and kill it to get a stack trace.
73 done := make(chan bool)
76 // This GOARCH/GOOS test is copied from cmd/dist/test.go.
77 // TODO(iant): Have cmd/dist update the environment variable.
78 if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
81 if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
82 if sc, err := strconv.Atoi(s); err == nil {
89 case <-time.After(time.Duration(scale) * time.Minute):
94 if err := cmd.Wait(); err != nil {
95 t.Logf("%s %s exit status: %v", binary, name, err)
102 func buildTestProg(t *testing.T, binary string, flags ...string) (string, error) {
110 defer testprog.Unlock()
111 if testprog.dir == "" {
112 dir, err := ioutil.TempDir("", "go-build")
114 t.Fatalf("failed to create temp directory: %v", err)
117 toRemove = append(toRemove, dir)
120 if testprog.target == nil {
121 testprog.target = make(map[string]buildexe)
125 name += "_" + strings.Join(flags, "_")
127 target, ok := testprog.target[name]
129 return target.exe, target.err
132 exe := filepath.Join(testprog.dir, name+".exe")
133 cmd := exec.Command(testenv.GoToolPath(t), append([]string{"build", "-o", exe}, flags...)...)
134 cmd.Dir = "testdata/" + binary
135 out, err := testenv.CleanCmdEnv(cmd).CombinedOutput()
137 target.err = fmt.Errorf("building %s %v: %v\n%s", binary, flags, err, out)
138 testprog.target[name] = target
139 return "", target.err
142 testprog.target[name] = target
147 staleRuntimeOnce sync.Once // guards init of staleRuntimeErr
148 staleRuntimeErr error
151 func checkStaleRuntime(t *testing.T) {
152 staleRuntimeOnce.Do(func() {
153 // 'go run' uses the installed copy of runtime.a, which may be out of date.
154 out, err := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "list", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "-f", "{{.Stale}}", "runtime")).CombinedOutput()
156 staleRuntimeErr = fmt.Errorf("failed to execute 'go list': %v\n%v", err, string(out))
159 if string(out) != "false\n" {
160 t.Logf("go list -f {{.Stale}} runtime:\n%s", out)
161 out, err := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "list", "-gcflags=all="+os.Getenv("GO_GCFLAGS"), "-f", "{{.StaleReason}}", "runtime")).CombinedOutput()
163 t.Logf("go list -f {{.StaleReason}} failed: %v", err)
165 t.Logf("go list -f {{.StaleReason}} runtime:\n%s", out)
166 staleRuntimeErr = fmt.Errorf("Stale runtime.a. Run 'go install runtime'.")
169 if staleRuntimeErr != nil {
170 t.Fatal(staleRuntimeErr)
174 func testCrashHandler(t *testing.T, cgo bool) {
175 type crashTest struct {
180 output = runTestProg(t, "testprogcgo", "Crash")
182 output = runTestProg(t, "testprog", "Crash")
184 want := "main: recovered done\nnew-thread: recovered done\nsecond-new-thread: recovered done\nmain-again: recovered done\n"
186 t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want)
190 func TestCrashHandler(t *testing.T) {
191 testCrashHandler(t, false)
194 func testDeadlock(t *testing.T, name string) {
195 output := runTestProg(t, "testprog", name)
196 want := "fatal error: all goroutines are asleep - deadlock!\n"
197 if !strings.HasPrefix(output, want) {
198 t.Fatalf("output does not start with %q:\n%s", want, output)
202 func TestSimpleDeadlock(t *testing.T) {
203 testDeadlock(t, "SimpleDeadlock")
206 func TestInitDeadlock(t *testing.T) {
207 testDeadlock(t, "InitDeadlock")
210 func TestLockedDeadlock(t *testing.T) {
211 testDeadlock(t, "LockedDeadlock")
214 func TestLockedDeadlock2(t *testing.T) {
215 testDeadlock(t, "LockedDeadlock2")
218 func TestGoexitDeadlock(t *testing.T) {
219 output := runTestProg(t, "testprog", "GoexitDeadlock")
220 want := "no goroutines (main called runtime.Goexit) - deadlock!"
221 if !strings.Contains(output, want) {
222 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
226 func TestStackOverflow(t *testing.T) {
227 output := runTestProg(t, "testprog", "StackOverflow")
228 want := "runtime: goroutine stack exceeds 1474560-byte limit\nfatal error: stack overflow"
229 if !strings.HasPrefix(output, want) {
230 t.Fatalf("output does not start with %q:\n%s", want, output)
234 func TestThreadExhaustion(t *testing.T) {
235 output := runTestProg(t, "testprog", "ThreadExhaustion")
236 want := "runtime: program exceeds 10-thread limit\nfatal error: thread exhaustion"
237 if !strings.HasPrefix(output, want) {
238 t.Fatalf("output does not start with %q:\n%s", want, output)
242 func TestRecursivePanic(t *testing.T) {
243 output := runTestProg(t, "testprog", "RecursivePanic")
248 if !strings.HasPrefix(output, want) {
249 t.Fatalf("output does not start with %q:\n%s", want, output)
254 func TestGoexitCrash(t *testing.T) {
255 output := runTestProg(t, "testprog", "GoexitExit")
256 want := "no goroutines (main called runtime.Goexit) - deadlock!"
257 if !strings.Contains(output, want) {
258 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
262 func TestGoexitDefer(t *testing.T) {
263 c := make(chan struct{})
268 t.Errorf("non-nil recover during Goexit")
274 // Note: if the defer fails to run, we will get a deadlock here
278 func TestGoNil(t *testing.T) {
279 output := runTestProg(t, "testprog", "GoNil")
280 want := "go of nil func value"
281 if !strings.Contains(output, want) {
282 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
286 func TestMainGoroutineID(t *testing.T) {
287 output := runTestProg(t, "testprog", "MainGoroutineID")
288 want := "panic: test\n\ngoroutine 1 [running]:\n"
289 if !strings.HasPrefix(output, want) {
290 t.Fatalf("output does not start with %q:\n%s", want, output)
294 func TestNoHelperGoroutines(t *testing.T) {
295 output := runTestProg(t, "testprog", "NoHelperGoroutines")
296 matches := regexp.MustCompile(`goroutine [0-9]+ \[`).FindAllStringSubmatch(output, -1)
297 if len(matches) != 1 || matches[0][0] != "goroutine 1 [" {
298 t.Fatalf("want to see only goroutine 1, see:\n%s", output)
302 func TestBreakpoint(t *testing.T) {
303 output := runTestProg(t, "testprog", "Breakpoint")
304 // If runtime.Breakpoint() is inlined, then the stack trace prints
305 // "runtime.Breakpoint(...)" instead of "runtime.Breakpoint()".
306 want := "runtime.Breakpoint("
307 if !strings.Contains(output, want) {
308 t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want)
312 func TestGoexitInPanic(t *testing.T) {
313 // see issue 8774: this code used to trigger an infinite recursion
314 output := runTestProg(t, "testprog", "GoexitInPanic")
315 want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
316 if !strings.HasPrefix(output, want) {
317 t.Fatalf("output does not start with %q:\n%s", want, output)
321 // Issue 14965: Runtime panics should be of type runtime.Error
322 func TestRuntimePanicWithRuntimeError(t *testing.T) {
323 testCases := [...]func(){
325 var m map[uint64]bool
329 ch := make(chan struct{})
334 var ch = make(chan struct{})
339 var s = make([]int, 2)
344 _ = make(chan bool, n)
347 close((chan bool)(nil))
351 for i, fn := range testCases {
352 got := panicValue(fn)
353 if _, ok := got.(runtime.Error); !ok {
354 t.Errorf("test #%d: recovered value %v(type %T) does not implement runtime.Error", i, got, got)
359 func panicValue(fn func()) (recovered interface{}) {
361 recovered = recover()
367 func TestPanicAfterGoexit(t *testing.T) {
368 // an uncaught panic should still work after goexit
369 output := runTestProg(t, "testprog", "PanicAfterGoexit")
370 want := "panic: hello"
371 if !strings.HasPrefix(output, want) {
372 t.Fatalf("output does not start with %q:\n%s", want, output)
376 func TestRecoveredPanicAfterGoexit(t *testing.T) {
377 output := runTestProg(t, "testprog", "RecoveredPanicAfterGoexit")
378 want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!"
379 if !strings.HasPrefix(output, want) {
380 t.Fatalf("output does not start with %q:\n%s", want, output)
384 func TestRecoverBeforePanicAfterGoexit(t *testing.T) {
385 // 1. defer a function that recovers
386 // 2. defer a function that panics
388 // Goexit should run the #2 defer. Its panic
389 // should be caught by the #1 defer, and execution
390 // should resume in the caller. Like the Goexit
404 func TestNetpollDeadlock(t *testing.T) {
406 output := runTestProg(t, "testprognet", "NetpollDeadlock")
408 if !strings.HasSuffix(output, want) {
409 t.Fatalf("output does not start with %q:\n%s", want, output)
413 func TestPanicTraceback(t *testing.T) {
415 output := runTestProg(t, "testprog", "PanicTraceback")
416 want := "panic: hello"
417 if !strings.HasPrefix(output, want) {
418 t.Fatalf("output does not start with %q:\n%s", want, output)
421 // Check functions in the traceback.
422 fns := []string{"main.pt1.func1", "panic", "main.pt2.func1", "panic", "main.pt2", "main.pt1"}
423 for _, fn := range fns {
424 re := regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(fn) + `\(.*\n`)
425 idx := re.FindStringIndex(output)
427 t.Fatalf("expected %q function in traceback:\n%s", fn, output)
429 output = output[idx[1]:]
433 func testPanicDeadlock(t *testing.T, name string, want string) {
435 output := runTestProg(t, "testprog", name)
436 if !strings.HasPrefix(output, want) {
437 t.Fatalf("output does not start with %q:\n%s", want, output)
441 func TestPanicDeadlockGosched(t *testing.T) {
442 testPanicDeadlock(t, "GoschedInPanic", "panic: errorThatGosched\n\n")
445 func TestPanicDeadlockSyscall(t *testing.T) {
446 testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
449 func TestPanicLoop(t *testing.T) {
450 output := runTestProg(t, "testprog", "PanicLoop")
451 if want := "panic while printing panic value"; !strings.Contains(output, want) {
452 t.Errorf("output does not contain %q:\n%s", want, output)
456 func TestMemPprof(t *testing.T) {
457 testenv.MustHaveGoRun(t)
459 exe, err := buildTestProg(t, "testprog")
464 got, err := testenv.CleanCmdEnv(exec.Command(exe, "MemProf")).CombinedOutput()
468 fn := strings.TrimSpace(string(got))
471 for try := 0; try < 2; try++ {
472 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-alloc_space", "-top"))
473 // Check that pprof works both with and without explicit executable on command line.
475 cmd.Args = append(cmd.Args, exe, fn)
477 cmd.Args = append(cmd.Args, fn)
480 for i, e := range cmd.Env {
481 if strings.HasPrefix(e, "PPROF_TMPDIR=") {
482 cmd.Env[i] = "PPROF_TMPDIR=" + os.TempDir()
488 cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
491 top, err := cmd.CombinedOutput()
492 t.Logf("%s:\n%s", cmd.Args, top)
495 } else if !bytes.Contains(top, []byte("MemProf")) {
496 t.Error("missing MemProf in pprof output")
501 var concurrentMapTest = flag.Bool("run_concurrent_map_tests", false, "also run flaky concurrent map tests")
503 func TestConcurrentMapWrites(t *testing.T) {
504 if !*concurrentMapTest {
505 t.Skip("skipping without -run_concurrent_map_tests")
507 testenv.MustHaveGoRun(t)
508 output := runTestProg(t, "testprog", "concurrentMapWrites")
509 want := "fatal error: concurrent map writes"
510 if !strings.HasPrefix(output, want) {
511 t.Fatalf("output does not start with %q:\n%s", want, output)
514 func TestConcurrentMapReadWrite(t *testing.T) {
515 if !*concurrentMapTest {
516 t.Skip("skipping without -run_concurrent_map_tests")
518 testenv.MustHaveGoRun(t)
519 output := runTestProg(t, "testprog", "concurrentMapReadWrite")
520 want := "fatal error: concurrent map read and map write"
521 if !strings.HasPrefix(output, want) {
522 t.Fatalf("output does not start with %q:\n%s", want, output)
525 func TestConcurrentMapIterateWrite(t *testing.T) {
526 if !*concurrentMapTest {
527 t.Skip("skipping without -run_concurrent_map_tests")
529 testenv.MustHaveGoRun(t)
530 output := runTestProg(t, "testprog", "concurrentMapIterateWrite")
531 want := "fatal error: concurrent map iteration and map write"
532 if !strings.HasPrefix(output, want) {
533 t.Fatalf("output does not start with %q:\n%s", want, output)
541 func (p *point) negate() {
546 // Test for issue #10152.
547 func TestPanicInlined(t *testing.T) {
551 t.Fatalf("recover failed")
553 buf := make([]byte, 2048)
554 n := runtime.Stack(buf, false)
556 if !bytes.Contains(buf, []byte("(*point).negate(")) {
557 t.Fatalf("expecting stack trace to contain call to (*point).negate()")
565 // Test for issues #3934 and #20018.
566 // We want to delay exiting until a panic print is complete.
567 func TestPanicRace(t *testing.T) {
568 testenv.MustHaveGoRun(t)
570 exe, err := buildTestProg(t, "testprog")
575 // The test is intentionally racy, and in my testing does not
576 // produce the expected output about 0.05% of the time.
577 // So run the program in a loop and only fail the test if we
578 // get the wrong output ten times in a row.
581 for i := 0; i < tries; i++ {
582 got, err := testenv.CleanCmdEnv(exec.Command(exe, "PanicRace")).CombinedOutput()
584 t.Logf("try %d: program exited successfully, should have failed", i+1)
589 t.Logf("try %d:\n", i+1)
598 for _, want := range wants {
599 if !bytes.Contains(got, []byte(want)) {
600 t.Logf("did not find expected string %q", want)
605 // Test generated expected output.
608 t.Errorf("test ran %d times without producing expected output", tries)
611 func TestBadTraceback(t *testing.T) {
612 output := runTestProg(t, "testprog", "BadTraceback")
613 for _, want := range []string{
614 "runtime: unexpected return pc",
616 "00000bad", // Smashed LR in hex dump
617 "<main.badLR", // Symbolization in hex dump (badLR1 or badLR2)
619 if !strings.Contains(output, want) {
620 t.Errorf("output does not contain %q:\n%s", want, output)
625 func TestTimePprof(t *testing.T) {
626 fn := runTestProg(t, "testprog", "TimeProf")
627 fn = strings.TrimSpace(fn)
630 cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-top", "-nodecount=1", fn))
631 cmd.Env = append(cmd.Env, "PPROF_TMPDIR="+os.TempDir())
632 top, err := cmd.CombinedOutput()
636 } else if bytes.Contains(top, []byte("ExternalCode")) {
637 t.Error("profiler refers to ExternalCode")
641 // Test that runtime.abort does so.
642 func TestAbort(t *testing.T) {
643 output := runTestProg(t, "testprog", "Abort")
644 if want := "runtime.abort"; !strings.Contains(output, want) {
645 t.Errorf("output does not contain %q:\n%s", want, output)
647 if strings.Contains(output, "BAD") {
648 t.Errorf("output contains BAD:\n%s", output)
650 // Check that it's a signal-style traceback.
651 if runtime.GOOS != "windows" {
652 if want := "PC="; !strings.Contains(output, want) {
653 t.Errorf("output does not contain %q:\n%s", want, output)