Skip to content

Commit 48f2a55

Browse files
committed
cmd/go: treat cached test results as satisfying any timeout
We want test caching to work even for people with scripts that set a non-default test timeout. But then that raises the question of what to do about runs with different timeouts: is a cached success with one timeout available for use when asked to run the test with a different timeout? This CL answers that question by saying that the timeout applies to the overall execution of either running the test or displaying the cached result, and displaying a cached result takes no time. So it's always OK to record a cached result, regardless of timeout, and it's always OK to display a cached result, again regardless of timeout. Fixes #22633. Change-Id: Iaef3602710e3be107602267bbc6dba9a2250796c Reviewed-on: https://go-review.googlesource.com/76552 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: roger peppe <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent 5993251 commit 48f2a55

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/cmd/go/go_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -4823,7 +4823,9 @@ func TestTestCache(t *testing.T) {
48234823
tg.setenv("GOPATH", tg.tempdir)
48244824
tg.setenv("GOCACHE", filepath.Join(tg.tempdir, "cache"))
48254825

4826-
tg.run("test", "-x", "errors")
4826+
// timeout here should not affect result being cached
4827+
// or being retrieved later.
4828+
tg.run("test", "-x", "-timeout=10s", "errors")
48274829
tg.grepStderr(`[\\/]compile|gccgo`, "did not run compiler")
48284830
tg.grepStderr(`[\\/]link|gccgo`, "did not run linker")
48294831
tg.grepStderr(`errors\.test`, "did not run test")
@@ -4835,6 +4837,10 @@ func TestTestCache(t *testing.T) {
48354837
tg.grepStderrNot(`errors\.test`, "incorrectly ran test")
48364838
tg.grepStderrNot("DO NOT USE", "poisoned action status leaked")
48374839

4840+
// Even very low timeouts do not disqualify cached entries.
4841+
tg.run("test", "-timeout=1ns", "-x", "errors")
4842+
tg.grepStderrNot(`errors\.test`, "incorrectly ran test")
4843+
48384844
// The -p=1 in the commands below just makes the -x output easier to read.
48394845

48404846
t.Log("\n\nINITIAL\n\n")

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ go test will redisplay the previous output instead of running the test
105105
binary again. In the summary line, go test prints '(cached)' in place of
106106
the elapsed time. To disable test caching, use any test flag or argument
107107
other than the cacheable flags. The idiomatic way to disable test caching
108-
explicitly is to use -count=1.
108+
explicitly is to use -count=1. A cached result is treated as executing in
109+
no time at all, so a successful package test result will be cached and reused
110+
regardless of -timeout setting.
109111
110112
` + strings.TrimSpace(testFlag1) + ` See 'go help testflag' for details.
111113
@@ -1346,6 +1348,7 @@ func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bo
13461348
return false
13471349
}
13481350

1351+
var cacheArgs []string
13491352
for _, arg := range testArgs {
13501353
i := strings.Index(arg, "=")
13511354
if i < 0 || !strings.HasPrefix(arg, "-test.") {
@@ -1362,6 +1365,12 @@ func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bo
13621365
// These are cacheable.
13631366
// Note that this list is documented above,
13641367
// so if you add to this list, update the docs too.
1368+
cacheArgs = append(cacheArgs, arg)
1369+
1370+
case "-test.timeout":
1371+
// Special case: this is cacheable but ignored during the hash.
1372+
// Do not add to cacheArgs.
1373+
13651374
default:
13661375
// nothing else is cacheable
13671376
c.disableCache = true
@@ -1375,7 +1384,7 @@ func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bo
13751384
}
13761385

13771386
h := cache.NewHash("testResult")
1378-
fmt.Fprintf(h, "test binary %s args %q execcmd %q", id, testArgs, work.ExecCmd)
1387+
fmt.Fprintf(h, "test binary %s args %q execcmd %q", id, cacheArgs, work.ExecCmd)
13791388
// TODO(rsc): How to handle other test dependencies like environment variables or input files?
13801389
// We could potentially add new API like testing.UsedEnv(envName string)
13811390
// or testing.UsedFile(inputFile string) to let tests declare what external inputs

0 commit comments

Comments
 (0)