File tree 2 files changed +62
-10
lines changed
2 files changed +62
-10
lines changed Original file line number Diff line number Diff line change @@ -706,6 +706,55 @@ func TestRacyOutput(t *T) {
706
706
}
707
707
}
708
708
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
+
709
758
func TestBenchmark (t * T ) {
710
759
res := Benchmark (func (b * B ) {
711
760
for i := 0 ; i < 5 ; i ++ {
Original file line number Diff line number Diff line change @@ -618,17 +618,20 @@ func (c *common) log(s string) {
618
618
func (c * common ) logDepth (s string , depth int ) {
619
619
c .mu .Lock ()
620
620
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 {
631
622
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" )
632
635
}
633
636
}
634
637
You can’t perform that action at this time.
0 commit comments