Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 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. |
| 4 | |
| 5 | package runtime_test |
| 6 | |
| 7 | import ( |
Brad Fitzpatrick | b70ddc0 | 2015-01-05 13:14:08 -0800 | [diff] [blame] | 8 | "fmt" |
Russ Cox | 7bc3e58 | 2015-06-05 11:01:53 -0400 | [diff] [blame] | 9 | "internal/testenv" |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 10 | "io/ioutil" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 14 | "regexp" |
Russ Cox | 0c2a727 | 2014-05-20 12:10:19 -0400 | [diff] [blame] | 15 | "runtime" |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 16 | "strings" |
Brad Fitzpatrick | b70ddc0 | 2015-01-05 13:14:08 -0800 | [diff] [blame] | 17 | "sync" |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 18 | "testing" |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 19 | ) |
| 20 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 21 | var toRemove []string |
| 22 | |
| 23 | func TestMain(m *testing.M) { |
| 24 | status := m.Run() |
| 25 | for _, file := range toRemove { |
| 26 | os.RemoveAll(file) |
| 27 | } |
| 28 | os.Exit(status) |
| 29 | } |
| 30 | |
Albert Strasheim | 4235fa8 | 2013-04-07 11:37:37 -0700 | [diff] [blame] | 31 | func testEnv(cmd *exec.Cmd) *exec.Cmd { |
| 32 | if cmd.Env != nil { |
| 33 | panic("environment already set") |
| 34 | } |
| 35 | for _, env := range os.Environ() { |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 36 | // Exclude GODEBUG from the environment to prevent its output |
| 37 | // from breaking tests that are trying to parse other command output. |
Dmitriy Vyukov | 4b536a5 | 2013-06-28 18:37:06 +0400 | [diff] [blame] | 38 | if strings.HasPrefix(env, "GODEBUG=") { |
Albert Strasheim | 4235fa8 | 2013-04-07 11:37:37 -0700 | [diff] [blame] | 39 | continue |
| 40 | } |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 41 | // Exclude GOTRACEBACK for the same reason. |
| 42 | if strings.HasPrefix(env, "GOTRACEBACK=") { |
| 43 | continue |
| 44 | } |
Albert Strasheim | 4235fa8 | 2013-04-07 11:37:37 -0700 | [diff] [blame] | 45 | cmd.Env = append(cmd.Env, env) |
| 46 | } |
| 47 | return cmd |
| 48 | } |
| 49 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 50 | var testprog struct { |
| 51 | sync.Mutex |
| 52 | dir string |
| 53 | target map[string]buildexe |
| 54 | } |
| 55 | |
| 56 | type buildexe struct { |
| 57 | exe string |
| 58 | err error |
| 59 | } |
| 60 | |
| 61 | func runTestProg(t *testing.T, binary, name string) string { |
Russ Cox | 7bc3e58 | 2015-06-05 11:01:53 -0400 | [diff] [blame] | 62 | testenv.MustHaveGoBuild(t) |
Russ Cox | 0c2a727 | 2014-05-20 12:10:19 -0400 | [diff] [blame] | 63 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 64 | exe, err := buildTestProg(t, binary) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | got, _ := testEnv(exec.Command(exe, name)).CombinedOutput() |
| 69 | return string(got) |
| 70 | } |
| 71 | |
| 72 | func buildTestProg(t *testing.T, binary string) (string, error) { |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 73 | checkStaleRuntime(t) |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 74 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 75 | testprog.Lock() |
| 76 | defer testprog.Unlock() |
| 77 | if testprog.dir == "" { |
| 78 | dir, err := ioutil.TempDir("", "go-build") |
| 79 | if err != nil { |
| 80 | t.Fatalf("failed to create temp directory: %v", err) |
Alex Brainman | 9b69196 | 2015-03-16 15:46:22 +1100 | [diff] [blame] | 81 | } |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 82 | testprog.dir = dir |
| 83 | toRemove = append(toRemove, dir) |
Russ Cox | c4efaac | 2014-10-28 21:53:09 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 86 | if testprog.target == nil { |
| 87 | testprog.target = make(map[string]buildexe) |
| 88 | } |
| 89 | target, ok := testprog.target[binary] |
| 90 | if ok { |
| 91 | return target.exe, target.err |
| 92 | } |
| 93 | |
| 94 | exe := filepath.Join(testprog.dir, binary+".exe") |
| 95 | cmd := exec.Command("go", "build", "-o", exe) |
| 96 | cmd.Dir = "testdata/" + binary |
Russ Cox | c4efaac | 2014-10-28 21:53:09 -0400 | [diff] [blame] | 97 | out, err := testEnv(cmd).CombinedOutput() |
| 98 | if err != nil { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 99 | exe = "" |
| 100 | target.err = fmt.Errorf("building %s: %v\n%s", binary, err, out) |
| 101 | testprog.target[binary] = target |
Ian Lance Taylor | e13a082 | 2016-01-11 09:34:38 -0800 | [diff] [blame] | 102 | return "", target.err |
Russ Cox | c4efaac | 2014-10-28 21:53:09 -0400 | [diff] [blame] | 103 | } |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 104 | target.exe = exe |
| 105 | testprog.target[binary] = target |
| 106 | return exe, nil |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 107 | } |
| 108 | |
Brad Fitzpatrick | b70ddc0 | 2015-01-05 13:14:08 -0800 | [diff] [blame] | 109 | var ( |
| 110 | staleRuntimeOnce sync.Once // guards init of staleRuntimeErr |
| 111 | staleRuntimeErr error |
| 112 | ) |
| 113 | |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 114 | func checkStaleRuntime(t *testing.T) { |
Brad Fitzpatrick | b70ddc0 | 2015-01-05 13:14:08 -0800 | [diff] [blame] | 115 | staleRuntimeOnce.Do(func() { |
| 116 | // 'go run' uses the installed copy of runtime.a, which may be out of date. |
| 117 | out, err := testEnv(exec.Command("go", "list", "-f", "{{.Stale}}", "runtime")).CombinedOutput() |
| 118 | if err != nil { |
| 119 | staleRuntimeErr = fmt.Errorf("failed to execute 'go list': %v\n%v", err, string(out)) |
| 120 | return |
| 121 | } |
| 122 | if string(out) != "false\n" { |
| 123 | staleRuntimeErr = fmt.Errorf("Stale runtime.a. Run 'go install runtime'.") |
| 124 | } |
| 125 | }) |
| 126 | if staleRuntimeErr != nil { |
| 127 | t.Fatal(staleRuntimeErr) |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | func testCrashHandler(t *testing.T, cgo bool) { |
| 132 | type crashTest struct { |
| 133 | Cgo bool |
| 134 | } |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 135 | var output string |
| 136 | if cgo { |
| 137 | output = runTestProg(t, "testprogcgo", "Crash") |
| 138 | } else { |
| 139 | output = runTestProg(t, "testprog", "Crash") |
| 140 | } |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 141 | want := "main: recovered done\nnew-thread: recovered done\nsecond-new-thread: recovered done\nmain-again: recovered done\n" |
Russ Cox | 757e0de | 2013-08-15 22:34:06 -0400 | [diff] [blame] | 142 | if output != want { |
| 143 | t.Fatalf("output:\n%s\n\nwanted:\n%s", output, want) |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestCrashHandler(t *testing.T) { |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 148 | testCrashHandler(t, false) |
| 149 | } |
| 150 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 151 | func testDeadlock(t *testing.T, name string) { |
| 152 | output := runTestProg(t, "testprog", name) |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 153 | want := "fatal error: all goroutines are asleep - deadlock!\n" |
Russ Cox | 757e0de | 2013-08-15 22:34:06 -0400 | [diff] [blame] | 154 | if !strings.HasPrefix(output, want) { |
| 155 | t.Fatalf("output does not start with %q:\n%s", want, output) |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestSimpleDeadlock(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 160 | testDeadlock(t, "SimpleDeadlock") |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | func TestInitDeadlock(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 164 | testDeadlock(t, "InitDeadlock") |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | func TestLockedDeadlock(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 168 | testDeadlock(t, "LockedDeadlock") |
Dmitriy Vyukov | 06a488f | 2013-02-20 12:15:02 +0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | func TestLockedDeadlock2(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 172 | testDeadlock(t, "LockedDeadlock2") |
Alex Brainman | afe0e97 | 2012-05-30 15:10:54 +1000 | [diff] [blame] | 173 | } |
| 174 | |
Dmitriy Vyukov | 2fe840f | 2013-03-05 09:40:17 +0200 | [diff] [blame] | 175 | func TestGoexitDeadlock(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 176 | output := runTestProg(t, "testprog", "GoexitDeadlock") |
Russ Cox | ade6bc6 | 2014-04-16 13:12:18 -0400 | [diff] [blame] | 177 | want := "no goroutines (main called runtime.Goexit) - deadlock!" |
| 178 | if !strings.Contains(output, want) { |
| 179 | t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want) |
Russ Cox | 757e0de | 2013-08-15 22:34:06 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | func TestStackOverflow(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 184 | output := runTestProg(t, "testprog", "StackOverflow") |
| 185 | want := "runtime: goroutine stack exceeds 1474560-byte limit\nfatal error: stack overflow" |
Russ Cox | 757e0de | 2013-08-15 22:34:06 -0400 | [diff] [blame] | 186 | if !strings.HasPrefix(output, want) { |
| 187 | t.Fatalf("output does not start with %q:\n%s", want, output) |
Dmitriy Vyukov | 2fe840f | 2013-03-05 09:40:17 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Russ Cox | 665feee | 2013-08-16 22:25:26 -0400 | [diff] [blame] | 191 | func TestThreadExhaustion(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 192 | output := runTestProg(t, "testprog", "ThreadExhaustion") |
Russ Cox | 665feee | 2013-08-16 22:25:26 -0400 | [diff] [blame] | 193 | want := "runtime: program exceeds 10-thread limit\nfatal error: thread exhaustion" |
| 194 | if !strings.HasPrefix(output, want) { |
| 195 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 196 | } |
| 197 | } |
| 198 | |
Dmitriy Vyukov | f946a7c | 2014-03-07 20:50:30 +0400 | [diff] [blame] | 199 | func TestRecursivePanic(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 200 | output := runTestProg(t, "testprog", "RecursivePanic") |
Dmitriy Vyukov | f946a7c | 2014-03-07 20:50:30 +0400 | [diff] [blame] | 201 | want := `wrap: bad |
| 202 | panic: again |
| 203 | |
| 204 | ` |
| 205 | if !strings.HasPrefix(output, want) { |
| 206 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
Russ Cox | ade6bc6 | 2014-04-16 13:12:18 -0400 | [diff] [blame] | 211 | func TestGoexitCrash(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 212 | output := runTestProg(t, "testprog", "GoexitExit") |
Russ Cox | ade6bc6 | 2014-04-16 13:12:18 -0400 | [diff] [blame] | 213 | want := "no goroutines (main called runtime.Goexit) - deadlock!" |
| 214 | if !strings.Contains(output, want) { |
| 215 | t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want) |
Dmitriy Vyukov | 55e0f36 | 2014-04-15 19:48:17 +0400 | [diff] [blame] | 216 | } |
Dmitriy Vyukov | 55e0f36 | 2014-04-15 19:48:17 +0400 | [diff] [blame] | 217 | } |
| 218 | |
Keith Randall | 7e62316 | 2014-09-15 15:09:17 -0700 | [diff] [blame] | 219 | func TestGoexitDefer(t *testing.T) { |
| 220 | c := make(chan struct{}) |
| 221 | go func() { |
| 222 | defer func() { |
| 223 | r := recover() |
| 224 | if r != nil { |
| 225 | t.Errorf("non-nil recover during Goexit") |
| 226 | } |
| 227 | c <- struct{}{} |
| 228 | }() |
| 229 | runtime.Goexit() |
| 230 | }() |
| 231 | // Note: if the defer fails to run, we will get a deadlock here |
| 232 | <-c |
| 233 | } |
| 234 | |
Dmitriy Vyukov | b5caa02 | 2014-05-28 00:00:01 -0400 | [diff] [blame] | 235 | func TestGoNil(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 236 | output := runTestProg(t, "testprog", "GoNil") |
Dmitriy Vyukov | b5caa02 | 2014-05-28 00:00:01 -0400 | [diff] [blame] | 237 | want := "go of nil func value" |
| 238 | if !strings.Contains(output, want) { |
| 239 | t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want) |
| 240 | } |
| 241 | } |
| 242 | |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 243 | func TestMainGoroutineID(t *testing.T) { |
| 244 | output := runTestProg(t, "testprog", "MainGoroutineID") |
Dmitriy Vyukov | aa76377 | 2014-07-16 12:19:33 +0400 | [diff] [blame] | 245 | want := "panic: test\n\ngoroutine 1 [running]:\n" |
| 246 | if !strings.HasPrefix(output, want) { |
| 247 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 248 | } |
| 249 | } |
| 250 | |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 251 | func TestNoHelperGoroutines(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 252 | output := runTestProg(t, "testprog", "NoHelperGoroutines") |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 253 | matches := regexp.MustCompile(`goroutine [0-9]+ \[`).FindAllStringSubmatch(output, -1) |
| 254 | if len(matches) != 1 || matches[0][0] != "goroutine 1 [" { |
| 255 | t.Fatalf("want to see only goroutine 1, see:\n%s", output) |
| 256 | } |
| 257 | } |
| 258 | |
Russ Cox | 1d550b8 | 2014-09-11 12:08:30 -0400 | [diff] [blame] | 259 | func TestBreakpoint(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 260 | output := runTestProg(t, "testprog", "Breakpoint") |
Russ Cox | 1d550b8 | 2014-09-11 12:08:30 -0400 | [diff] [blame] | 261 | want := "runtime.Breakpoint()" |
| 262 | if !strings.Contains(output, want) { |
| 263 | t.Fatalf("output:\n%s\n\nwant output containing: %s", output, want) |
| 264 | } |
| 265 | } |
| 266 | |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 267 | func TestGoexitInPanic(t *testing.T) { |
| 268 | // see issue 8774: this code used to trigger an infinite recursion |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 269 | output := runTestProg(t, "testprog", "GoexitInPanic") |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 270 | want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!" |
| 271 | if !strings.HasPrefix(output, want) { |
| 272 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 273 | } |
| 274 | } |
| 275 | |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 276 | func TestPanicAfterGoexit(t *testing.T) { |
| 277 | // an uncaught panic should still work after goexit |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 278 | output := runTestProg(t, "testprog", "PanicAfterGoexit") |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 279 | want := "panic: hello" |
| 280 | if !strings.HasPrefix(output, want) { |
| 281 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 282 | } |
| 283 | } |
| 284 | |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 285 | func TestRecoveredPanicAfterGoexit(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 286 | output := runTestProg(t, "testprog", "RecoveredPanicAfterGoexit") |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 287 | want := "fatal error: no goroutines (main called runtime.Goexit) - deadlock!" |
| 288 | if !strings.HasPrefix(output, want) { |
| 289 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 290 | } |
| 291 | } |
| 292 | |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 293 | func TestRecoverBeforePanicAfterGoexit(t *testing.T) { |
| 294 | // 1. defer a function that recovers |
| 295 | // 2. defer a function that panics |
| 296 | // 3. call goexit |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 297 | // Goexit should run the #2 defer. Its panic |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 298 | // should be caught by the #1 defer, and execution |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 299 | // should resume in the caller. Like the Goexit |
Keith Randall | 0306478 | 2014-09-19 16:33:14 -0700 | [diff] [blame] | 300 | // never happened! |
| 301 | defer func() { |
| 302 | r := recover() |
| 303 | if r == nil { |
| 304 | panic("bad recover") |
| 305 | } |
| 306 | }() |
| 307 | defer func() { |
| 308 | panic("hello") |
| 309 | }() |
| 310 | runtime.Goexit() |
| 311 | } |
Dmitry Vyukov | 776aeca | 2015-01-13 20:12:50 +0300 | [diff] [blame] | 312 | |
| 313 | func TestNetpollDeadlock(t *testing.T) { |
Russ Cox | 8d5ff2e | 2015-12-21 10:29:21 -0500 | [diff] [blame] | 314 | output := runTestProg(t, "testprognet", "NetpollDeadlock") |
Dmitry Vyukov | 776aeca | 2015-01-13 20:12:50 +0300 | [diff] [blame] | 315 | want := "done\n" |
| 316 | if !strings.HasSuffix(output, want) { |
| 317 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 318 | } |
| 319 | } |
Austin Clements | 0c02bc0 | 2016-02-12 10:33:51 -0500 | [diff] [blame] | 320 | |
| 321 | func TestPanicTraceback(t *testing.T) { |
| 322 | output := runTestProg(t, "testprog", "PanicTraceback") |
| 323 | want := "panic: hello" |
| 324 | if !strings.HasPrefix(output, want) { |
| 325 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 326 | } |
| 327 | |
| 328 | // Check functions in the traceback. |
| 329 | fns := []string{"panic", "main.pt1.func1", "panic", "main.pt2.func1", "panic", "main.pt2", "main.pt1"} |
| 330 | for _, fn := range fns { |
| 331 | re := regexp.MustCompile(`(?m)^` + regexp.QuoteMeta(fn) + `\(.*\n`) |
| 332 | idx := re.FindStringIndex(output) |
| 333 | if idx == nil { |
| 334 | t.Fatalf("expected %q function in traceback:\n%s", fn, output) |
| 335 | } |
| 336 | output = output[idx[1]:] |
| 337 | } |
| 338 | } |
Shenghou Ma | e960302 | 2016-02-21 13:56:08 -0500 | [diff] [blame] | 339 | |
| 340 | func testPanicDeadlock(t *testing.T, name string, want string) { |
| 341 | // test issue 14432 |
| 342 | output := runTestProg(t, "testprog", name) |
| 343 | if !strings.HasPrefix(output, want) { |
| 344 | t.Fatalf("output does not start with %q:\n%s", want, output) |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | func TestPanicDeadlockGosched(t *testing.T) { |
| 349 | testPanicDeadlock(t, "GoschedInPanic", "panic: errorThatGosched\n\n") |
| 350 | } |
| 351 | |
| 352 | func TestPanicDeadlockSyscall(t *testing.T) { |
| 353 | testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n") |
| 354 | } |