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

Commit c6cee01

Browse files
authored
Merge pull request #119 from launchdarkly/eb/ch32176/no-user-no-event
track or identify without a valid user shouldn't send an event
2 parents 9f3eac2 + 5d208ae commit c6cee01

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public void track(String eventName, LDUser user, JsonElement data) {
116116
}
117117
if (user == null || user.getKey() == null) {
118118
logger.warn("Track called with null user or null user key!");
119+
} else {
120+
eventProcessor.sendEvent(EventFactory.DEFAULT.newCustomEvent(eventName, user, data));
119121
}
120-
eventProcessor.sendEvent(EventFactory.DEFAULT.newCustomEvent(eventName, user, data));
121122
}
122123

123124
@Override
@@ -132,8 +133,9 @@ public void track(String eventName, LDUser user) {
132133
public void identify(LDUser user) {
133134
if (user == null || user.getKey() == null) {
134135
logger.warn("Identify called with null user or null user key!");
136+
} else {
137+
eventProcessor.sendEvent(EventFactory.DEFAULT.newIdentifyEvent(user));
135138
}
136-
eventProcessor.sendEvent(EventFactory.DEFAULT.newIdentifyEvent(user));
137139
}
138140

139141
private void sendFlagRequestEvent(Event.FeatureRequest event) {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
public class LDClientEventTest {
2525
private static final LDUser user = new LDUser("userkey");
26+
private static final LDUser userWithNullKey = new LDUser.Builder((String)null).build();
2627

2728
private FeatureStore featureStore = TestUtil.initedFeatureStore();
2829
private TestUtil.TestEventProcessor eventSink = new TestUtil.TestEventProcessor();
@@ -43,6 +44,18 @@ public void identifySendsEvent() throws Exception {
4344
Event.Identify ie = (Event.Identify)e;
4445
assertEquals(user.getKey(), ie.user.getKey());
4546
}
47+
48+
@Test
49+
public void identifyWithNullUserDoesNotSendEvent() {
50+
client.identify(null);
51+
assertEquals(0, eventSink.events.size());
52+
}
53+
54+
@Test
55+
public void identifyWithUserWithNoKeyDoesNotSendEvent() {
56+
client.identify(userWithNullKey);
57+
assertEquals(0, eventSink.events.size());
58+
}
4659

4760
@Test
4861
public void trackSendsEventWithoutData() throws Exception {
@@ -72,6 +85,18 @@ public void trackSendsEventWithData() throws Exception {
7285
assertEquals(data, ce.data);
7386
}
7487

88+
@Test
89+
public void trackWithNullUserDoesNotSendEvent() {
90+
client.track("eventkey", null);
91+
assertEquals(0, eventSink.events.size());
92+
}
93+
94+
@Test
95+
public void trackWithUserWithNoKeyDoesNotSendEvent() {
96+
client.track("eventkey", userWithNullKey);
97+
assertEquals(0, eventSink.events.size());
98+
}
99+
75100
@Test
76101
public void boolVariationSendsEvent() throws Exception {
77102
FeatureFlag flag = flagWithValue("key", jbool(true));

0 commit comments

Comments
 (0)