Skip to content

Commit 1752610

Browse files
Bryan C. Millspull[bot]
Bryan C. Mills
authored andcommitted
os: use testenv.Command and os.Executable in tests
On Unix platforms, testenv.Command sends SIGQUIT to stuck commands before the test times out. For subprocesses that are written in Go, that causes the runtime to dump running goroutines, and in other languages it triggers similar behavior (such as a core dump). If the subprocess is stuck due to a bug (such as #57999), that may help to diagnose it. For #57999. Change-Id: I00f381b8052cbbb1a7eea90e7f102a3f68c842d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/521817 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Bryan Mills <[email protected]>
1 parent fa87d81 commit 1752610

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/os/signal/signal_cgo_test.go

+35-10
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"context"
1515
"encoding/binary"
1616
"fmt"
17+
"internal/testenv"
1718
"internal/testpty"
1819
"os"
19-
"os/exec"
2020
"os/signal"
2121
"runtime"
2222
"strconv"
@@ -93,7 +93,7 @@ func TestTerminalSignal(t *testing.T) {
9393
// Main test process, run code below.
9494
break
9595
case "1":
96-
runSessionLeader(pause)
96+
runSessionLeader(t, pause)
9797
panic("unreachable")
9898
case "2":
9999
runStoppingChild()
@@ -128,9 +128,22 @@ func TestTerminalSignal(t *testing.T) {
128128
t.Fatal(err)
129129
}
130130

131-
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
132-
defer cancel()
133-
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=TestTerminalSignal")
131+
var (
132+
ctx = context.Background()
133+
cmdArgs = []string{"-test.run=TestTerminalSignal"}
134+
)
135+
if deadline, ok := t.Deadline(); ok {
136+
d := time.Until(deadline)
137+
var cancel context.CancelFunc
138+
ctx, cancel = context.WithTimeout(ctx, d)
139+
t.Cleanup(cancel)
140+
141+
// We run the subprocess with an additional 20% margin to allow it to fail
142+
// and clean up gracefully if it times out.
143+
cmdArgs = append(cmdArgs, fmt.Sprintf("-test.timeout=%v", d*5/4))
144+
}
145+
146+
cmd := testenv.CommandContext(t, ctx, os.Args[0], cmdArgs...)
134147
cmd.Env = append(os.Environ(), "GO_TEST_TERMINAL_SIGNALS=1")
135148
cmd.Stdin = os.Stdin
136149
cmd.Stdout = os.Stdout // for logging
@@ -216,7 +229,7 @@ func TestTerminalSignal(t *testing.T) {
216229
}
217230

218231
// GO_TEST_TERMINAL_SIGNALS=1 subprocess above.
219-
func runSessionLeader(pause time.Duration) {
232+
func runSessionLeader(t *testing.T, pause time.Duration) {
220233
// "Attempts to use tcsetpgrp() from a process which is a
221234
// member of a background process group on a fildes associated
222235
// with its controlling terminal shall cause the process group
@@ -235,10 +248,22 @@ func runSessionLeader(pause time.Duration) {
235248
pty := os.NewFile(ptyFD, "pty")
236249
controlW := os.NewFile(controlFD, "control-pipe")
237250

238-
// Slightly shorter timeout than in the parent.
239-
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
240-
defer cancel()
241-
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=TestTerminalSignal")
251+
var (
252+
ctx = context.Background()
253+
cmdArgs = []string{"-test.run=TestTerminalSignal"}
254+
)
255+
if deadline, ok := t.Deadline(); ok {
256+
d := time.Until(deadline)
257+
var cancel context.CancelFunc
258+
ctx, cancel = context.WithTimeout(ctx, d)
259+
t.Cleanup(cancel)
260+
261+
// We run the subprocess with an additional 20% margin to allow it to fail
262+
// and clean up gracefully if it times out.
263+
cmdArgs = append(cmdArgs, fmt.Sprintf("-test.timeout=%v", d*5/4))
264+
}
265+
266+
cmd := testenv.CommandContext(t, ctx, os.Args[0], cmdArgs...)
242267
cmd.Env = append(os.Environ(), "GO_TEST_TERMINAL_SIGNALS=2")
243268
cmd.Stdin = os.Stdin
244269
cmd.Stdout = os.Stdout

src/os/signal/signal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func TestNohup(t *testing.T) {
483483
defer wg.Done()
484484

485485
// POSIX specifies that nohup writes to a file named nohup.out if standard
486-
// output is a terminal. However, for an exec.Command, standard output is
486+
// output is a terminal. However, for an exec.Cmd, standard output is
487487
// not a terminal — so we don't need to read or remove that file (and,
488488
// indeed, cannot even create it if the current user is unable to write to
489489
// GOROOT/src, such as when GOROOT is installed and owned by root).

0 commit comments

Comments
 (0)