Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why can't some of my logs be output in goroutine? #24998

Closed
TaoSunkist opened this issue Apr 23, 2018 · 3 comments
Closed

Why can't some of my logs be output in goroutine? #24998

TaoSunkist opened this issue Apr 23, 2018 · 3 comments

Comments

@TaoSunkist
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version:go1.10

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/Users/<user_name>/DevStation/hi_golang/bin"
GOCACHE="/Users/<user_name>/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/<user_name>/DevStation/hi_golang"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/yh/tlxxvm450gzgv9yxbqtnqy5h0000gn/T/go-build971977641=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package golang

import (
    "testing"
    "net"
    "time"
    "fmt"
    "log"
)

func TestTcp1(t *testing.T) {
    connFunc := func(conn net.Conn, err error) {
        defer conn.Close()
        time.Sleep(time.Second * 3)
        t.Log("t.Log: ", conn, err)
        fmt.Println("fmt.Println: ", conn, err)
        log.Println("log.Println: ", conn, err)
    }

    if tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000, Zone: ""}); err != nil {
            t.Error(err)
     } else {
            defer tcpListener.Close()
            for {
                go connFunc(tcpListener.Accept())
            }
    }
}

What did you expect to see?

➜  golang go test -v -run Tcp1/                   
=== RUN   TestTcp1
fmt.Println:  &{{0xc4200bc100}} <nil>
2018/04/23 11:13:57 log.Println:  &{{0xc4200bc100}} <nil>

What did you see instead?

➜  golang go test -v -run Tcp1/                   
=== RUN   TestTcp1
t.Log:  &{{0xc4200bc100}} <nil>
fmt.Println:  &{{0xc4200bc100}} <nil>
2018/04/23 11:13:57 log.Println:  &{{0xc4200bc100}} <nil>

Why t.Log() doesn't have any output during the process ?
Thank you for any help.

@agnivade
Copy link
Contributor

I think you mixed up the "What did you expect to see" vs "What did you see instead". Anyways, t.Log() won't output until the test is finished, which in your case, is never.

Also, we do not use the issue tracker for general discussions or questions like these. The Github bug tracker is only used for tracking bugs and proposals going through the Proposal Process. You might want to take a look at #24929.

For further questions, please have a look at Questions wiki page; it has a list of good places for asking questions.

Closing since this is not a bug.

@TaoSunkist
Copy link
Author

Sorry, I don't understand, why log.Println can be displayed normally, and t.log cannot. However, when I simply use goroutine and use t.log to display the log, this is feasible.

@agnivade
Copy link
Contributor

Please look at the linked issue. We do not use the issue tracker for general discussions or questions.

For asking questions, see:

@golang golang locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants