Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 63d0d97

Browse files
authored
Merge pull request #88 from yuv422/master
Issue #87 Added the ability to check if a given flag is known to the client.
2 parents e4a8ca8 + 7e34acc commit 63d0d97

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/main/java/com/launchdarkly/client/LDClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,24 @@ public JsonElement jsonVariation(String featureKey, LDUser user, JsonElement def
308308
return value;
309309
}
310310

311+
@Override
312+
public boolean isFlagKnown(String featureKey) {
313+
if (!initialized()) {
314+
logger.warn("Evaluation called before Client has been initialized for feature flag " + featureKey + "; returning unknown");
315+
return false;
316+
}
317+
318+
try {
319+
if (config.featureStore.get(featureKey) != null) {
320+
return true;
321+
}
322+
} catch (Exception e) {
323+
logger.error("Encountered exception in LaunchDarkly client", e);
324+
}
325+
326+
return false;
327+
}
328+
311329
private JsonElement evaluate(String featureKey, LDUser user, JsonElement defaultValue, VariationType expectedType) {
312330
if (user == null || user.getKey() == null) {
313331
logger.warn("Null user or null user key when evaluating flag: " + featureKey + "; returning default value");

src/main/java/com/launchdarkly/client/LDClientInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public interface LDClientInterface extends Closeable {
3030

3131
JsonElement jsonVariation(String featureKey, LDUser user, JsonElement defaultValue);
3232

33+
boolean isFlagKnown(String featureKey);
34+
3335
@Override
3436
void close() throws IOException;
3537

src/test/java/com/launchdarkly/client/LDClientTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,49 @@ public void testTestFeatureStoreJsonVariationArray() throws Exception {
272272
verifyAll();
273273
}
274274

275+
@Test
276+
public void testIsFlagKnown() throws Exception {
277+
TestFeatureStore testFeatureStore = new TestFeatureStore();
278+
LDConfig config = new LDConfig.Builder()
279+
.startWaitMillis(10L)
280+
.stream(false)
281+
.featureStore(testFeatureStore)
282+
.build();
283+
284+
expect(initFuture.get(10L, TimeUnit.MILLISECONDS)).andReturn(new Object());
285+
expect(pollingProcessor.start()).andReturn(initFuture);
286+
expect(pollingProcessor.initialized()).andReturn(true).times(2);
287+
replayAll();
288+
289+
client = createMockClient(config);
290+
291+
testFeatureStore.setIntegerValue("key", 1);
292+
assertTrue("Flag is known", client.isFlagKnown("key"));
293+
assertFalse("Flag is unknown", client.isFlagKnown("unKnownKey"));
294+
verifyAll();
295+
}
296+
297+
@Test
298+
public void testIsFlagKnownCallBeforeInitialization() throws Exception {
299+
TestFeatureStore testFeatureStore = new TestFeatureStore();
300+
LDConfig config = new LDConfig.Builder()
301+
.startWaitMillis(10L)
302+
.stream(false)
303+
.featureStore(testFeatureStore)
304+
.build();
305+
306+
expect(initFuture.get(10L, TimeUnit.MILLISECONDS)).andReturn(new Object());
307+
expect(pollingProcessor.start()).andReturn(initFuture);
308+
expect(pollingProcessor.initialized()).andReturn(false).times(1);
309+
replayAll();
310+
311+
client = createMockClient(config);
312+
313+
testFeatureStore.setIntegerValue("key", 1);
314+
assertFalse("Flag is marked as unknown", client.isFlagKnown("key"));
315+
verifyAll();
316+
}
317+
275318
@Test
276319
public void testUseLdd() throws IOException {
277320
LDConfig config = new LDConfig.Builder()

0 commit comments

Comments
 (0)