testing: loses log messages made in a goroutine after test has completed #30389
Comments
Hey, I tried investigating and wrote a test and modifying various places in I might be naively making a mistake here but I guess after the func TestLateLogWrapper(t *testing.T) {
testenv.MustHaveGoBuild(t)
tempDir, err := ioutil.TempDir("", "issue-30389")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)
code := `
package main
import (
"testing"
"time"
"fmt"
)
func TestLateLog(t *testing.T) {
c := make(chan bool)
go func(t *testing.T) {
<-c
time.Sleep(10*time.Millisecond)
fmt.Printf("-> %v\n", t)
t.Log("log after test")
}(t)
close(c)
}
`
destFile := filepath.Join(tempDir, "issue30389_test.go")
if err := ioutil.WriteFile(destFile, []byte(code), 0644); err != nil {
t.Fatalf("Failed to save test code: %v", err)
}
got, err := exec.Command(testenv.GoToolPath(t), "test", destFile).CombinedOutput()
if err == nil {
t.Fatal("The command unexpectedly passed")
}
want := []byte(`xxx`)
if !bytes.HasPrefix(got, want) {
t.Errorf("Did not print previous output\nGot:\n%s\n\nWant prefix:\n%s", got, want)
}
} Another thing I had a doubt about was, if we are to add a panic before the following to notify users that this information will be lost (or even for debugging if we reach there for this specific case) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The testing package silently drops
t.Log
messages that are made by a goroutine after the test has completed. For example, the following test passes and logs nothing.This should be fixed by extending the code added in 1.12 that panics if a goroutine logs after a test has completed to not silently discard the log message. The silent discarding is occurring in the current code because every test runs in a parent context, and the output accumulated by the parent context is discarded.
The text was updated successfully, but these errors were encountered: