| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | // CITimeoutMultiplier is the multiplier for all timeouts in the CI. |
| 18 | CITimeoutMultiplier = 3 |
| 19 | ) |
| 20 | |
| 21 | // Sleep pauses the current goroutine for at least the duration d. |
| 22 | func Sleep(d time.Duration) { |
| 23 | d = applyCIMultiplier(d) |
| 24 | time.Sleep(d) |
| 25 | } |
| 26 | |
| 27 | func applyCIMultiplier(timeout time.Duration) time.Duration { |
| 28 | ci := os.Getenv("CI") |
| 29 | if ci == "" { |
| 30 | return timeout |
| 31 | } |
| 32 | b, err := strconv.ParseBool(ci) |
| 33 | if err != nil || !b { |
| 34 | return timeout |
| 35 | } |
| 36 | return time.Duration(float64(timeout) * CITimeoutMultiplier) |
| 37 | } |
| 38 | |
| 39 | func TestYaegiCmdCancel(t *testing.T) { |
| 40 | if runtime.GOOS == "windows" { |
| 41 | t.Skip("skipping cancel test since windows has no os.Interrupt signal") |
| 42 | } |
| 43 | tmp := t.TempDir() |
| 44 | yaegi := filepath.Join(tmp, "yaegi") |
| 45 | |
| 46 | args := []string{"build"} |
| 47 | if raceDetectorSupported(runtime.GOOS, runtime.GOARCH) { |
| 48 | args = append(args, "-race") |
| 49 | } |
| 50 | args = append(args, "-o", yaegi, ".") |
| 51 | |
| 52 | build := exec.Command("go", args...) |
| 53 | |
| 54 | out, err := build.CombinedOutput() |
| 55 | if err != nil { |
| 56 | t.Fatalf("failed to build yaegi command: %v: %s", err, out) |
| 57 | } |
| 58 | |
| 59 | // Test src must be terminated by a single newline. |
| 60 | tests := []string{ |
| 61 | "for {}\n", |
| 62 | "select {}\n", |
| 63 | } |
| 64 | for _, src := range tests { |
| 65 | cmd := exec.Command(yaegi) |
| 66 | in, err := cmd.StdinPipe() |
| 67 | if err != nil { |
| 68 | t.Errorf("failed to get stdin pipe to yaegi command: %v", err) |
| 69 | } |
| 70 | var outBuf, errBuf bytes.Buffer |
| 71 | cmd.Stdout = &outBuf |
| 72 | cmd.Stderr = &errBuf |
| 73 | |
| 74 | // https://golang.org/doc/articles/race_detector.html#Options |
| 75 | cmd.Env = []string{`GORACE="halt_on_error=1"`} |
| 76 | |
| 77 | err = cmd.Start() |
| 78 | if err != nil { |
| 79 | t.Fatalf("failed to start yaegi command: %v", err) |
| 80 | } |
| 81 | |
| 82 | _, err = in.Write([]byte(src)) |
| 83 | if err != nil { |
| 84 | t.Errorf("failed pipe test source to yaegi command: %v", err) |
| 85 | } |
| 86 | Sleep(500 * time.Millisecond) |
| 87 | err = cmd.Process.Signal(os.Interrupt) |
| 88 | if err != nil { |
| 89 | t.Errorf("failed to send os.Interrupt to yaegi command: %v", err) |
| 90 | } |
| 91 | |
| 92 | _, err = in.Write([]byte("1+1\n")) |
| 93 | if err != nil { |
| 94 | t.Errorf("failed to probe race: %v", err) |
| 95 | } |
| 96 | err = in.Close() |
| 97 | if err != nil { |
| 98 | t.Errorf("failed to close stdin pipe: %v", err) |
| 99 | } |
| 100 | |
| 101 | err = cmd.Wait() |
| 102 | if err != nil { |
| 103 | if cmd.ProcessState.ExitCode() == 66 { // See race_detector.html article. |
| 104 | t.Errorf("race detected running yaegi command canceling %q: %v", src, err) |
| 105 | if testing.Verbose() { |
| 106 | t.Log(&errBuf) |
| 107 | } |
| 108 | } else { |
| 109 | t.Errorf("error running yaegi command for %q: %v", src, err) |
| 110 | } |
| 111 | continue |
| 112 | } |
| 113 | |
| 114 | if strings.TrimSuffix(errBuf.String(), "\n") != context.Canceled.Error() { |
| 115 | t.Errorf("unexpected error: %q", &errBuf) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func raceDetectorSupported(goos, goarch string) bool { |
| 121 | if strings.Contains(os.Getenv("GOFLAGS"), "-buildmode=pie") { |
| 122 | // The Go race detector is not compatible with position independent code (pie). |
| 123 | // We read the conventional GOFLAGS env variable used for example on AlpineLinux |
| 124 | // to build packages, as there is no way to get this information from the runtime. |
| 125 | return false |
| 126 | } |
| 127 | switch goos { |
| 128 | case "linux": |
| 129 | return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" |
| 130 | case "darwin": |
| 131 | return goarch == "amd64" || goarch == "arm64" |
| 132 | case "freebsd", "netbsd", "openbsd", "windows": |
| 133 | return goarch == "amd64" |
| 134 | default: |
| 135 | return false |
| 136 | } |
| 137 | } |