Skip to content

Commit ca36da1

Browse files
toshipponsi
authored andcommitted
Export EnforceDefaultTimeoutsWhenUsingContexts and DisableDefaultTimeoutsWhenUsingContext
fix: #790 Signed-off-by: Toshikuni Fukaya <[email protected]>
1 parent d6331f9 commit ca36da1

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ SetDefaultConsistentlyPollingInterval(t time.Duration)
604604

605605
You can also adjust these global timeouts by setting the `GOMEGA_DEFAULT_EVENTUALLY_TIMEOUT`, `GOMEGA_DEFAULT_EVENTUALLY_POLLING_INTERVAL`, `GOMEGA_DEFAULT_CONSISTENTLY_DURATION`, and `GOMEGA_DEFAULT_CONSISTENTLY_POLLING_INTERVAL` environment variables to a parseable duration string. The environment variables have a lower precedence than `SetDefault...()`.
606606

607-
As discussed [above](#category-2-making-eventually-assertions-on-functions) `Eventually`s that are passed a `context` object without an explicit timeout will only stop polling when the context is cancelled. If you would like to enforce the default timeout when a context is provided you can call `EnforceDefaultTimeoutsWhenUsingContexts()` (to go back to the default behavior call `DoNotEnforceDefaultTimeoutsWhenUsingContexts()`). You can also set the `GOMEGA_ENFORCE_DEFAULT_TIMEOUTS_WHEN_USING_CONTEXTS` environment variable to enforce the default timeout when a context is provided.
607+
As discussed [above](#category-2-making-eventually-assertions-on-functions) `Eventually`s that are passed a `context` object without an explicit timeout will only stop polling when the context is cancelled. If you would like to enforce the default timeout when a context is provided you can call `EnforceDefaultTimeoutsWhenUsingContexts()` (to go back to the default behavior call `DisableDefaultTimeoutsWhenUsingContexts()`). You can also set the `GOMEGA_ENFORCE_DEFAULT_TIMEOUTS_WHEN_USING_CONTEXTS` environment variable to enforce the default timeout when a context is provided.
608608

609609
## Making Assertions in Helper Functions
610610

gomega_dsl.go

+10
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ func SetDefaultConsistentlyPollingInterval(t time.Duration) {
503503
Default.SetDefaultConsistentlyPollingInterval(t)
504504
}
505505

506+
// EnforceDefaultTimeoutsWhenUsingContexts forces `Eventually` to apply a default timeout even when a context is provided.
507+
func EnforceDefaultTimeoutsWhenUsingContexts() {
508+
Default.EnforceDefaultTimeoutsWhenUsingContexts()
509+
}
510+
511+
// DisableDefaultTimeoutsWhenUsingContext disables the default timeout when a context is provided to `Eventually`.
512+
func DisableDefaultTimeoutsWhenUsingContext() {
513+
Default.DisableDefaultTimeoutsWhenUsingContext()
514+
}
515+
506516
// AsyncAssertion is returned by Eventually and Consistently and polls the actual value passed into Eventually against
507517
// the matcher passed to the Should and ShouldNot methods.
508518
//

internal/dsl_test.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func setGlobalDurationBundle(bundle internal.DurationBundle) {
1919
SetDefaultEventuallyPollingInterval(bundle.EventuallyPollingInterval)
2020
SetDefaultConsistentlyDuration(bundle.ConsistentlyDuration)
2121
SetDefaultConsistentlyPollingInterval(bundle.ConsistentlyPollingInterval)
22+
if bundle.EnforceDefaultTimeoutsWhenUsingContexts {
23+
EnforceDefaultTimeoutsWhenUsingContexts()
24+
} else {
25+
DisableDefaultTimeoutsWhenUsingContext()
26+
}
2227
}
2328

2429
var _ = Describe("Gomega DSL", func() {
@@ -51,10 +56,11 @@ var _ = Describe("Gomega DSL", func() {
5156
Describe("NewGomega", func() {
5257
It("creates and configures a new Gomega, using the global duration bundle", func() {
5358
bundle := internal.DurationBundle{
54-
EventuallyTimeout: time.Minute,
55-
EventuallyPollingInterval: 2 * time.Minute,
56-
ConsistentlyDuration: 3 * time.Minute,
57-
ConsistentlyPollingInterval: 4 * time.Minute,
59+
EventuallyTimeout: time.Minute,
60+
EventuallyPollingInterval: 2 * time.Minute,
61+
ConsistentlyDuration: 3 * time.Minute,
62+
ConsistentlyPollingInterval: 4 * time.Minute,
63+
EnforceDefaultTimeoutsWhenUsingContexts: true,
5864
}
5965
setGlobalDurationBundle(bundle)
6066

@@ -74,10 +80,11 @@ var _ = Describe("Gomega DSL", func() {
7480
Describe("NewWithT", func() {
7581
It("creates and configure a new Gomega with the passed-in T, using the global duration bundle", func() {
7682
bundle := internal.DurationBundle{
77-
EventuallyTimeout: time.Minute,
78-
EventuallyPollingInterval: 2 * time.Minute,
79-
ConsistentlyDuration: 3 * time.Minute,
80-
ConsistentlyPollingInterval: 4 * time.Minute,
83+
EventuallyTimeout: time.Minute,
84+
EventuallyPollingInterval: 2 * time.Minute,
85+
ConsistentlyDuration: 3 * time.Minute,
86+
ConsistentlyPollingInterval: 4 * time.Minute,
87+
EnforceDefaultTimeoutsWhenUsingContexts: true,
8188
}
8289
setGlobalDurationBundle(bundle)
8390

@@ -191,16 +198,18 @@ var _ = Describe("Gomega DSL", func() {
191198
Describe("specifying default durations globally", func() {
192199
It("should update the durations on the Default gomega", func() {
193200
bundle := internal.DurationBundle{
194-
EventuallyTimeout: time.Minute,
195-
EventuallyPollingInterval: 2 * time.Minute,
196-
ConsistentlyDuration: 3 * time.Minute,
197-
ConsistentlyPollingInterval: 4 * time.Minute,
201+
EventuallyTimeout: time.Minute,
202+
EventuallyPollingInterval: 2 * time.Minute,
203+
ConsistentlyDuration: 3 * time.Minute,
204+
ConsistentlyPollingInterval: 4 * time.Minute,
205+
EnforceDefaultTimeoutsWhenUsingContexts: true,
198206
}
199207

200208
SetDefaultEventuallyTimeout(bundle.EventuallyTimeout)
201209
SetDefaultEventuallyPollingInterval(bundle.EventuallyPollingInterval)
202210
SetDefaultConsistentlyDuration(bundle.ConsistentlyDuration)
203211
SetDefaultConsistentlyPollingInterval(bundle.ConsistentlyPollingInterval)
212+
EnforceDefaultTimeoutsWhenUsingContexts()
204213

205214
Ω(Default.(*internal.Gomega).DurationBundle).Should(Equal(bundle))
206215
})

types/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Gomega interface {
2929
SetDefaultEventuallyPollingInterval(time.Duration)
3030
SetDefaultConsistentlyDuration(time.Duration)
3131
SetDefaultConsistentlyPollingInterval(time.Duration)
32+
EnforceDefaultTimeoutsWhenUsingContexts()
33+
DisableDefaultTimeoutsWhenUsingContext()
3234
}
3335

3436
// All Gomega matchers must implement the GomegaMatcher interface

0 commit comments

Comments
 (0)