@@ -1016,30 +1016,33 @@ func (c *common) log(s string) {
1016
1016
s = strings .ReplaceAll (s , "\n " , "\n " + indent )
1017
1017
s += "\n "
1018
1018
1019
+ // Select test nesting level for output.
1020
+ n := c .nest ()
1019
1021
// The test and all its parents are done and the log cannot be output.
1020
- if ! c . canOutput () {
1022
+ if n == nil {
1021
1023
panic ("Log in goroutine after " + c .name + " has completed: " + s )
1022
1024
}
1023
1025
1024
1026
// Prefix with the call site. It is located by skipping 3 functions:
1025
1027
// 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 ))
1028
1030
}
1029
1031
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 {
1032
1035
if ! c .done {
1033
- return true
1036
+ return c
1034
1037
}
1035
1038
for parent := c .parent ; parent != nil ; parent = parent .parent {
1036
1039
parent .mu .Lock ()
1037
1040
defer parent .mu .Unlock ()
1038
1041
if ! parent .done {
1039
- return true
1042
+ return parent
1040
1043
}
1041
1044
}
1042
- return false
1045
+ return nil
1043
1046
}
1044
1047
1045
1048
// callSite retrieves and formats the file and line of the call site.
@@ -1099,17 +1102,7 @@ func (o *outputWriter) Write(p []byte) (int, error) {
1099
1102
// writeLine generates the output for a given line.
1100
1103
func (o * outputWriter ) writeLine (b []byte ) {
1101
1104
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 ... )
1113
1106
} else {
1114
1107
if o .c .chatty != nil {
1115
1108
if o .c .bench {
0 commit comments