Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import androidx.test.core.app.ApplicationProvider;

import com.launchdarkly.sdk.EvaluationDetail;
import com.launchdarkly.sdk.EvaluationReason;
import com.launchdarkly.sdk.LDContext;
import com.launchdarkly.sdk.LDValue;
Expand Down Expand Up @@ -199,6 +200,49 @@ public void variationFlagTrackReasonGeneratesEventWithReason() throws IOExceptio
}
}

@Test
public void wrongTypeEvaluationEventHasDefaultValueAndNoVariation() throws IOException, InterruptedException {
try (MockWebServer mockEventsServer = new MockWebServer()) {
mockEventsServer.start();
// Enqueue a successful empty response
mockEventsServer.enqueue(new MockResponse());

// The flag value is a string, so a boolean evaluation has the wrong type.
Flag flag = new FlagBuilder("wrong-type-flag").version(10)
.variation(0).value(LDValue.of("a")).reason(EvaluationReason.fallthrough())
.trackEvents(true).build();
PersistentDataStore store = new InMemoryPersistentDataStore();
TestUtil.writeFlagUpdateToStore(store, mobileKey, ldContext, flag);

LDConfig ldConfig = baseConfigBuilder(mockEventsServer)
.persistentDataStore(store).build();

try (LDClient client = LDClient.init(application, ldConfig, ldContext, 0)) {
EvaluationDetail<Boolean> detail = client.boolVariationDetail("wrong-type-flag", true);
assertEquals(Boolean.TRUE, detail.getValue());
assertEquals(EvaluationReason.error(EvaluationReason.ErrorKind.WRONG_TYPE), detail.getReason());
client.blockingFlush();

LDValue[] events = getEventsFromLastRequest(mockEventsServer, 3);
LDValue featureEvent = events[1], summaryEvent = events[2];
assertFeatureEvent(featureEvent, ldContext);
assertEquals(LDValue.of("wrong-type-flag"), featureEvent.get("key"));
assertEquals(LDValue.of(true), featureEvent.get("value"));
assertEquals(LDValue.ofNull(), featureEvent.get("variation"));
assertEquals(LDValue.of(10), featureEvent.get("version"));
assertEquals(LDValue.parse(JsonSerialization.serialize(
EvaluationReason.error(EvaluationReason.ErrorKind.WRONG_TYPE))),
featureEvent.get("reason"));

assertSummaryEvent(summaryEvent);
LDValue counter = summaryEvent.get("features").get("wrong-type-flag").get("counters").get(0);
assertEquals(LDValue.of(true), counter.get("value"));
assertEquals(LDValue.ofNull(), counter.get("variation"));
assertEquals(LDValue.of(10), counter.get("version"));
}
}
}

@Test
public void flagEvaluationWithPrereqProducesPrereqEvents() throws IOException, InterruptedException {
try (MockWebServer mockEventsServer = new MockWebServer()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ private EvaluationDetail<LDValue> variationDetailInternal(@NonNull String key, @
context,
key,
flag.getVersionForEvents(),
flag.getVariation() == null ? -1 : flag.getVariation().intValue(),
// The event records the variation of the result. A wrong-type result has no variation.
result.getVariationIndex(),
value,
flag.isTrackReason() | needsReason ? result.getReason() : null,
defaultValue,
Expand Down
Loading