-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
How to handle failed expectations inside of a goroutine? #772
Comments
Maybe there should be documentation or a guide on using testify with goroutines in general. |
Hi, @ernesto-jimenez @boyan-soubachov as major contributors to the library, do you have any advice or good reading to follow? It's a bit dark what should be done? Also @trivigy @xordspar0 in the meantime, did you find the way to go? Thanks, EDIT: an old post has a "solution" but it seems a bit... chatty just for failing a test from a goroutine :/ ipfs/kubo#2043 (comment) |
Using testify The Go 1.16 release notes also speak about this because calling That method is good enough™️ because either the test will fail, or if the assertion fails after the test has "finshed" then the assertion will cause a panic. But that's not great, the next fix is to use proper synchronisation and the issue that @sneko linked to has reasonable examples of this. I think this is more of a Go issue than a testify one, but that's not to say testify shouldn't give helpful advice and examples. I'd consider doing:
func TestProbablyPanics(t *testing.T) {
go func() {
// Use assert in goroutines, not require.
assert.Equal(t, 1, 2)
}()
}
func TestFails(t *testing.T) {
syn := make(chan struct{})
// Allow goroutines to finish before the end of the test so their assertions
// are reported without a panic
defer func() { <-syn }()
go func() {
defer close(syn)
assert.Equal(t, 1, 2)
}()
} What do you think? |
Related to #1499 |
here is an interesting PR that rewrote the code using go routines in the tests |
I found some conversations about this topic but could not find any workarounds for this issue. If anyone knows I would greatly appreciate the help. Basically I simply want to fail an entire test from within a goroutine running inside of the test. Is there a way to do that and if so how do I do that?
The text was updated successfully, but these errors were encountered: