This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Added a "TestFeatureStore" #51
Merged
Merged
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/launchdarkly/client/TestFeatureStore.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,40 @@ | ||
| package com.launchdarkly.client; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * A decorated {@link InMemoryFeatureStore} which provides functionality to create (or override) "on" or "off" feature flags for all users. | ||
| * | ||
| * Using this store is useful for testing purposes when you want to have runtime support for turning specific features "on" or "off". | ||
| * | ||
| */ | ||
| public class TestFeatureStore extends InMemoryFeatureStore { | ||
|
|
||
| private AtomicInteger version = new AtomicInteger(0); | ||
|
|
||
| /** | ||
| * Turns a feature, identified by key, "on" for every user. If the feature rules already exist in the store then it will override it to be "on" for every {@link LDUser}. | ||
| * If the feature rule is not currently in the store, it will create one that is "on" for every {@link LDUser}. | ||
| * | ||
| * @param key the key of the feature flag to be "on". | ||
| */ | ||
| public void turnFeatureOn(String key) { | ||
| writeFeatureRep(key, new Variation.Builder<>(true, 100).build()); | ||
| } | ||
|
|
||
| /** | ||
| * Turns a feature, identified by key, "off" for every user. If the feature rules already exists in the store then it will override it to be "off" for every {@link LDUser}. | ||
| * If the feature rule is not currently in the store, it will create one that is "off" for every {@link LDUser}. | ||
| * | ||
| * @param key the key of the feature flag to be "off". | ||
| */ | ||
| public void turnFeatureOff(String key) { | ||
| writeFeatureRep(key, new Variation.Builder<>(false, 100).build()); | ||
| } | ||
|
|
||
| private void writeFeatureRep(final String key, final Variation<Boolean> variation) { | ||
| FeatureRep<Boolean> newFeature = new FeatureRep.Builder<Boolean>(String.format("test-%s", key), key) | ||
| .variation(variation).version(version.incrementAndGet()).build(); | ||
| upsert(key, newFeature); | ||
| } | ||
| } | ||
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
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 is kind of hardcoded to Boolean based flags only at the moment...