Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions openfeature/context_aware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ func TestContextAwareInitialization(t *testing.T) {
}
})

t.Run("cancelled context does not fail async setup", func(t *testing.T) {
// The non-blocking variants hand the context to the background
// initialization; they do not return an error when it is cancelled.
ctx, cancel := context.WithCancel(t.Context())
cancel()

if err := SetProviderWithContext(ctx, &testContextAwareProvider{}); err != nil {
t.Errorf("SetProviderWithContext with cancelled context: got %v, want nil", err)
}
if err := SetNamedProviderWithContext(ctx, "cancelled-domain", &testContextAwareProvider{}); err != nil {
t.Errorf("SetNamedProviderWithContext with cancelled context: got %v, want nil", err)
}
})

t.Run("named provider with context works", func(t *testing.T) {
namedProvider := &testContextAwareProvider{initDelay: 50 * time.Millisecond}

Expand Down
10 changes: 8 additions & 2 deletions openfeature/openfeature.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func SetProviderAndWait(provider FeatureProvider) error {
// SetProviderWithContext sets the default [FeatureProvider] with context-aware initialization.
// If the provider implements ContextAwareStateHandler, InitWithContext will be called with the provided context.
// Provider initialization is asynchronous and status can be checked from provider status.
// Returns an error immediately if provider is nil, or if context is cancelled during setup.
// Returns an error immediately if the provider is nil or is already bound to a different
// API instance. Initialization runs in the background, so a cancelled context does not make
// this function return an error; the context is passed to that background initialization,
// whose outcome is reported through provider status.
//
// Use this function for non-blocking provider setup with timeout control where you want
// to continue application startup while the provider initializes in background.
Expand Down Expand Up @@ -111,7 +114,10 @@ func SetNamedProviderAndWait(domain string, provider FeatureProvider) error {
// SetNamedProviderWithContext sets a [FeatureProvider] mapped to the given [Client] domain with context-aware initialization.
// If the provider implements ContextAwareStateHandler, InitWithContext will be called with the provided context.
// Provider initialization is asynchronous and status can be checked from provider status.
// Returns an error immediately if provider is nil, or if context is cancelled during setup.
// Returns an error immediately if the provider is nil or is already bound to a different
// API instance. Initialization runs in the background, so a cancelled context does not make
// this function return an error; the context is passed to that background initialization,
// whose outcome is reported through provider status.
//
// Named providers allow different domains to use different feature flag providers,
// enabling multi-tenant applications or microservice architectures.
Expand Down
Loading