From 135972eb89aa4fb3d04091c0a29bb80aac770f4a Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 24 Jan 2022 10:49:25 -0500 Subject: [PATCH] gopls/internal/regtest/codelens: use the test's deadline instead of a hard-coded timeout We may want to generalize this to have regtest.Run always derive the default timeout from the test's deadline. In the meantime, this is a more targeted fix for the specific timeout in TestGCDetails. Fixes golang/go#49902 (Maybe.) Change-Id: Ie15735dc7b0d462ec047d3f3d8a2eceeb4411fa0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/380496 Trust: Bryan Mills Run-TryBot: Bryan Mills gopls-CI: kokoro Reviewed-by: Robert Findley TryBot-Result: Gopher Robot --- gopls/internal/regtest/codelens/codelens_test.go | 10 ++++++++-- internal/testenv/testenv.go | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/gopls/internal/regtest/codelens/codelens_test.go b/gopls/internal/regtest/codelens/codelens_test.go index ad35a29911..38721fb2c6 100644 --- a/gopls/internal/regtest/codelens/codelens_test.go +++ b/gopls/internal/regtest/codelens/codelens_test.go @@ -292,6 +292,13 @@ func TestGCDetails(t *testing.T) { t.Skipf("the gc details code lens doesn't work on Android") } + // TestGCDetails seems to suffer from poor performance on certain builders. + // Give it as long as it needs to complete. + timeout := 60 * time.Second + if d, ok := testenv.Deadline(t); ok { + timeout = time.Until(d) * 19 / 20 // Leave 5% headroom for cleanup. + } + const mod = ` -- go.mod -- module mod.com @@ -311,8 +318,7 @@ func main() { CodeLenses: map[string]bool{ "gc_details": true, }}, - // TestGCDetails seems to suffer from poor performance on certain builders. Give it some more time to complete. - Timeout(60*time.Second), + Timeout(timeout), ).Run(t, mod, func(t *testing.T, env *Env) { env.OpenFile("main.go") env.ExecuteCodeLensCommand("main.go", command.GCDetails) diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 2a7b2a6a6d..b381232649 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -15,6 +15,7 @@ import ( "runtime" "strings" "sync" + "time" exec "golang.org/x/sys/execabs" ) @@ -307,3 +308,15 @@ func SkipAfterGo1Point(t Testing, x int) { t.Skipf("running Go version %q is version 1.%d, newer than maximum 1.%d", runtime.Version(), Go1Point(), x) } } + +// Deadline returns the deadline of t, if known, +// using the Deadline method added in Go 1.15. +func Deadline(t Testing) (time.Time, bool) { + td, ok := t.(interface { + Deadline() (time.Time, bool) + }) + if !ok { + return time.Time{}, false + } + return td.Deadline() +}