Skip to content

Commit b16151b

Browse files
feat: add test case for it
1 parent 4e637d5 commit b16151b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

tests/event_queue.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,93 @@ TEST_CASE("Tests that sets 'setEventsToRQThreshold' before and after SDK starts"
234234
nlohmann::json events = nlohmann::json::parse(oldest_call.data["events"]);
235235
CHECK(events.size() == 3);
236236
}
237+
}
238+
239+
TEST_CASE("Tests that saving user details trigger flushing EQ"){
240+
clearSDK();
241+
Countly &countly = Countly::getInstance();
242+
243+
// Automatic saving of events before user props calls
244+
SUBCASE("Saving user properties should flush EQ") {
245+
countly.enableManualSessionControl();
246+
test_utils::initCountlyWithFakeNetworking(true, countly);
247+
248+
test_utils::generateEvents(4, countly);
249+
CHECK(countly.checkEQSize() == 4);
250+
251+
// set user properties, this should flush the EQ
252+
countly.setUserDetails({{"name", "Full name"}});
253+
CHECK(countly.checkEQSize() == 0);
254+
255+
test_utils::generateEvents(4, countly);
256+
CHECK(countly.checkEQSize() == 4);
257+
258+
// set custom user properties, this should flush the EQ
259+
countly.setCustomUserDetails({{"custom_key", "custom_value"}});
260+
CHECK(countly.checkEQSize() == 0);
261+
262+
// RQ should have 4 events and user details
263+
// trigger RQ to send requests to http_call_queue
264+
countly.processRQDebug();
265+
// queue should have 4 requests
266+
CHECK(!http_call_queue.empty());
267+
CHECK(http_call_queue.size() == 4);
268+
HTTPCall eventsReq1 = http_call_queue.front();
269+
http_call_queue.pop_front();
270+
HTTPCall userDetails = http_call_queue.front();
271+
http_call_queue.pop_front();
272+
HTTPCall eventsReq2 = http_call_queue.front();
273+
http_call_queue.pop_front();
274+
HTTPCall customUserDetails = http_call_queue.front();
275+
http_call_queue.pop_front();
276+
CHECK(http_call_queue.size() == 0);
277+
278+
// last call should have 4 events
279+
nlohmann::json events1 = nlohmann::json::parse(eventsReq1.data["events"]);
280+
CHECK(events1.size() == 4);
281+
nlohmann::json userDetailsJson = nlohmann::json::parse(userDetails.data["user_details"]);
282+
CHECK(userDetailsJson["name"] == "Full name");
283+
284+
nlohmann::json events2 = nlohmann::json::parse(eventsReq2.data["events"]);
285+
CHECK(events2.size() == 4);
286+
nlohmann::json customUserDetailsJson = nlohmann::json::parse(customUserDetails.data["user_details"]);
287+
CHECK(customUserDetailsJson["custom"]["custom_key"] == "custom_value");
288+
}
289+
290+
// Automatic saving of events before user props calls
291+
SUBCASE("Saving user properties should not flush EQ when behavior is disabled") {
292+
countly.enableManualSessionControl();
293+
countly.disableAutoEventsOnUserProperties();
294+
test_utils::initCountlyWithFakeNetworking(true, countly);
295+
296+
test_utils::generateEvents(4, countly);
297+
CHECK(countly.checkEQSize() == 4);
298+
299+
// set user properties, this should flush the EQ
300+
countly.setUserDetails({{"name", "Full name"}});
301+
CHECK(countly.checkEQSize() == 4);
302+
303+
test_utils::generateEvents(4, countly);
304+
CHECK(countly.checkEQSize() == 8);
305+
306+
// set custom user properties, this should flush the EQ
307+
countly.setCustomUserDetails({{"custom_key", "custom_value"}});
308+
CHECK(countly.checkEQSize() == 8);
309+
// RQ should have 4 events and user details
310+
// trigger RQ to send requests to http_call_queue
311+
countly.processRQDebug();
312+
// queue should have 2 requests
313+
CHECK(!http_call_queue.empty());
314+
CHECK(http_call_queue.size() == 2);
315+
HTTPCall userDetails = http_call_queue.front();
316+
http_call_queue.pop_front();
317+
HTTPCall customUserDetails = http_call_queue.front();
318+
http_call_queue.pop_front();
319+
CHECK(http_call_queue.size() == 0);
320+
321+
nlohmann::json userDetailsJson = nlohmann::json::parse(userDetails.data["user_details"]);
322+
CHECK(userDetailsJson["name"] == "Full name");
323+
nlohmann::json customUserDetailsJson = nlohmann::json::parse(customUserDetails.data["user_details"]);
324+
CHECK(customUserDetailsJson["custom"]["custom_key"] == "custom_value");
325+
}
237326
}

0 commit comments

Comments
 (0)