-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
using t.Fatal in a goroutine #2043
Comments
@whyrusleeping is there something else that should be used instead? |
the errors need to be somehow propogated back to the main goroutine (the one executing the Test* function) and |
Ok, thanks! |
Here are some examples of what i do: example: func TestSimpleAsync(t *testing.T) {
// use a channel to hand off the error
errs := make(chan error, 1)
go func() {
err := somethingAsync()
errs <- err
}()
// wait for it
err := <-errs
if err != nil {
t.Fatal(err)
}
} func TestNAsync(t *testing.T) {
// we're going to have N children
N := 10
// use a channel to hand off the error
errs := make(chan error, N)
for i := 0; i < N; i++ {
go func() {
err := somethingAsync()
errs <- err
}()
}
// wait for all N to finish
for i := 0; i < N; i++ {
err := <-errs
if err != nil {
t.Fatal(err)
}
}
} func TestManyAsync(t *testing.T) {
var wg sync.WaitGroup
errs := make(chan error, 10)
for shouldKeepGoing() {
wg.Add(1)
go func() {
defer wg.Done()
err := somethingAsync()
if err != nil {
errs <- err
}
}()
}
// close errs once all children finish.
// wg allows us to do this without knowing
// how many children a priori.
go func() {
wg.Wait()
close(errs)
}()
for err := range errs {
if err != nil {
t.Fatal(err)
}
}
} |
We should verify that all occurences of this have been resolved, then close the issue |
Closing because, realistically, nobody's going to audit every one of our repos for this. |
Apparently (it makes sense thinking about it) you can't use
t.Fatal
in a goroutine.A cursory search reveals at least a few occurences of this bug: https://gist.github.com/whyrusleeping/6ff875746eccfc2611ab
Lots of them in our dependencies unfortunately, but a few in our code:
exchange/bitswap/bitswap_test.go-173
fuse/readonly/ipfs_test.go-160
merkledag/merkledag_test.go-198-
test/supernode_client/main.go-145-
test/supernode_client/main.go-183
The text was updated successfully, but these errors were encountered: