Skip to content

Commit 66f9a6f

Browse files
committed
cmd/go/internal/test: pass default timeout to test programs even if it is not given from command line
Make 'go test' command to pass the default timeout (10m) to test programs even if the value is not given from command line. Fixes #28147
1 parent 243c8eb commit 66f9a6f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/cmd/go/internal/test/test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,9 @@ var (
484484
pkgArgs []string
485485
pkgs []*load.Package
486486

487-
testKillTimeout = 10 * time.Minute
488-
testCacheExpire time.Time // ignore cached test results before this time
487+
testActualTimeout = 10 * time.Minute // actual timeout which is passed to tests
488+
testKillTimeout = testActualTimeout + 1*time.Minute // backup alarm
489+
testCacheExpire time.Time // ignore cached test results before this time
489490
)
490491

491492
// testVetFlags is the list of flags to pass to vet when invoked automatically during go test.
@@ -552,13 +553,21 @@ func runTest(cmd *base.Command, args []string) {
552553
// the test wedges with a goroutine spinning and its background
553554
// timer does not get a chance to fire.
554555
if dt, err := time.ParseDuration(testTimeout); err == nil && dt > 0 {
555-
testKillTimeout = dt + 1*time.Minute
556+
testActualTimeout = dt
557+
testKillTimeout = testActualTimeout + 1*time.Minute
556558
} else if err == nil && dt == 0 {
557559
// An explicit zero disables the test timeout.
560+
// No timeout is passed to tests.
558561
// Let it have one century (almost) before we kill it.
562+
testActualTimeout = -1
559563
testKillTimeout = 100 * 365 * 24 * time.Hour
560564
}
561565

566+
// Pass timeout to tests if it exists.
567+
if testActualTimeout > 0 {
568+
testArgs = append(testArgs, "-test.timeout="+testActualTimeout.String())
569+
}
570+
562571
// show passing test output (after buffering) with -v flag.
563572
// must buffer because tests are running in parallel, and
564573
// otherwise the output will get mixed.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
env GO111MODULE=off
2+
cd a
3+
4+
# No timeout is passed via 'go test' command.
5+
go test -v
6+
stdout '10m0s'
7+
8+
# Timeout is passed via 'go test' command.
9+
go test -v -timeout 30m
10+
stdout '30m0s'
11+
12+
-- a/timeout_test.go --
13+
package t
14+
import (
15+
"flag"
16+
"fmt"
17+
"testing"
18+
)
19+
func TestTimeout(t *testing.T) {
20+
fmt.Println(flag.Lookup("test.timeout").Value.String())
21+
}

0 commit comments

Comments
 (0)