-
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
14
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
14 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 0e53150
feat: move factory to isolated package and warn on multi-instance pro…
marcozabel 586e636
docs: add experimental @apiNote to OpenFeatureAPIFactory
marcozabel 2da34f5
test: add reverse-direction singleton isolation test
marcozabel 3a25c3c
test: add delay before negative assertion in event isolation test
marcozabel 4041ebc
test: use direct constructor in isolated tests
marcozabel 27ca0a2
refactor: remove OpenFeatureAPIFactory class
marcozabel 3e7374c
fix: replace @apiNote with prose to satisfy javadoc plugin
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
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
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
| 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.ThreadLocalTransactionContextPropagator; | ||
| 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 = new OpenFeatureAPI(); | ||
| 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(); | ||
| } | ||
|
|
||
| /** | ||
| * Requirement 1.8.1 — mutating the singleton does not affect a | ||
| * previously created isolated instance. | ||
| */ | ||
| @Test | ||
| @DisplayName("singleton does not interfere with isolated instance") | ||
| void singletonDoesNotInterfereWithIsolatedInstance() { | ||
| OpenFeatureAPI isolated = new OpenFeatureAPI(); | ||
|
|
||
| // record isolated baseline | ||
| FeatureProvider isolatedProvider = isolated.getProvider(); | ||
|
|
||
| // mutate only the singleton | ||
| singleton.setProvider(new InMemoryProvider(Map.of())); | ||
| singleton.addHooks(new NoOpHook()); | ||
| singleton.setEvaluationContext(new ImmutableContext("singleton-key")); | ||
| singleton.setTransactionContextPropagator(new ThreadLocalTransactionContextPropagator()); | ||
|
|
||
| // isolated instance remains at baseline | ||
| assertThat(isolated.getProvider()).isSameAs(isolatedProvider); | ||
| assertThat(isolated.getHooks()).isEmpty(); | ||
| assertThat(isolated.getEvaluationContext()).isNull(); | ||
| assertThat(isolated.getTransactionContextPropagator()).isInstanceOf(NoOpTransactionContextPropagator.class); | ||
| } | ||
| } | ||
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.
this.attachmentis not guaranteed to benullhere any more. If we want to ensure that, we need anAtomicReferenceand a cas operation