@@ -442,6 +442,137 @@ 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 will not work because reversed
474+ *
475+ * @throws InterruptedException when sleep is interrupted
476+ */
477+ @ Test
478+ public void eventsUserProps_reversed () throws InterruptedException {
479+ init (TestUtils .getConfigEvents (4 ).setUpdateSessionTimerDelay (2 ).disableAutoSendUserProperties ());
480+
481+ Countly .instance ().userProfile ().setProperty ("before_event" , "value1" );
482+ Countly .instance ().events ().recordEvent (eKeys [0 ]);
483+
484+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
485+ Assert .assertEquals (0 , RQ .length );
486+ TestUtils .validateEventInEQ (eKeys [0 ], null , 1 , null , null , 0 , 1 , "_CLY_" , null , "" , null );
487+
488+ Countly .instance ().userProfile ().setProperty ("after_event" , "value2" );
489+ Thread .sleep (2500 ); // wait for the tick
490+ RQ = TestUtils .getCurrentRQ ();
491+
492+ Assert .assertEquals (1 , RQ .length );
493+ Assert .assertTrue (RQ [0 ].containsKey ("events" ));
494+ }
495+
496+ /**
497+ * Recording events with user properties and with flushing events
498+ * Validating that if a user property save called, it flushes EQ before saving user properties
499+ */
500+ @ Test
501+ public void eventsUserProps_propsSave () {
502+ init (TestUtils .getConfigEvents (4 ));
503+
504+ Countly .instance ().events ().recordEvent (eKeys [0 ]);
505+
506+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
507+ Assert .assertEquals (0 , RQ .length );
508+ TestUtils .validateEventInEQ (eKeys [0 ], null , 1 , null , null , 0 , 1 , "_CLY_" , null , "" , null );
509+
510+ Countly .instance ().userProfile ().setProperty ("after_event" , "value2" );
511+ Countly .instance ().userProfile ().save ();
512+
513+ RQ = TestUtils .getCurrentRQ ();
514+ Assert .assertEquals (2 , RQ .length );
515+ Assert .assertTrue (RQ [0 ].containsKey ("events" ));
516+ Assert .assertEquals (TestUtils .json ("custom" , TestUtils .map ("after_event" , "value2" )), RQ [1 ].get ("user_details" ));
517+ }
518+
519+ /**
520+ * Recording events with user properties and with flushing events
521+ * Validating that if a user property save called, it does not flush EQ before saving user properties
522+ */
523+ @ Test
524+ public void eventsUserProps_propsSave_reversed () {
525+ init (TestUtils .getConfigEvents (4 ).disableAutoSendUserProperties ());
526+
527+ Countly .instance ().events ().recordEvent (eKeys [0 ]);
528+
529+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
530+ Assert .assertEquals (0 , RQ .length );
531+ TestUtils .validateEventInEQ (eKeys [0 ], null , 1 , null , null , 0 , 1 , "_CLY_" , null , "" , null );
532+
533+ Countly .instance ().userProfile ().setProperty ("after_event" , "value2" );
534+ Countly .instance ().userProfile ().save ();
535+
536+ RQ = TestUtils .getCurrentRQ ();
537+ Assert .assertEquals (1 , RQ .length );
538+ Assert .assertEquals (TestUtils .json ("custom" , TestUtils .map ("after_event" , "value2" )), RQ [0 ].get ("user_details" ));
539+ }
540+
541+ /**
542+ * Validate that user properties are sent with timer tick if no events are recorded
543+ */
544+ @ Test
545+ public void eventsUserProps_timer () throws InterruptedException {
546+ init (TestUtils .getConfigEvents (4 ).setUpdateSessionTimerDelay (2 ));
547+
548+ Countly .instance ().userProfile ().setProperty ("before_timer" , "value1" );
549+
550+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
551+ Assert .assertEquals (0 , RQ .length );
552+
553+ Thread .sleep (2500 ); // wait for the tick
554+ RQ = TestUtils .getCurrentRQ ();
555+ Assert .assertEquals (1 , RQ .length );
556+ Assert .assertEquals (TestUtils .json ("custom" , TestUtils .map ("before_timer" , "value1" )), RQ [0 ].get ("user_details" ));
557+ }
558+
559+ /**
560+ * Validate that user properties does not send with timer tick if no events are recorded
561+ */
562+ @ Test
563+ public void eventsUserProps_timer_reversed () throws InterruptedException {
564+ init (TestUtils .getConfigEvents (4 ).setUpdateSessionTimerDelay (2 ).disableAutoSendUserProperties ());
565+
566+ Countly .instance ().userProfile ().setProperty ("before_timer" , "value1" );
567+
568+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
569+ Assert .assertEquals (0 , RQ .length );
570+
571+ Thread .sleep (2500 ); // wait for the tick
572+ RQ = TestUtils .getCurrentRQ ();
573+ Assert .assertEquals (0 , RQ .length );
574+ }
575+
445576 private void validateTimedEventSize (int expectedQueueSize , int expectedTimedEventSize ) {
446577 TestUtils .validateEQSize (expectedQueueSize , TestUtils .getCurrentEQ (), moduleEvents .eventQueue );
447578 Assert .assertEquals (expectedTimedEventSize , moduleEvents .timedEvents .size ());
0 commit comments