Skip to content

Commit f91592a

Browse files
Bryan C. Millsromaindoumenc
authored andcommitted
internal/testenv: remove RunWithTimout
For most tests, the test's deadline itself is more appropriate than an arbitrary timeout layered atop of it (especially once golang#48157 is implemented), and testenv.Command already adds cleaner timeout behavior when a command would run too close to the test's deadline. That makes RunWithTimeout something of an attractive nuisance. For now, migrate the two existing uses of it to testenv.CommandContext, with a shorter timeout implemented using context.WithTimeout. As a followup, we may want to drop the extra timeouts from these invocations entirely. Updates golang#50436. Updates golang#37405. Change-Id: I16840fd36c0137b6da87ec54012b3e44661f0d08 Reviewed-on: https://go-review.googlesource.com/c/go/+/445597 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Austin Clements <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent cc1f5e4 commit f91592a

File tree

3 files changed

+20
-62
lines changed

3 files changed

+20
-62
lines changed

src/internal/testenv/testenv.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
package testenv
1212

1313
import (
14-
"bytes"
1514
"context"
1615
"errors"
1716
"flag"
@@ -505,62 +504,6 @@ func SkipIfOptimizationOff(t testing.TB) {
505504
}
506505
}
507506

508-
// RunWithTimeout runs cmd and returns its combined output. If the
509-
// subprocess exits with a non-zero status, it will log that status
510-
// and return a non-nil error, but this is not considered fatal.
511-
func RunWithTimeout(t testing.TB, cmd *exec.Cmd) ([]byte, error) {
512-
args := cmd.Args
513-
if args == nil {
514-
args = []string{cmd.Path}
515-
}
516-
517-
var b bytes.Buffer
518-
cmd.Stdout = &b
519-
cmd.Stderr = &b
520-
if err := cmd.Start(); err != nil {
521-
t.Fatalf("starting %s: %v", args, err)
522-
}
523-
524-
// If the process doesn't complete within 1 minute,
525-
// assume it is hanging and kill it to get a stack trace.
526-
p := cmd.Process
527-
done := make(chan bool)
528-
go func() {
529-
scale := 1
530-
// This GOARCH/GOOS test is copied from cmd/dist/test.go.
531-
// TODO(iant): Have cmd/dist update the environment variable.
532-
if runtime.GOARCH == "arm" || runtime.GOOS == "windows" {
533-
scale = 2
534-
}
535-
if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
536-
if sc, err := strconv.Atoi(s); err == nil {
537-
scale = sc
538-
}
539-
}
540-
541-
select {
542-
case <-done:
543-
case <-time.After(time.Duration(scale) * time.Minute):
544-
p.Signal(Sigquit)
545-
// If SIGQUIT doesn't do it after a little
546-
// while, kill the process.
547-
select {
548-
case <-done:
549-
case <-time.After(time.Duration(scale) * 30 * time.Second):
550-
p.Signal(os.Kill)
551-
}
552-
}
553-
}()
554-
555-
err := cmd.Wait()
556-
if err != nil {
557-
t.Logf("%s exit status: %v", args, err)
558-
}
559-
close(done)
560-
561-
return b.Bytes(), err
562-
}
563-
564507
// WriteImportcfg writes an importcfg file used by the compiler or linker to
565508
// dstPath containing entries for the packages in std and cmd in addition
566509
// to the package to package file mappings in additionalPackageFiles.

src/runtime/crash_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime_test
66

77
import (
88
"bytes"
9+
"context"
910
"errors"
1011
"flag"
1112
"fmt"
@@ -18,6 +19,7 @@ import (
1819
"strings"
1920
"sync"
2021
"testing"
22+
"time"
2123
)
2224

2325
var toRemove []string
@@ -58,18 +60,27 @@ func runTestProg(t *testing.T, binary, name string, env ...string) string {
5860
}
5961

6062
func runBuiltTestProg(t *testing.T, exe, name string, env ...string) string {
63+
t.Helper()
64+
6165
if *flagQuick {
6266
t.Skip("-quick")
6367
}
6468

65-
testenv.MustHaveGoBuild(t)
66-
67-
cmd := testenv.CleanCmdEnv(exec.Command(exe, name))
69+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
70+
defer cancel()
71+
cmd := testenv.CleanCmdEnv(testenv.CommandContext(t, ctx, exe, name))
6872
cmd.Env = append(cmd.Env, env...)
6973
if testing.Short() {
7074
cmd.Env = append(cmd.Env, "RUNTIME_TEST_SHORT=1")
7175
}
72-
out, _ := testenv.RunWithTimeout(t, cmd)
76+
out, err := cmd.CombinedOutput()
77+
if err != nil {
78+
if _, ok := err.(*exec.ExitError); ok {
79+
t.Logf("%v: %v", cmd, err)
80+
} else {
81+
t.Fatalf("%v failed to start: %v", cmd, err)
82+
}
83+
}
7384
return string(out)
7485
}
7586

src/runtime/runtime-gdb_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime_test
66

77
import (
88
"bytes"
9+
"context"
910
"fmt"
1011
"internal/testenv"
1112
"os"
@@ -16,6 +17,7 @@ import (
1617
"strconv"
1718
"strings"
1819
"testing"
20+
"time"
1921
)
2022

2123
// NOTE: In some configurations, GDB will segfault when sent a SIGWINCH signal.
@@ -428,7 +430,9 @@ func TestGdbBacktrace(t *testing.T) {
428430
"-ex", "continue",
429431
filepath.Join(dir, "a.exe"),
430432
}
431-
got, err := testenv.RunWithTimeout(t, exec.Command("gdb", args...))
433+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
434+
defer cancel()
435+
got, err := testenv.CommandContext(t, ctx, "gdb", args...).CombinedOutput()
432436
t.Logf("gdb output:\n%s", got)
433437
if err != nil {
434438
if bytes.Contains(got, []byte("internal-error: wait returned unexpected status 0x0")) {

0 commit comments

Comments
 (0)