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

Fix docs for handling failures in goroutines #1339

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/css/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ a:last-of-type .logo {
}

#left-background {
grid-area; left-background;
grid-area: left-background;
}

#right-background {
grid-area; right-background;
grid-area: right-background;
}

#header {
Expand Down
19 changes: 18 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1202,15 +1202,32 @@ When a failure occurs in a `BeforeEach`, `JustBeforeEach`, or `It` closure Ginkg

Ginkgo orchestrates this behavior by rescuing the panic thrown by `Fail` and unwinding the spec. However, if your spec launches a **goroutine** that calls `Fail` (or, equivalently, invokes a failing Gomega assertion), there's no way for Ginkgo to rescue the panic that `Fail` throws. This will cause the suite to panic and no subsequent specs will run. To get around this you must rescue the panic using `defer GinkgoRecover()`. Here's an example:

However, if you block in a test case, Ginkgo will not be able to catch the failure and the case will time out instead.

```go
/* === INVALID === */
It("panics in a goroutine", func() {
var c chan interface{}
go func() {
defer GinkgoRecover()
Fail("boom")
close(c)
}()
<-c
<-c // Do not block!
})
```

[Asynchronous assertions](https://onsi.github.io/gomega/#making-asynchronous-assertions) can be used to wait for the condition while allowing Ginkgo to abort the case when an async failure occurs.

```go
It("panics in a goroutine", func() {
done := make(chan struct{})
go func() {
defer GinkgoRecover()
Fail("boom")
close(c)
}()
Eventually(done).Should(BeClosed())
})
```

Expand Down