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
Copy file name to clipboardexpand all lines: docs/index.md
+50-17
Original file line number
Diff line number
Diff line change
@@ -308,7 +308,7 @@ In both cases you should always pass `Eventually` a function that, when polled,
308
308
309
309
#### Category 2: Making `Eventually` assertions on functions
310
310
311
-
`Eventually` can be passed functions that **take no arguments** and **return at least one value**. When configured this way, `Eventually` will poll the function repeatedly and pass the first returned value to the matcher.
311
+
`Eventually` can be passed functions that **return at least one value**. When configured this way, `Eventually` will poll the function repeatedly and pass the first returned value to the matcher.
312
312
313
313
For example:
314
314
@@ -322,7 +322,7 @@ will repeatedly poll `client.FetchCount` until the `BeNumerically` matcher is sa
322
322
323
323
> Note that this example could have been written as `Eventually(client.FetchCount).Should(BeNumerically(">=", 17))`
324
324
325
-
If multple values are returned by the function, `Eventually` will pass the first value to the matcher and require that all others are zero-valued. This allows you to pass `Eventually` a function that returns a value and an error - a common pattern in Go.
325
+
If multiple values are returned by the function, `Eventually` will pass the first value to the matcher and require that all others are zero-valued. This allows you to pass `Eventually` a function that returns a value and an error - a common pattern in Go.
326
326
327
327
For example, consider a method that returns a value and an error:
will pass only if and when the returned error is `nil` *and* the returned string satisfies the matcher.
340
340
341
+
342
+
Eventually can also accept functions that take arguments, however you must provide those arguments using `Eventually().WithArguments()`. For example, consider a function that takes a user-id and makes a network request to fetch a full name:
`WithArguments()` supports multiple arugments as well as variadic arguments.
355
+
341
356
It is important to note that the function passed into Eventually is invoked **synchronously** when polled. `Eventually` does not (in fact, it cannot) kill the function if it takes longer to return than `Eventually`'s configured timeout. This is where using a `context.Context` can be helpful. Here is an example that leverages Gingko's support for interruptible nodes and spec timeouts:
342
357
343
358
```go
344
359
It("fetches the correct count", func(ctx SpecContext) {
345
360
Eventually(func() int {
346
-
return client.FetchCount(ctx)
361
+
return client.FetchCount(ctx, "/users")
347
362
}, ctx).Should(BeNumerically(">=", 17))
348
363
}, SpecTimeout(time.Second))
349
364
```
350
365
351
-
now when the spec times out both the `client.FetchCount` function and `Eventually` will be signaled and told to exit.
366
+
now when the spec times out both the `client.FetchCount` function and `Eventually` will be signaled and told to exit. you an also use `Eventually().WithContext(ctx)` to provide the context.
367
+
368
+
369
+
Since functions that take a context.Context as a first-argument are common in Go, `Eventually` supports automatically injecting the provided context into the function. This plays nicely with `WithArguments()` as well. You can rewrite the above example as:
370
+
371
+
```go
372
+
It("fetches the correct count", func(ctx SpecContext) {
In addition, Gingko's `SpecContext` allows Goemga to tell Ginkgo about the status of a currently running `Eventually` whenever a Progress Report is generated. So, if a spec times out while running an `Eventually` Ginkgo will not only show you which `Eventually` was running when the timeout occured, but will also include the failure the `Eventually` was hitting when the timeout occurred.
392
+
In addition, Gingko's `SpecContext` allows Gomega to tell Ginkgo about the status of a currently running `Eventually` whenever a Progress Report is generated. So, if a spec times out while running an `Eventually` Ginkgo will not only show you which `Eventually` was running when the timeout occured, but will also include the failure the `Eventually` was hitting when the timeout occurred.
373
393
374
394
#### Category 3: Making assertions _in_ the function passed into `Eventually`
375
395
@@ -404,6 +424,19 @@ Eventually(func(g Gomega) {
404
424
405
425
will rerun the function until all assertions pass.
406
426
427
+
You can also pass additional arugments to functions that take a Gomega. The only rule is that the Gomega argument must be first. If you also want to pass the context attached to `Eventually` you must ensure that is the second argument. For example:
`Consistently` checks that an assertion passes for a period of time. It does this by polling its argument repeatedly during the period. It fails if the matcher ever fails during that period.
As with `Eventually`, the duration parameters can be `time.Duration`s, string representations of a `time.Duration` (e.g. `"200ms"`) or `float64`s that are interpreted as seconds.
426
459
427
-
Also as with `Eventually`, `Consistently` supports chaining `WithTimeout` and `WithPolling`and `WithContext` in the form of:
460
+
Also as with `Eventually`, `Consistently` supports chaining `WithTimeout`, `WithPolling`, `WithContext`and `WithArguments` in the form of:
`Consistently` tries to capture the notion that something "does not eventually" happen. A common use-case is to assert that no goroutine writes to a channel for a period of time. If you pass `Consistently` an argument that is not a function, it simply passes that argument to the matcher. So we can assert that:
Copy file name to clipboardexpand all lines: gomega_dsl.go
+26-3
Original file line number
Diff line number
Diff line change
@@ -266,7 +266,7 @@ this will trigger Go's race detector as the goroutine polling via Eventually wil
266
266
267
267
**Category 2: Make Eventually assertions on functions**
268
268
269
-
Eventually can be passed functions that **take no arguments** and **return at least one value**. When configured this way, Eventually will poll the function repeatedly and pass the first returned value to the matcher.
269
+
Eventually can be passed functions that **return at least one value**. When configured this way, Eventually will poll the function repeatedly and pass the first returned value to the matcher.
270
270
271
271
For example:
272
272
@@ -286,15 +286,27 @@ Then
286
286
287
287
will pass only if and when the returned error is nil *and* the returned string satisfies the matcher.
288
288
289
+
Eventually can also accept functions that take arguments, however you must provide those arguments using .WithArguments(). For example, consider a function that takes a user-id and makes a network request to fetch a full name:
It is important to note that the function passed into Eventually is invoked *synchronously* when polled. Eventually does not (in fact, it cannot) kill the function if it takes longer to return than Eventually's configured timeout. A common practice here is to use a context. Here's an example that combines Ginkgo's spec timeout support with Eventually:
290
296
291
297
It("fetches the correct count", func(ctx SpecContext) {
292
298
Eventually(func() int {
293
-
return client.FetchCount(ctx)
299
+
return client.FetchCount(ctx, "/users")
294
300
}, ctx).Should(BeNumerically(">=", 17))
295
301
}, SpecTimeout(time.Second))
296
302
297
-
now, when Ginkgo cancels the context both the FetchCount client and Gomega will be informed and can exit.
303
+
you an also use Eventually().WithContext(ctx) to pass in the context. Passed-in contexts play nicely with paseed-in arguments as long as the context appears first. You can rewrite the above example as:
304
+
305
+
It("fetches the correct count", func(ctx SpecContext) {
Either way the context passd to Eventually is also passed to the underlying funciton. Now, when Ginkgo cancels the context both the FetchCount client and Gomega will be informed and can exit.
298
310
299
311
**Category 3: Making assertions _in_ the function passed into Eventually**
300
312
@@ -324,6 +336,17 @@ For example:
324
336
325
337
will rerun the function until all assertions pass.
326
338
339
+
You can also pass additional arugments to functions that take a Gomega. The only rule is that the Gomega argument must be first. If you also want to pass the context attached to Eventually you must ensure that is the second argument. For example:
returnfmt.Errorf(`The function passed to %s requested a context.Context, but no context has been provided to %s. Please pass one in using %s().WithContext().
returnfmt.Errorf(`The function passed to %s requested a context.Context, but no context has been provided. Please pass one in using %s().WithContext().
150
156
151
157
You can learn more at https://onsi.github.io/gomega/#eventually
returnfmt.Errorf(`The function passed to %s has signature %s takes %d arguments but %d %s been provided. Please use %s().WithArguments() to pass the corect set of arguments.
167
+
168
+
You can learn more at https://onsi.github.io/gomega/#eventually
0 commit comments