@@ -629,6 +629,81 @@ public void view_stopStartedAndNext() {
629629 ModuleViewsTests .validateView ("next" , 0.0 , 2 , 3 , false , true , null , TestUtils .keysValues [1 ], TestUtils .keysValues [0 ]);
630630 }
631631
632+ /**
633+ * Validates that when session calls are made, if any user properties are set,
634+ * they are sent before sending that session call
635+ * Validated with all session calls: begin, update, end
636+ *
637+ * @throws InterruptedException if thread is interrupted
638+ */
639+ @ Test
640+ public void userPropsOnSessions () throws InterruptedException {
641+ Countly .instance ().init (TestUtils .getConfigSessions (Config .Feature .UserProfiles ));
642+ Countly .instance ().userProfile ().setProperty ("name" , "John Doe" );
643+ Countly .instance ().userProfile ().setProperty ("custom_key" , "custom_value" );
644+
645+ Countly .session ().begin ();
646+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
647+ UserEditorTests .validateUserDetailsRequestInRQ (TestUtils .map ("user_details" , TestUtils .json ("name" , "John Doe" , "custom" , TestUtils .map ("custom_key" , "custom_value" ))), 0 , 2 );
648+ Assert .assertEquals ("1" , RQ [1 ].get ("begin_session" ));
649+
650+ Thread .sleep (2000 ); // wait for session to update
651+ Countly .instance ().userProfile ().save ();
652+ RQ = TestUtils .getCurrentRQ ();
653+ Assert .assertEquals (2 , RQ .length ); // Validate that user properties are flushed
654+
655+ Countly .instance ().userProfile ().setProperty ("email" , "john@doe.com" );
656+ Countly .session ().update ();
657+
658+ RQ = TestUtils .getCurrentRQ ();
659+ Assert .assertEquals (TestUtils .json ("email" , "john@doe.com" ), RQ [2 ].get ("user_details" ));
660+ Assert .assertEquals ("2" , RQ [3 ].get ("session_duration" ));
661+
662+ Thread .sleep (2000 ); // wait for session to update
663+ Countly .instance ().userProfile ().save ();
664+ RQ = TestUtils .getCurrentRQ ();
665+ Assert .assertEquals (4 , RQ .length ); // Validate that user properties are flushed with update call
666+
667+ Countly .instance ().userProfile ().setProperty ("done" , "yes" );
668+ Countly .session ().end ();
669+
670+ RQ = TestUtils .getCurrentRQ ();
671+ Assert .assertEquals (TestUtils .json ("custom" , TestUtils .map ("done" , "yes" )), RQ [4 ].get ("user_details" ));
672+ Assert .assertEquals ("1" , RQ [5 ].get ("end_session" ));
673+ }
674+
675+ /**
676+ * Validates that when session calls are made, if any user properties are set,
677+ * they are not packed because auto-send is disabled
678+ *
679+ * @throws InterruptedException if thread is interrupted
680+ */
681+ @ Test
682+ public void userPropsOnSessions_reversed () throws InterruptedException {
683+ Countly .instance ().init (TestUtils .getConfigSessions (Config .Feature .UserProfiles ).disableAutoSendUserPropertiesOnSessions ());
684+ Countly .instance ().userProfile ().setProperty ("name" , "John Doe" );
685+ Countly .instance ().userProfile ().setProperty ("custom_key" , "custom_value" );
686+
687+ Countly .session ().begin ();
688+ Map <String , String >[] RQ = TestUtils .getCurrentRQ ();
689+ Assert .assertEquals (1 , RQ .length );
690+ Assert .assertEquals ("1" , RQ [0 ].get ("begin_session" ));
691+
692+ Thread .sleep (2000 ); // wait for session to update
693+ Countly .session ().update ();
694+ RQ = TestUtils .getCurrentRQ ();
695+
696+ Assert .assertEquals (2 , RQ .length );
697+ Assert .assertEquals ("2" , RQ [1 ].get ("session_duration" ));
698+
699+ Thread .sleep (2000 );
700+ Countly .session ().end ();
701+ RQ = TestUtils .getCurrentRQ ();
702+
703+ Assert .assertEquals (3 , RQ .length );
704+ Assert .assertEquals ("1" , RQ [2 ].get ("end_session" ));
705+ }
706+
632707 private void validateNotEquals (int idOffset , BiFunction <SessionImpl , SessionImpl , Consumer <Long >> setter ) {
633708 Countly .instance ().init (TestUtils .getConfigSessions ());
634709 long ts = TimeUtils .timestampMs ();
0 commit comments