-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Fallback to existing evaluation context on failed refresh #14
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
Merged
caitlynstocker
merged 3 commits into
main
from
cat/devex-41/default-to-old-state-on-failed-request
May 26, 2026
+129
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
124 changes: 124 additions & 0 deletions
124
src/test/java/com/octopus/openfeature/provider/OctopusContextProviderTests.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,124 @@ | ||
| package com.octopus.openfeature.provider; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.logging.Handler; | ||
| import java.util.logging.LogRecord; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class OctopusContextProviderTests { | ||
|
|
||
| static class MockOctopusFeatureClient extends OctopusClient { | ||
|
|
||
| private volatile FeatureToggles toggles; | ||
|
Contributor
Author
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.
Apparently this just guarantees that a write to |
||
|
|
||
| MockOctopusFeatureClient(FeatureToggles toggles) { | ||
| super(null); | ||
| this.toggles = toggles; | ||
| } | ||
|
|
||
| void changeToggles(FeatureToggles toggles) { | ||
| this.toggles = toggles; | ||
| } | ||
|
|
||
| @Override | ||
| Boolean haveFeatureTogglesChanged(byte[] contentHash) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| FeatureToggles getFeatureToggleEvaluationManifest() { | ||
| return toggles; | ||
| } | ||
| } | ||
|
|
||
| private final OctopusConfiguration configuration = configure(); | ||
|
|
||
| private static OctopusConfiguration configure() { | ||
| var config = new OctopusConfiguration("token"); | ||
| config.setCacheDuration(Duration.ofMillis(100)); | ||
| return config; | ||
| } | ||
|
|
||
| @Test | ||
| void whenInitialized_RefreshesCacheAfterCacheDurationExpires() throws InterruptedException { | ||
|
|
||
| byte[] initialHash = {0x01, 0x02, 0x03, 0x04}; | ||
| byte[] updatedHash = {0x01, 0x02, 0x03, 0x05}; | ||
|
|
||
| var client = new MockOctopusFeatureClient(new FeatureToggles( | ||
| List.of(new FeatureToggleEvaluation("test-feature", true, "evaluation-key", Collections.emptyList(), 100)), | ||
| initialHash | ||
| )); | ||
|
|
||
| var provider = new OctopusContextProvider(configuration, client); | ||
| provider.initialize(); | ||
|
|
||
| try { | ||
| // Validate the initial state | ||
| assertThat(provider.getOctopusContext().getContentHash()).isEqualTo(initialHash); | ||
| assertThat(provider.getOctopusContext().evaluate("test-feature", false, null).getValue()).isTrue(); | ||
|
|
||
| // Simulate a change in the available feature toggles | ||
| client.changeToggles(new FeatureToggles( | ||
| List.of(new FeatureToggleEvaluation("test-feature", false, "evaluation-key", Collections.emptyList(), 100)), | ||
| updatedHash | ||
| )); | ||
|
|
||
| // Wait for the cache to expire | ||
| Thread.sleep(500); | ||
|
|
||
| // Validate the updated toggles are available | ||
| assertThat(provider.getOctopusContext().getContentHash()).isEqualTo(updatedHash); | ||
| assertThat(provider.getOctopusContext().evaluate("test-feature", true, null).getValue()).isFalse(); | ||
|
|
||
| } finally { | ||
| provider.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void whenInitialized_AndRefreshFails_RetainsExistingContextAndLogsError() throws InterruptedException { | ||
|
|
||
| byte[] contentHash = {0x01, 0x02, 0x03, 0x04}; | ||
|
|
||
| var client = new MockOctopusFeatureClient(new FeatureToggles( | ||
| List.of(new FeatureToggleEvaluation("test-feature", true, "evaluation-key", Collections.emptyList(), 100)), | ||
| contentHash | ||
| )); | ||
|
|
||
| var logMessages = new ArrayList<String>(); | ||
| var julLogger = Logger.getLogger(OctopusClient.class.getName()); | ||
| var handler = new Handler() { | ||
| @Override public void publish(LogRecord record) { logMessages.add(record.getMessage()); } | ||
| @Override public void flush() {} | ||
| @Override public void close() {} | ||
| }; | ||
| julLogger.addHandler(handler); | ||
|
|
||
| var provider = new OctopusContextProvider(configuration, client); | ||
|
|
||
| try { | ||
| provider.initialize(); | ||
|
|
||
| // Simulate a failed fetch | ||
| client.changeToggles(null); | ||
|
|
||
| // Wait for the cache to expire | ||
| Thread.sleep(500); | ||
|
|
||
| // Validate that the existing context is retained and an error was logged | ||
| assertThat(provider.getOctopusContext().getContentHash()).isEqualTo(contentHash); | ||
| assertThat(provider.getOctopusContext().evaluate("test-feature", false, null).getValue()).isTrue(); | ||
| assertThat(logMessages).anyMatch(m -> m.startsWith("Failed to retrieve updated feature manifest")); | ||
|
|
||
| } finally { | ||
| julLogger.removeHandler(handler); | ||
| provider.shutdown(); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
These tests are copied straight over from the dotnet library / my dotnet PR for this same ticket.