You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- StopTrying now always signifies a failure
- StopTrying(message).Wrap(err) can wrap an error
- STopTrying(message).Attach(description, object) can attach arbitrary objects to the error report. These are rendered with Gomega's default formatter.
Copy file name to clipboardExpand all lines: docs/index.md
+20-31Lines changed: 20 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -492,24 +492,23 @@ When no explicit duration is provided, `Consistently` will use the default durat
492
492
493
493
### Bailing Out Early - Polling Functions
494
494
495
-
There are cases where you need to signal to `Eventually` and `Consistently` that they should stop trying. Gomega provides`StopTrying(format string, args ...any)` to allow you to send that signal. There are two ways to use `StopTrying`.
495
+
There are cases where you need to signal to `Eventually` and `Consistently` that they should stop trying. Gomega provides`StopTrying(message string)` to allow you to send that signal. There are two ways to use `StopTrying`.
496
496
497
497
First, you can return `StopTrying` as an error. Consider, for example, the case where `Eventually` is searching through a set of possible queries with a server:
498
498
499
499
```go
500
500
playerIndex, numPlayers:=0, 11
501
501
Eventually(func() (string, error) {
502
-
name:= client.FetchPlayer(playerIndex)
503
-
playerIndex += 1
504
502
if playerIndex == numPlayers {
505
-
return name, StopTrying("No more players left")
506
-
} else {
507
-
return name, nil
503
+
return"", StopTrying("no more players left")
508
504
}
505
+
name:= client.FetchPlayer(playerIndex)
506
+
playerIndex += 1
507
+
return name, nil
509
508
}).Should(Equal("Patrick Mahomes"))
510
509
```
511
510
512
-
Here we return a `StopTrying` error to tell `Eventually` that we've looked through all possible players and that it should stop. Note that `Eventually` will check last name returned by this function and succeed if that name is the desired name.
511
+
Here we return a `StopTrying` error to tell `Eventually` that we've looked through all possible players and that it should stop.
513
512
514
513
You can also call `StopTrying(...).Now()` to immediately end execution of the function. Consider, for example, the case of a client communicating with a server that experiences an irrevocable error:
calling `.Now()` will trigger a panic that will signal to `Eventually` that the it should stop trying.
525
+
calling `.Now()` will trigger a panic that will signal to `Eventually` that it should stop trying.
527
526
528
-
You can also use both verison of `StopTrying()`with `Consistently`. Since `Consistently` is validating that something is _true_ consitently for the entire requested duration sending a `StopTrying()` signal is interpreted as success. Here's a somewhat contrived example:
527
+
You can also return `StopTrying()`errors and use `StopTrying().Now()` with `Consistently`.
529
528
530
-
```go
531
-
go client.DoSomethingComplicated()
532
-
Consistently(func() int {
533
-
if client.Status() == client.DoneStatus {
534
-
StopTrying("Client finished").Now()
535
-
}
536
-
return client.NumErrors()
537
-
}).Should(Equal(0))
538
-
```
529
+
Both `Eventually` and `Consistently` always treat the `StopTrying()` signal as a failure. The failure message will include the message passed in to `StopTrying()`.
539
530
540
-
here we succeed because no errors were identified while the client was working.
531
+
You can add additional information to this failure message in a few ways. You can wrap an error via `StopTrying(message).Wrap(wrappedErr)` - now the output will read `<message>: <wrappedErr.Error()>`.
541
532
542
-
`StopTrying` also allows you wrap an error using the `%w` verb. For example:
533
+
You can also attach arbitrary objects to `StopTrying()` via `StopTrying(message).Attach(description string, object any)`. Gomega will run the object through Gomega's standard formatting library to build a consistent representation for end users. You can attach multiple objects in this way and the output will look like:
543
534
544
-
```go
545
-
Eventually(func() []string {
546
-
names, err:= client.FetchAllPlayers()
547
-
if err == client.TOKEN_EXPIRED || err == client.SEVER_GONE {
Wrapping an error in this way allows you to simultaneously signal that `Eventually` should stop trying _and_ that the assertion should count as a failure regardless of the state of the match when `StopTrying` is returned/thrown.
538
+
<message>: <wrappedErr.Error()>
539
+
<description>:
540
+
<formatted-object>
541
+
<description>:
542
+
<formatted-object>
543
+
```
555
544
556
545
### Bailing Out Early - Matchers
557
546
@@ -561,9 +550,9 @@ Just like functions being polled, matchers can also indicate if `Eventually`/`Co
If a matcher returns `StopTrying` for `error`, or calls `StopTrying(...).Now()`, `Eventually` and `Consistently` will stop polling and use the returned value of `success` to determine if the match succeeded or not. To signal that the `success` values should wrap an error via `StopTrying("<reason>: %w", err)`.
553
+
If a matcher returns `StopTrying` for `error`, or calls `StopTrying(...).Now()`, `Eventually` and `Consistently` will stop polling and fail: `StopTrying`**always** signifies a failure.
565
554
566
-
> Note: An older mechanism for doing this is documented in the [custom matchers section below](#aborting-eventuallyconsistently)
555
+
> Note: An alternative mechanism for having matchers bail out early is documented in the [custom matchers section below](#aborting-eventuallyconsistently). This mechanism, which entails implementing a `MatchMayChangeIntheFuture(<actual>) bool` method, allows matchers to signify that no future change is possible out-of-band of the call to the matcher.
0 commit comments