Skip to content

Commit acdc4a7

Browse files
feat: tests for the user props auto save
1 parent 97e6094 commit acdc4a7

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleEventsTests.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,74 @@ public void timedEventFlow() throws InterruptedException {
442442
TestUtils.validateEventInEQ(eKeys[0], null, 2, 4.0, 2.0, 1, 2, TestUtils.keysValues[1], null, "", TestUtils.keysValues[0]);
443443
}
444444

445+
/**
446+
* Recording events with user properties and with flushing events
447+
* Validating that if a user property set before a recordEvent call it is sent before adding the event to EQ
448+
* And also user properties packed after flushing events.
449+
*
450+
* @throws InterruptedException when sleep is interrupted
451+
*/
452+
@Test
453+
public void eventsUserProps() throws InterruptedException {
454+
init(TestUtils.getConfigEvents(4).setUpdateSessionTimerDelay(2));
455+
456+
Countly.instance().userProfile().setProperty("before_event", "value1");
457+
Countly.instance().events().recordEvent(eKeys[0]);
458+
459+
Map<String, String>[] RQ = TestUtils.getCurrentRQ();
460+
Assert.assertEquals(1, RQ.length);
461+
Assert.assertEquals(TestUtils.json("custom", TestUtils.map("before_event", "value1")), RQ[0].get("user_details"));
462+
TestUtils.validateEventInEQ(eKeys[0], null, 1, null, null, 0, 1, "_CLY_", null, "", null);
463+
464+
Countly.instance().userProfile().setProperty("after_event", "value2");
465+
Thread.sleep(2500); // wait for the tick
466+
RQ = TestUtils.getCurrentRQ();
467+
Assert.assertEquals(3, RQ.length);
468+
Assert.assertTrue(RQ[1].containsKey("events"));
469+
Assert.assertEquals(TestUtils.json("custom", TestUtils.map("after_event", "value2")), RQ[2].get("user_details"));
470+
}
471+
472+
/**
473+
* Recording events with user properties and with flushing events
474+
* Validating that if a user property save called, it flushes EQ before saving user properties
475+
*/
476+
@Test
477+
public void eventsUserProps_propsSave() {
478+
init(TestUtils.getConfigEvents(4));
479+
480+
Countly.instance().events().recordEvent(eKeys[0]);
481+
482+
Map<String, String>[] RQ = TestUtils.getCurrentRQ();
483+
Assert.assertEquals(0, RQ.length);
484+
TestUtils.validateEventInEQ(eKeys[0], null, 1, null, null, 0, 1, "_CLY_", null, "", null);
485+
486+
Countly.instance().userProfile().setProperty("after_event", "value2");
487+
Countly.instance().userProfile().save();
488+
489+
RQ = TestUtils.getCurrentRQ();
490+
Assert.assertEquals(2, RQ.length);
491+
Assert.assertTrue(RQ[0].containsKey("events"));
492+
Assert.assertEquals(TestUtils.json("custom", TestUtils.map("after_event", "value2")), RQ[1].get("user_details"));
493+
}
494+
495+
/**
496+
* Validate that user properties are sent with timer tick if no events are recorded
497+
*/
498+
@Test
499+
public void eventsUserProps_timer() throws InterruptedException {
500+
init(TestUtils.getConfigEvents(4).setUpdateSessionTimerDelay(2));
501+
502+
Countly.instance().userProfile().setProperty("before_timer", "value1");
503+
504+
Map<String, String>[] RQ = TestUtils.getCurrentRQ();
505+
Assert.assertEquals(0, RQ.length);
506+
507+
Thread.sleep(2500); // wait for the tick
508+
RQ = TestUtils.getCurrentRQ();
509+
Assert.assertEquals(1, RQ.length);
510+
Assert.assertEquals(TestUtils.json("custom", TestUtils.map("before_timer", "value1")), RQ[0].get("user_details"));
511+
}
512+
445513
private void validateTimedEventSize(int expectedQueueSize, int expectedTimedEventSize) {
446514
TestUtils.validateEQSize(expectedQueueSize, TestUtils.getCurrentEQ(), moduleEvents.eventQueue);
447515
Assert.assertEquals(expectedTimedEventSize, moduleEvents.timedEvents.size());

0 commit comments

Comments
 (0)