|
19 | 19 | import static com.launchdarkly.sdk.server.DataModel.FEATURES; |
20 | 20 | import static com.launchdarkly.sdk.server.EvaluatorTestUtil.BASE_USER; |
21 | 21 | import static com.launchdarkly.sdk.server.ModelBuilders.flagBuilder; |
| 22 | +import static com.launchdarkly.sdk.server.ModelBuilders.prerequisite; |
22 | 23 | import static com.launchdarkly.sdk.server.TestComponents.nullLogger; |
23 | 24 | import static org.junit.Assert.assertEquals; |
24 | 25 | import static org.junit.Assert.assertFalse; |
@@ -49,6 +50,59 @@ public boolean isInitialized() { |
49 | 50 | }; |
50 | 51 | } |
51 | 52 |
|
| 53 | + // A store that holds the given flags and fails when the given key is read, as a store does |
| 54 | + // when its database is unreachable. |
| 55 | + private static ReadOnlyStore storeFailingOn(String failingKey, FeatureFlag... flags) { |
| 56 | + ReadOnlyStore base = storeWith(flags); |
| 57 | + return new ReadOnlyStore() { |
| 58 | + @Override |
| 59 | + public ItemDescriptor get(DataKind kind, String key) { |
| 60 | + if (key.equals(failingKey)) { |
| 61 | + throw new RuntimeException("store failed"); |
| 62 | + } |
| 63 | + return base.get(kind, key); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public KeyedItems<ItemDescriptor> getAll(DataKind kind) { |
| 68 | + return base.getAll(kind); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean isInitialized() { |
| 73 | + return true; |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + // A flag whose evaluation reads the prerequisite "prereq". |
| 79 | + private static FeatureFlag flagWithPrerequisite(String key) { |
| 80 | + return flagBuilder(key).on(true).prerequisites(prerequisite("prereq", 0)).fallthroughVariation(0) |
| 81 | + .offVariation(0).variations(LDValue.of("value")).trackEvents(true).build(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void exceptionWhileEvaluatingOverrideFlagKeepsMarking() { |
| 86 | + FeatureFlag flag = flagWithPrerequisite("flag").markedAsOverride(); |
| 87 | + EvalResultAndFlag result = evaluatorOver(storeFailingOn("prereq", flag)).evaluate("flag", BASE_USER, |
| 88 | + LDValue.of("default"), null, InputValidatingEvaluator.NO_OP_EVALUATION_EVENT_RECORDER); |
| 89 | + |
| 90 | + assertEquals(LDValue.of("default"), result.getResult().getValue()); |
| 91 | + assertEquals(EvaluationReason.ErrorKind.EXCEPTION, result.getResult().getReason().getErrorKind()); |
| 92 | + assertTrue(result.getResult().getReason().isOverrideAffected()); |
| 93 | + assertTrue(result.getResult().isOverrideAffected()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void exceptionWhileEvaluatingPlainFlagIsNotMarked() { |
| 98 | + FeatureFlag flag = flagWithPrerequisite("flag"); |
| 99 | + EvalResultAndFlag result = evaluatorOver(storeFailingOn("prereq", flag)).evaluate("flag", BASE_USER, |
| 100 | + LDValue.of("default"), null, InputValidatingEvaluator.NO_OP_EVALUATION_EVENT_RECORDER); |
| 101 | + |
| 102 | + assertEquals(EvaluationReason.ErrorKind.EXCEPTION, result.getResult().getReason().getErrorKind()); |
| 103 | + assertFalse(result.getResult().isOverrideAffected()); |
| 104 | + } |
| 105 | + |
52 | 106 | private static InputValidatingEvaluator evaluatorOver(ReadOnlyStore store) { |
53 | 107 | return new InputValidatingEvaluator(store, null, new NoOpEventProcessor(), nullLogger); |
54 | 108 | } |
|
0 commit comments