Skip to content

Commit 91934a0

Browse files
author
suntala
committed
Refactor selection of nesting level for test output
1 parent d4b63d6 commit 91934a0

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/testing/testing.go

+12-19
Original file line numberDiff line numberDiff line change
@@ -1016,30 +1016,33 @@ func (c *common) log(s string) {
10161016
s = strings.ReplaceAll(s, "\n", "\n"+indent)
10171017
s += "\n"
10181018

1019+
// Select test nesting level for output.
1020+
n := c.nest()
10191021
// The test and all its parents are done and the log cannot be output.
1020-
if !c.canOutput() {
1022+
if n == nil {
10211023
panic("Log in goroutine after " + c.name + " has completed: " + s)
10221024
}
10231025

10241026
// Prefix with the call site. It is located by skipping 3 functions:
10251027
// callSite + log + public function
1026-
s = c.callSite(3) + s
1027-
c.o.Write([]byte(s))
1028+
s = n.callSite(3) + s
1029+
n.o.Write([]byte(s))
10281030
}
10291031

1030-
// canOutput checks if the test or one of its parents is incomplete.
1031-
func (c *common) canOutput() bool {
1032+
// nest selects the nesting level for test output. It returns the test if it is
1033+
// incomplete. Otherwise, it finds its closest incomplete parent.
1034+
func (c *common) nest() *common {
10321035
if !c.done {
1033-
return true
1036+
return c
10341037
}
10351038
for parent := c.parent; parent != nil; parent = parent.parent {
10361039
parent.mu.Lock()
10371040
defer parent.mu.Unlock()
10381041
if !parent.done {
1039-
return true
1042+
return parent
10401043
}
10411044
}
1042-
return false
1045+
return nil
10431046
}
10441047

10451048
// callSite retrieves and formats the file and line of the call site.
@@ -1099,17 +1102,7 @@ func (o *outputWriter) Write(p []byte) (int, error) {
10991102
// writeLine generates the output for a given line.
11001103
func (o *outputWriter) writeLine(b []byte) {
11011104
if o.c.done {
1102-
// This test has already finished. Try and log this message
1103-
// with our parent. If we don't have a parent, panic.
1104-
for parent := o.c.parent; parent != nil; parent = parent.parent {
1105-
parent.mu.Lock()
1106-
defer parent.mu.Unlock()
1107-
if !parent.done {
1108-
parent.output = append(parent.output, b...)
1109-
return
1110-
}
1111-
}
1112-
panic("Log in goroutine after " + o.c.name + " has completed: " + string(bytes.TrimSpace(b)))
1105+
o.c.output = append(o.c.output, b...)
11131106
} else {
11141107
if o.c.chatty != nil {
11151108
if o.c.bench {

0 commit comments

Comments
 (0)