diff options
Diffstat (limited to 'llgo/third_party/gofrontend/libgo/go/runtime/crash_test.go')
| -rw-r--r-- | llgo/third_party/gofrontend/libgo/go/runtime/crash_test.go | 105 |
1 files changed, 89 insertions, 16 deletions
diff --git a/llgo/third_party/gofrontend/libgo/go/runtime/crash_test.go b/llgo/third_party/gofrontend/libgo/go/runtime/crash_test.go index 7e8a2e45f0e..8efce4da2d4 100644 --- a/llgo/third_party/gofrontend/libgo/go/runtime/crash_test.go +++ b/llgo/third_party/gofrontend/libgo/go/runtime/crash_test.go @@ -5,38 +5,41 @@ package runtime_test import ( + "fmt" + "internal/testenv" "io/ioutil" "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" + "sync" "testing" "text/template" ) -// testEnv excludes GODEBUG from the environment -// to prevent its output from breaking tests that -// are trying to parse other command output. func testEnv(cmd *exec.Cmd) *exec.Cmd { if cmd.Env != nil { panic("environment already set") } for _, env := range os.Environ() { + // Exclude GODEBUG from the environment to prevent its output + // from breaking tests that are trying to parse other command output. if strings.HasPrefix(env, "GODEBUG=") { continue } + // Exclude GOTRACEBACK for the same reason. + if strings.HasPrefix(env, "GOTRACEBACK=") { + continue + } cmd.Env = append(cmd.Env, env) } return cmd } func executeTest(t *testing.T, templ string, data interface{}, extra ...string) string { - t.Skip("gccgo does not have a go command") - switch runtime.GOOS { - case "android", "nacl": - t.Skipf("skipping on %s", runtime.GOOS) - } + testenv.MustHaveGoBuild(t) checkStaleRuntime(t) @@ -63,7 +66,14 @@ func executeTest(t *testing.T, templ string, data interface{}, extra ...string) } for i := 0; i < len(extra); i += 2 { - if err := ioutil.WriteFile(filepath.Join(dir, extra[i]), []byte(extra[i+1]), 0666); err != nil { + fname := extra[i] + contents := extra[i+1] + if d, _ := filepath.Split(fname); d != "" { + if err := os.Mkdir(filepath.Join(dir, d), 0755); err != nil { + t.Fatal(err) + } + } + if err := ioutil.WriteFile(filepath.Join(dir, fname), []byte(contents), 0666); err != nil { t.Fatal(err) } } @@ -79,14 +89,25 @@ func executeTest(t *testing.T, templ string, data interface{}, extra ...string) return string(got) } +var ( + staleRuntimeOnce sync.Once // guards init of staleRuntimeErr + staleRuntimeErr error +) + func checkStaleRuntime(t *testing.T) { - // 'go run' uses the installed copy of runtime.a, which may be out of date. - out, err := testEnv(exec.Command("go", "list", "-f", "{{.Stale}}", "runtime")).CombinedOutput() - if err != nil { - t.Fatalf("failed to execute 'go list': %v\n%v", err, string(out)) - } - if string(out) != "false\n" { - t.Fatalf("Stale runtime.a. Run 'go install runtime'.") + staleRuntimeOnce.Do(func() { + // 'go run' uses the installed copy of runtime.a, which may be out of date. + out, err := testEnv(exec.Command("go", "list", "-f", "{{.Stale}}", "runtime")).CombinedOutput() + if err != nil { + staleRuntimeErr = fmt.Errorf("failed to execute 'go list': %v\n%v", err, string(out)) + return + } + if string(out) != "false\n" { + staleRuntimeErr = fmt.Errorf("Stale runtime.a. Run 'go install runtime'.") + } + }) + if staleRuntimeErr != nil { + t.Fatal(staleRuntimeErr) } } @@ -205,6 +226,14 @@ func TestMainGoroutineId(t *testing.T) { } } +func TestNoHelperGoroutines(t *testing.T) { + output := executeTest(t, noHelperGoroutinesSource, nil) + matches := regexp.MustCompile(`goroutine [0-9]+ \[`).FindAllStringSubmatch(output, -1) + if len(matches) != 1 || matches[0][0] != "goroutine 1 [" { + t.Fatalf("want to see only goroutine 1, see:\n%s", output) + } +} + func TestBreakpoint(t *testing.T) { output := executeTest(t, breakpointSource, nil) want := "runtime.Breakpoint()" @@ -419,6 +448,22 @@ func main() { } ` +const noHelperGoroutinesSource = ` +package main +import ( + "runtime" + "time" +) +func init() { + i := 0 + runtime.SetFinalizer(&i, func(p *int) {}) + time.AfterFunc(time.Hour, func() {}) + panic("oops") +} +func main() { +} +` + const breakpointSource = ` package main import "runtime" @@ -514,3 +559,31 @@ func TestRecoverBeforePanicAfterGoexit(t *testing.T) { }() runtime.Goexit() } + +func TestNetpollDeadlock(t *testing.T) { + output := executeTest(t, netpollDeadlockSource, nil) + want := "done\n" + if !strings.HasSuffix(output, want) { + t.Fatalf("output does not start with %q:\n%s", want, output) + } +} + +const netpollDeadlockSource = ` +package main +import ( + "fmt" + "net" +) +func init() { + fmt.Println("dialing") + c, err := net.Dial("tcp", "localhost:14356") + if err == nil { + c.Close() + } else { + fmt.Println("error: ", err) + } +} +func main() { + fmt.Println("done") +} +` |

