Skip to content

Commit 6b89773

Browse files
committed
testenv: abstract run-with-timeout into testenv
This lifts the logic to run a subcommand with a timeout in a test from the runtime's runTestProg into testenv. The implementation is unchanged in this CL. We'll improve it in a future CL. Currently, tests that run subcommands usually just timeout with no useful output if the subcommand runs for too long. This is a step toward improving this. For #37405. Change-Id: I2298770db516e216379c4c438e05d23cbbdda51d Reviewed-on: https://go-review.googlesource.com/c/go/+/370701 Trust: Austin Clements <[email protected]> Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 9c6e8f6 commit 6b89773

File tree

5 files changed

+71
-48
lines changed

5 files changed

+71
-48
lines changed

src/internal/testenv/testenv.go

+51
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package testenv
1212

1313
import (
14+
"bytes"
1415
"errors"
1516
"flag"
1617
"internal/cfg"
@@ -22,6 +23,7 @@ import (
2223
"strings"
2324
"sync"
2425
"testing"
26+
"time"
2527
)
2628

2729
// Builder reports the name of the builder running this test
@@ -306,3 +308,52 @@ func SkipIfShortAndSlow(t testing.TB) {
306308
t.Skipf("skipping test in -short mode on %s", runtime.GOARCH)
307309
}
308310
}
311+
312+
// RunWithTimeout runs cmd and returns its combined output. If the
313+
// subprocess exits with a non-zero status, it will log that status
314+
// and return a non-nil error, but this is not considered fatal.
315+
func RunWithTimeout(t testing.TB, cmd *exec.Cmd) ([]byte, error) {
316+
args := cmd.Args
317+
if args == nil {
318+
args = []string{cmd.Path}
319+
}
320+
321+
var b bytes.Buffer
322+
cmd.Stdout = &b
323+
cmd.Stderr = &b
324+
if err := cmd.Start(); err != nil {
325+
t.Fatalf("starting %s: %v", args, err)
326+
}
327+
328+
// If the process doesn't complete within 1 minute,
329+
// assume it is hanging and kill it to get a stack trace.
330+
p := cmd.Process
331+
done := make(chan bool)
332+
go func() {
333+
scale := 1
334+
// This GOARCH/GOOS test is copied from cmd/dist/test.go.
335+
// TODO(iant): Have cmd/dist update the environment variable.
336+
if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
337+
scale = 2
338+
}
339+
if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
340+
if sc, err := strconv.Atoi(s); err == nil {
341+
scale = sc
342+
}
343+
}
344+
345+
select {
346+
case <-done:
347+
case <-time.After(time.Duration(scale) * time.Minute):
348+
p.Signal(Sigquit)
349+
}
350+
}()
351+
352+
err := cmd.Wait()
353+
if err != nil {
354+
t.Logf("%s exit status: %v", args, err)
355+
}
356+
close(done)
357+
358+
return b.Bytes(), err
359+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// Copyright 2016 The Go Authors. All rights reserved.
1+
// Copyright 2021 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

55
//go:build windows || plan9 || (js && wasm)
66

7-
package runtime_test
7+
package testenv
88

99
import "os"
1010

11-
// sigquit is the signal to send to kill a hanging testdata program.
11+
// Sigquit is the signal to send to kill a hanging subprocess.
1212
// On Unix we send SIGQUIT, but on non-Unix we only have os.Kill.
13-
var sigquit = os.Kill
13+
var Sigquit = os.Kill

src/internal/testenv/testenv_unix.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2021 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+
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
6+
7+
package testenv
8+
9+
import "syscall"
10+
11+
// Sigquit is the signal to send to kill a hanging subprocess.
12+
// Send SIGQUIT to get a stack trace.
13+
var Sigquit = syscall.SIGQUIT

src/runtime/crash_test.go

+2-39
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ import (
1515
"path/filepath"
1616
"regexp"
1717
"runtime"
18-
"strconv"
1918
"strings"
2019
"sync"
2120
"testing"
22-
"time"
2321
)
2422

2523
var toRemove []string
@@ -71,43 +69,8 @@ func runBuiltTestProg(t *testing.T, exe, name string, env ...string) string {
7169
if testing.Short() {
7270
cmd.Env = append(cmd.Env, "RUNTIME_TEST_SHORT=1")
7371
}
74-
var b bytes.Buffer
75-
cmd.Stdout = &b
76-
cmd.Stderr = &b
77-
if err := cmd.Start(); err != nil {
78-
t.Fatalf("starting %s %s: %v", exe, name, err)
79-
}
80-
81-
// If the process doesn't complete within 1 minute,
82-
// assume it is hanging and kill it to get a stack trace.
83-
p := cmd.Process
84-
done := make(chan bool)
85-
go func() {
86-
scale := 1
87-
// This GOARCH/GOOS test is copied from cmd/dist/test.go.
88-
// TODO(iant): Have cmd/dist update the environment variable.
89-
if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
90-
scale = 2
91-
}
92-
if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
93-
if sc, err := strconv.Atoi(s); err == nil {
94-
scale = sc
95-
}
96-
}
97-
98-
select {
99-
case <-done:
100-
case <-time.After(time.Duration(scale) * time.Minute):
101-
p.Signal(sigquit)
102-
}
103-
}()
104-
105-
if err := cmd.Wait(); err != nil {
106-
t.Logf("%s %s exit status: %v", exe, name, err)
107-
}
108-
close(done)
109-
110-
return b.String()
72+
out, _ := testenv.RunWithTimeout(t, cmd)
73+
return string(out)
11174
}
11275

11376
var serializeBuild = make(chan bool, 2)

src/runtime/crash_unix_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ import (
2121
"unsafe"
2222
)
2323

24-
// sigquit is the signal to send to kill a hanging testdata program.
25-
// Send SIGQUIT to get a stack trace.
26-
var sigquit = syscall.SIGQUIT
27-
2824
func init() {
2925
if runtime.Sigisblocked(int(syscall.SIGQUIT)) {
3026
// We can't use SIGQUIT to kill subprocesses because
3127
// it's blocked. Use SIGKILL instead. See issue
3228
// #19196 for an example of when this happens.
33-
sigquit = syscall.SIGKILL
29+
testenv.Sigquit = syscall.SIGKILL
3430
}
3531
}
3632

0 commit comments

Comments
 (0)