-
Notifications
You must be signed in to change notification settings - Fork 55
feat: support isolated API instances #1928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e7b91f3
b084f9c
3815faa
da9d472
7b9b39d
5f14bb9
c06e500
0e53150
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package dev.openfeature.sdk.isolated; | ||
|
|
||
| import dev.openfeature.sdk.OpenFeatureAPI; | ||
|
|
||
| /** | ||
| * Factory for creating isolated OpenFeature API instances. | ||
| * | ||
| * <p>Each instance returned by {@link #createAPI()} maintains its own state, | ||
| * including providers, evaluation context, hooks, event handlers, and | ||
| * transaction context propagators. Instances do not share state with the | ||
| * global singleton ({@link OpenFeatureAPI#getInstance()}) or with each other. | ||
| * | ||
| * <p>This class lives in a distinct package ({@code dev.openfeature.sdk.isolated}) | ||
| * to make isolated instances intentionally less discoverable than the global | ||
| * singleton, reducing the chance of accidental use when the singleton would be | ||
| * appropriate. | ||
| * | ||
| * <p>This is useful for dependency injection frameworks, testing scenarios, | ||
| * and applications composed of multiple submodules requiring distinct providers. | ||
| * | ||
| * <p><strong>Spec references:</strong> | ||
| * <ul> | ||
| * <li>Requirement 1.8.1 — factory function for isolated instances</li> | ||
| * <li>Requirement 1.8.3 — factory in a distinct package/module</li> | ||
| * </ul> | ||
| * | ||
| * @see <a href="https://openfeature.dev/specification/sections/flag-evaluation#18-isolated-api-instances"> | ||
| * Spec §1.8 — Isolated API Instances</a> | ||
| */ | ||
| public final class OpenFeatureAPIFactory { | ||
|
|
||
| private OpenFeatureAPIFactory() { | ||
| // utility class | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new, independent {@link OpenFeatureAPI} instance with fully | ||
| * isolated state. | ||
| * | ||
| * <p>Usage: | ||
| * <pre>{@code | ||
| * OpenFeatureAPI api = OpenFeatureAPIFactory.createAPI(); | ||
| * api.setProvider(new MyProvider()); | ||
| * Client client = api.getClient(); | ||
| * }</pre> | ||
| * | ||
| * @return a new API instance | ||
| */ | ||
| public static OpenFeatureAPI createAPI() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully see the point of this method/class.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree |
||
| return new OpenFeatureAPI(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package dev.openfeature.sdk.isolated; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import dev.openfeature.sdk.FeatureProvider; | ||
| import dev.openfeature.sdk.ImmutableContext; | ||
| import dev.openfeature.sdk.NoOpTransactionContextPropagator; | ||
| import dev.openfeature.sdk.OpenFeatureAPI; | ||
| import dev.openfeature.sdk.providers.memory.InMemoryProvider; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class IsolatedAPISingeltonTest { | ||
|
|
||
| private final OpenFeatureAPI singleton = OpenFeatureAPI.getInstance(); | ||
|
|
||
| @AfterEach | ||
| void restoreSingleton() { | ||
| singleton.shutdown(); | ||
| singleton.clearHooks(); | ||
| singleton.setEvaluationContext(null); | ||
| singleton.setTransactionContextPropagator(new NoOpTransactionContextPropagator()); | ||
| } | ||
|
|
||
| /** | ||
| * Requirement 1.8.1 — isolated instances do not share state with | ||
| * the global singleton. | ||
| */ | ||
| @Test | ||
| @DisplayName("isolated instance does not interfere with singleton") | ||
| void isolatedInstanceDoesNotInterfereWithSingleton() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests that mutating an isolated instance doesn't affect the singleton, which is great. Worth adding the reverse direction too; e.g. set a provider/hook/context on the singleton and assert a previously created isolated instance is unaffected. |
||
| // record singleton baseline | ||
| FeatureProvider singletonProvider = singleton.getProvider(); | ||
|
|
||
| OpenFeatureAPI isolated = OpenFeatureAPIFactory.createAPI(); | ||
| assertThat(isolated).isNotSameAs(singleton); | ||
|
|
||
| // mutate only the isolated instance | ||
| isolated.setProvider(new InMemoryProvider(Map.of())); | ||
| isolated.addHooks(new NoOpHook()); | ||
| isolated.setEvaluationContext(new ImmutableContext("isolated-key")); | ||
|
|
||
| // singleton remains at baseline | ||
| assertThat(singleton.getProvider()).isSameAs(singletonProvider); | ||
| assertThat(singleton.getHooks()).isEmpty(); | ||
| assertThat(singleton.getEvaluationContext()).isNull(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Section 1.8 is marked experimental in the spec. Might be worth adding an
@apiNoteto flag that, e.g.:Could go on the class-level Javadoc or on
createAPI()itself (or both).