Skip to content

Commit ad5d2f6

Browse files
rscgopherbot
authored andcommitted
testing: print names of running tests on test timeout
Track the running tests and when they started, so that we can report the running tests on a test timeout. % go test -timeout=5s panic: test timed out after 5s running tests: TestTCPSpuriousConnSetupCompletion (4s) ... stack traces as usual ... % go test -run=Script -timeout=10s cmd/go vcs-test.golang.org rerouted to http://127.0.0.1:65168 https://vcs-test.golang.org rerouted to https://127.0.0.1:65169 go test proxy running at GOPROXY=http://127.0.0.1:65170/mod panic: test timed out after 10s running tests: TestScript (10s) TestScript/mod_get_patchcycle (0s) TestScript/mod_get_prefer_incompatible (0s) TestScript/mod_get_promote_implicit (0s) TestScript/mod_get_pseudo (0s) TestScript/mod_get_pseudo_other_branch (0s) TestScript/mod_get_pseudo_prefix (0s) TestScript/mod_get_test (0s) TestScript/mod_get_trailing_slash (0s) TestScript/mod_get_update_unrelated_sum (0s) TestScript/mod_gobuild_import (0s) TestScript/mod_gomodcache (0s) TestScript/mod_gonoproxy (0s) TestScript/mod_load_badchain (0s) TestScript/mod_overlay (0s) TestScript/test_fuzz_minimize (6s) TestScript/test_fuzz_minimize_dirty_cov (7s) ... stack traces as usual ... Change-Id: I3a6647c029097becc06664ebd76a2597c7ed7b8e Reviewed-on: https://go-review.googlesource.com/c/go/+/446176 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Russ Cox <[email protected]>
1 parent 02cffcd commit ad5d2f6

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/testing/testing.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ import (
381381
"runtime"
382382
"runtime/debug"
383383
"runtime/trace"
384+
"sort"
384385
"strconv"
385386
"strings"
386387
"sync"
@@ -477,6 +478,8 @@ var (
477478
testlogFile *os.File
478479

479480
numFailed atomic.Uint32 // number of test failures
481+
482+
running sync.Map // map[string]time.Time of running, unpaused tests
480483
)
481484

482485
type chattyFlag struct {
@@ -1363,6 +1366,7 @@ func (t *T) Parallel() {
13631366
if t.chatty != nil {
13641367
t.chatty.Updatef(t.name, "=== PAUSE %s\n", t.name)
13651368
}
1369+
running.Delete(t.name)
13661370

13671371
t.signal <- true // Release calling test.
13681372
<-t.parent.barrier // Wait for the parent test to complete.
@@ -1371,6 +1375,7 @@ func (t *T) Parallel() {
13711375
if t.chatty != nil {
13721376
t.chatty.Updatef(t.name, "=== CONT %s\n", t.name)
13731377
}
1378+
running.Store(t.name, time.Now())
13741379

13751380
t.start = time.Now()
13761381
t.raceErrors += -race.Errors()
@@ -1474,15 +1479,16 @@ func tRunner(t *T, fn func(t *T)) {
14741479
// complete even if a cleanup function calls t.FailNow. See issue 41355.
14751480
didPanic := false
14761481
defer func() {
1482+
// Only report that the test is complete if it doesn't panic,
1483+
// as otherwise the test binary can exit before the panic is
1484+
// reported to the user. See issue 41479.
14771485
if didPanic {
14781486
return
14791487
}
14801488
if err != nil {
14811489
panic(err)
14821490
}
1483-
// Only report that the test is complete if it doesn't panic,
1484-
// as otherwise the test binary can exit before the panic is
1485-
// reported to the user. See issue 41479.
1491+
running.Delete(t.name)
14861492
t.signal <- signal
14871493
}()
14881494

@@ -1595,6 +1601,8 @@ func (t *T) Run(name string, f func(t *T)) bool {
15951601
if t.chatty != nil {
15961602
t.chatty.Updatef(t.name, "=== RUN %s\n", t.name)
15971603
}
1604+
running.Store(t.name, time.Now())
1605+
15981606
// Instead of reducing the running count of this test before calling the
15991607
// tRunner and increasing it afterwards, we rely on tRunner keeping the
16001608
// count correct. This ensures that a sequence of sequential tests runs
@@ -2201,11 +2209,33 @@ func (m *M) startAlarm() time.Time {
22012209
m.timer = time.AfterFunc(*timeout, func() {
22022210
m.after()
22032211
debug.SetTraceback("all")
2204-
panic(fmt.Sprintf("test timed out after %v", *timeout))
2212+
extra := ""
2213+
2214+
if list := runningList(); len(list) > 0 {
2215+
var b strings.Builder
2216+
b.WriteString("\nrunning tests:")
2217+
for _, name := range list {
2218+
b.WriteString("\n\t")
2219+
b.WriteString(name)
2220+
}
2221+
extra = b.String()
2222+
}
2223+
panic(fmt.Sprintf("test timed out after %v%s", *timeout, extra))
22052224
})
22062225
return deadline
22072226
}
22082227

2228+
// runningList returns the list of running tests.
2229+
func runningList() []string {
2230+
var list []string
2231+
running.Range(func(k, v any) bool {
2232+
list = append(list, fmt.Sprintf("%s (%v)", k.(string), time.Since(v.(time.Time)).Round(time.Second)))
2233+
return true
2234+
})
2235+
sort.Strings(list)
2236+
return list
2237+
}
2238+
22092239
// stopAlarm turns off the alarm.
22102240
func (m *M) stopAlarm() {
22112241
if *timeout > 0 {

0 commit comments

Comments
 (0)