Skip to content

Commit dc29af3

Browse files
Clean up tests
1 parent 7591217 commit dc29af3

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

src/main/java/com/octopus/openfeature/provider/OctopusContextProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class OctopusContextProvider {
1515
this.config = config;
1616
this.client = client;
1717
}
18-
18+
1919
OctopusContext getOctopusContext() { return currentContext; }
2020

2121
void initialize() {
@@ -34,7 +34,7 @@ void initialize() {
3434
// run the refresh loop in the background
3535
refreshThread = new Thread(this::refresh);
3636
refreshThread.start();
37-
37+
3838
initialized = true;
3939
}
4040

src/test/java/com/octopus/openfeature/provider/OctopusContextProviderTests.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
package com.octopus.openfeature.provider;
2-
3-
import org.junit.jupiter.api.Test;
4-
52
import java.time.Duration;
63
import java.util.ArrayList;
74
import java.util.Collections;
@@ -11,13 +8,15 @@
118
import java.util.logging.Logger;
129

1310
import static org.assertj.core.api.Assertions.assertThat;
11+
import org.junit.jupiter.api.Test;
1412

1513
class OctopusContextProviderTests {
1614

17-
static class FakeClient extends OctopusClient {
15+
static class MockOctopusFeatureClient extends OctopusClient {
16+
1817
private volatile FeatureToggles toggles;
1918

20-
FakeClient(FeatureToggles toggles) {
19+
MockOctopusFeatureClient(FeatureToggles toggles) {
2120
super(null);
2221
this.toggles = toggles;
2322
}
@@ -37,46 +36,57 @@ FeatureToggles getFeatureToggleEvaluationManifest() {
3736
}
3837
}
3938

40-
private OctopusConfiguration fastConfig() {
39+
private final OctopusConfiguration configuration = configure();
40+
41+
private static OctopusConfiguration configure() {
4142
var config = new OctopusConfiguration("token");
4243
config.setCacheDuration(Duration.ofMillis(100));
4344
return config;
4445
}
4546

4647
@Test
4748
void whenInitialized_RefreshesCacheAfterCacheDurationExpires() throws InterruptedException {
49+
4850
byte[] initialHash = {0x01, 0x02, 0x03, 0x04};
4951
byte[] updatedHash = {0x01, 0x02, 0x03, 0x05};
5052

51-
var client = new FakeClient(new FeatureToggles(
53+
var client = new MockOctopusFeatureClient(new FeatureToggles(
5254
List.of(new FeatureToggleEvaluation("test-feature", true, "evaluation-key", Collections.emptyList(), 100)),
5355
initialHash
5456
));
55-
var provider = new OctopusContextProvider(fastConfig(), client);
57+
58+
var provider = new OctopusContextProvider(configuration, client);
5659
provider.initialize();
5760

5861
try {
62+
// Validate the initial state
5963
assertThat(provider.getOctopusContext().getContentHash()).isEqualTo(initialHash);
6064
assertThat(provider.getOctopusContext().evaluate("test-feature", false, null).getValue()).isTrue();
6165

66+
// Simulate a change in the available feature toggles
6267
client.changeToggles(new FeatureToggles(
6368
List.of(new FeatureToggleEvaluation("test-feature", false, "evaluation-key", Collections.emptyList(), 100)),
6469
updatedHash
6570
));
71+
72+
// Wait for the cache to expire
6673
Thread.sleep(500);
6774

75+
// Validate the updated toggles are available
6876
assertThat(provider.getOctopusContext().getContentHash()).isEqualTo(updatedHash);
6977
assertThat(provider.getOctopusContext().evaluate("test-feature", false, null).getValue()).isFalse();
78+
7079
} finally {
7180
provider.shutdown();
7281
}
7382
}
7483

7584
@Test
7685
void whenInitialized_AndRefreshFails_RetainsExistingContextAndLogsError() throws InterruptedException {
86+
7787
byte[] contentHash = {0x01, 0x02, 0x03, 0x04};
7888

79-
var client = new FakeClient(new FeatureToggles(
89+
var client = new MockOctopusFeatureClient(new FeatureToggles(
8090
List.of(new FeatureToggleEvaluation("test-feature", true, "evaluation-key", Collections.emptyList(), 100)),
8191
contentHash
8292
));
@@ -90,16 +100,22 @@ void whenInitialized_AndRefreshFails_RetainsExistingContextAndLogsError() throws
90100
};
91101
julLogger.addHandler(handler);
92102

93-
var provider = new OctopusContextProvider(fastConfig(), client);
103+
var provider = new OctopusContextProvider(configuration, client);
104+
94105
try {
95106
provider.initialize();
96107

108+
// Simulate a failed fetch
97109
client.changeToggles(null);
110+
111+
// Wait for the cache to expire
98112
Thread.sleep(500);
99113

114+
// Validate that the existing context is retained and an error was logged
100115
assertThat(provider.getOctopusContext().getContentHash()).isEqualTo(contentHash);
101116
assertThat(provider.getOctopusContext().evaluate("test-feature", false, null).getValue()).isTrue();
102117
assertThat(logMessages).anyMatch(m -> m.startsWith("Failed to retrieve updated feature manifest"));
118+
103119
} finally {
104120
julLogger.removeHandler(handler);
105121
provider.shutdown();

0 commit comments

Comments
 (0)