diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-10 10:32:15 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-14 09:54:12 +0300 |
commit | 8f5e825786ef80fdba4b8e93acc05a9fb9aae8a0 (patch) | |
tree | 030a1098c9b1aede316701d5eaa34a35cc62ffcd /internal | |
parent | 9731244a9e5e53d8d469ca9ca8b0fac5049a2e21 (diff) |
testhelper: Add ability to create uncancellable contexts
In some contexts, we want to create test contexts which are not
cancellable to exercise certain behaviour in case it is missing. It's
still useful to inject feature flags in these cases though, so one
shouldn't just reach for `context.Background()` in such a case.
Introduce a new function `ContextWithoutCancel()` to address this
usecase.
Diffstat (limited to 'internal')
-rw-r--r-- | internal/testhelper/testhelper.go | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index f9f3656fd..b98a7f4d8 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -168,7 +168,12 @@ func ContextWithLogger(logger *log.Entry) ContextOpt { // Context returns a cancellable context. func Context(opts ...ContextOpt) (context.Context, func()) { - ctx, cancel := context.WithCancel(context.Background()) + return context.WithCancel(ContextWithoutCancel(opts...)) +} + +// ContextWithoutCancel returns a non-cancellable context. +func ContextWithoutCancel(opts ...ContextOpt) context.Context { + ctx := context.Background() // Enable use of explicit feature flags. Each feature flag which is checked must have been // explicitly injected into the context, or otherwise we panic. This is a sanity check to @@ -184,7 +189,7 @@ func Context(opts ...ContextOpt) (context.Context, func()) { ctx = opt(ctx) } - return ctx, cancel + return ctx } // CreateGlobalDirectory creates a directory in the test directory that is shared across all |