-
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
Open
marcozabel
wants to merge
7
commits into
open-feature:main
Choose a base branch
from
marcozabel:feat/isolated-api-instances
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7b91f3
feat: support isolated API instances
marcozabel b084f9c
fix(tests): separate singleton tests
marcozabel 3815faa
fix: spotless formatting and arch test compliance
marcozabel da9d472
refactor(test): inject lock via constructor instead of field mutation
marcozabel 7b9b39d
refactor: make OpenFeatureAPI lock field final
marcozabel 5f14bb9
refactor: move OpenFeatureAPIFactory to sdk package and remove create…
marcozabel c06e500
refactor: bundle EventProvider onEmit and lock into atomic attachment
marcozabel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/dev/openfeature/sdk/OpenFeatureAPIFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package dev.openfeature.sdk; | ||
|
|
||
| /** | ||
| * 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 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> | ||
| * </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() { | ||
| return new OpenFeatureAPI(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/dev/openfeature/sdk/isolated/IsolatedAPISingeltonTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 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.OpenFeatureAPIFactory; | ||
| 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() { | ||
| // 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't fully see the point of this method/class.
OpenFeatureAPI.createIsolated()has the exact same visibility level and does the same thing. I think this will only lead to confusion