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.
17 type crashTest struct {
21 // This test is a separate program, because it is testing
22 // both main (m0) and non-main threads (m).
24 func testCrashHandler(t *testing.T, ct *crashTest) {
25 if runtime.GOOS == "freebsd" || runtime.GOOS == "netbsd" {
26 // TODO(brainman): do not know why this test fails on freebsd
27 // TODO(jsing): figure out why this causes delayed failures
28 // on NetBSD - http://golang.org/issue/3954
29 t.Logf("skipping test on %q", runtime.GOOS)
33 st := template.Must(template.New("crashSource").Parse(crashSource))
35 dir, err := ioutil.TempDir("", "go-build")
37 t.Fatalf("failed to create temp directory: %v", err)
39 defer os.RemoveAll(dir)
41 src := filepath.Join(dir, "main.go")
42 f, err := os.Create(src)
44 t.Fatalf("failed to create %v: %v", src, err)
46 err = st.Execute(f, ct)
49 t.Fatalf("failed to execute template: %v", err)
53 got, err := exec.Command("go", "run", src).CombinedOutput()
55 t.Fatalf("program exited with error: %v\n%v", err, string(got))
57 want := "main: recovered done\nnew-thread: recovered done\nsecond-new-thread: recovered done\nmain-again: recovered done\n"
58 if string(got) != string(want) {
59 t.Fatalf("expected %q, but got %q", string(want), string(got))
63 func TestCrashHandler(t *testing.T) {
64 testCrashHandler(t, &crashTest{Cgo: false})
79 func test(name string) {
81 if x := recover(); x != nil {
82 fmt.Printf(" recovered")
86 fmt.Printf("%s:", name)
89 fmt.Print("SHOULD NOT BE HERE")
92 func testInNewThread(name string) {
95 runtime.LockOSThread()
103 runtime.LockOSThread()
105 testInNewThread("new-thread")
106 testInNewThread("second-new-thread")