Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 006a5e7

Browse files
committedJan 17, 2019
testing: report the failing test in a late log panic
Updates #29388 Change-Id: Icb0e6048d05fde7a5486b923ff62147edb5c8dac Reviewed-on: https://go-review.googlesource.com/c/157617 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 0456036 commit 006a5e7

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed
 

‎src/testing/sub_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,55 @@ func TestRacyOutput(t *T) {
706706
}
707707
}
708708

709+
// The late log message did not include the test name. Issue 29388.
710+
func TestLogAfterComplete(t *T) {
711+
ctx := newTestContext(1, newMatcher(regexp.MatchString, "", ""))
712+
var buf bytes.Buffer
713+
t1 := &T{
714+
common: common{
715+
// Use a buffered channel so that tRunner can write
716+
// to it although nothing is reading from it.
717+
signal: make(chan bool, 1),
718+
w: &buf,
719+
},
720+
context: ctx,
721+
}
722+
723+
c1 := make(chan bool)
724+
c2 := make(chan string)
725+
tRunner(t1, func(t *T) {
726+
t.Run("TestLateLog", func(t *T) {
727+
go func() {
728+
defer close(c2)
729+
defer func() {
730+
p := recover()
731+
if p == nil {
732+
c2 <- "subtest did not panic"
733+
return
734+
}
735+
s, ok := p.(string)
736+
if !ok {
737+
c2 <- fmt.Sprintf("subtest panic with unexpected value %v", p)
738+
return
739+
}
740+
const want = "Log in goroutine after TestLateLog has completed"
741+
if !strings.Contains(s, want) {
742+
c2 <- fmt.Sprintf("subtest panic %q does not contain %q", s, want)
743+
}
744+
}()
745+
746+
<-c1
747+
t.Log("log after test")
748+
}()
749+
})
750+
})
751+
close(c1)
752+
753+
if s := <-c2; s != "" {
754+
t.Error(s)
755+
}
756+
}
757+
709758
func TestBenchmark(t *T) {
710759
res := Benchmark(func(b *B) {
711760
for i := 0; i < 5; i++ {

‎src/testing/testing.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -618,17 +618,20 @@ func (c *common) log(s string) {
618618
func (c *common) logDepth(s string, depth int) {
619619
c.mu.Lock()
620620
defer c.mu.Unlock()
621-
// If this test has already finished try and log this message with our parent
622-
// with this test name tagged so we know where it came from.
623-
// If we don't have a parent panic.
624-
if c.done {
625-
if c.parent != nil {
626-
c.parent.logDepth(s, depth+1)
627-
} else {
628-
panic("Log in goroutine after " + c.name + " has completed")
629-
}
630-
} else {
621+
if !c.done {
631622
c.output = append(c.output, c.decorate(s, depth+1)...)
623+
} else {
624+
// This test has already finished. Try and log this message
625+
// with our parent. If we don't have a parent, panic.
626+
for parent := c.parent; parent != nil; parent = parent.parent {
627+
parent.mu.Lock()
628+
defer parent.mu.Unlock()
629+
if !parent.done {
630+
parent.output = append(parent.output, parent.decorate(s, depth+1)...)
631+
return
632+
}
633+
}
634+
panic("Log in goroutine after " + c.name + " has completed")
632635
}
633636
}
634637

0 commit comments

Comments
 (0)
Please sign in to comment.