Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go/internal/test: pass default timeout to test programs if not given from command line #30545

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,9 @@ var (
pkgArgs []string
pkgs []*load.Package

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

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

// Pass timeout to tests if it exists.
if testActualTimeout > 0 {
testArgs = append(testArgs, "-test.timeout="+testActualTimeout.String())
}

// show passing test output (after buffering) with -v flag.
// must buffer because tests are running in parallel, and
// otherwise the output will get mixed.
Expand Down
21 changes: 21 additions & 0 deletions src/cmd/go/testdata/script/test_timeout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
env GO111MODULE=off
cd a

# No timeout is passed via 'go test' command.
go test -v
stdout '10m0s'

# Timeout is passed via 'go test' command.
go test -v -timeout 30m
stdout '30m0s'

-- a/timeout_test.go --
package t
import (
"flag"
"fmt"
"testing"
)
func TestTimeout(t *testing.T) {
fmt.Println(flag.Lookup("test.timeout").Value.String())
}