Skip to content

Commit

Permalink
split functions
Browse files Browse the repository at this point in the history
  • Loading branch information
turtledreams committed Jan 29, 2024
1 parent 32c7536 commit ef8c455
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 59 deletions.
64 changes: 8 additions & 56 deletions cypress/integration/async_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,55 +88,7 @@ describe("Test Countly.q related methods and processes", () => {
});
});
});
// This test is same with the one above but this time we only use sendEventsForced which alredy includes processAsyncQueue inside
it("Check sendEventsForced works as expected", () => {
hp.haltAndClearStorage(() => {
// Disable heartbeat and init the SDK
Countly.noHeartBeat = true;
Countly.q = [];
initMain();
cy.wait(1000);

// Check that the .q is empty
expect(Countly.q.length).to.equal(0);

// Add 4 events to the .q
Countly.q.push(["add_event", event(1)]);
Countly.q.push(["add_event", event(2)]);
Countly.q.push(["add_event", event(3)]);
Countly.q.push(["add_event", event(4)]);
// Check that the .q has 4 events
expect(Countly.q.length).to.equal(4);

cy.fetch_local_event_queue().then((rq) => {
// Check that the event queue is empty
expect(rq.length).to.equal(0);

// Check that events are still in .q
expect(Countly.q.length).to.equal(4);

// Send events from .q to event queue and then to request queue
Countly._internals.sendEventsForced();

// Check that the .q is empty
expect(Countly.q.length).to.equal(0);
cy.fetch_local_event_queue().then((eq) => {
// Check that event queue is empty
expect(eq.length).to.equal(0);
cy.fetch_local_request_queue().then((rq_2) => {
// Check that events are now in request queue
expect(rq_2.length).to.equal(1);
const eventsArray = JSON.parse(rq_2[0].events);
expect(eventsArray[0].key).to.equal("event_1");
expect(eventsArray[1].key).to.equal("event_2");
expect(eventsArray[2].key).to.equal("event_3");
expect(eventsArray[3].key).to.equal("event_4");
});
});
});
});
});
// This test is same with the ones above but this time we use change_id to trigger sendEventsForced
// This test is same with the ones above but this time we use change_id to trigger processAsyncQueue
it("Check changing device ID without merge empties the .q", () => {
hp.haltAndClearStorage(() => {
// Disable heartbeat and init the SDK
Expand All @@ -163,7 +115,7 @@ describe("Test Countly.q related methods and processes", () => {
// Check that events are still in .q
expect(Countly.q.length).to.equal(4);

// Trigger sendEventsForced by changing device ID without merge
// Trigger processAsyncQueue by changing device ID without merge
Countly.change_id("new_user_id", false);

// Check that the .q is empty
Expand All @@ -187,7 +139,7 @@ describe("Test Countly.q related methods and processes", () => {
});
});
});
// This test checks if clear_stored_id set to true during init we call sendEventsForced (it sends events from .q to event queue and then to request queue)
// This test checks if clear_stored_id set to true during init we call processAsyncQueue (it sends events from .q to event queue and then to request queue)
it("Check clear_stored_id set to true empties the .q", () => {
hp.haltAndClearStorage(() => {
// Disable heartbeat
Expand All @@ -212,7 +164,7 @@ describe("Test Countly.q related methods and processes", () => {
expect(Countly.q.length).to.equal(0);

cy.fetch_local_event_queue().then((rq) => {
// Check that the event queue is empty because sendEventsForced sends events from .q to event queue and then to request queue
// Check that the event queue is empty because processAsyncQueue sends events from .q to event queue and then to request queue
expect(rq.length).to.equal(0);

cy.fetch_local_request_queue().then((rq_2) => {
Expand All @@ -227,7 +179,7 @@ describe("Test Countly.q related methods and processes", () => {
});
});
});
// This test checks if calling user_details triggers sendEventsForced (it sends events from .q to event queue and then to request queue)
// This test checks if calling user_details triggers processAsyncQueue (it sends events from .q to event queue and then to request queue)
it("Check sending user details empties .q", () => {
hp.haltAndClearStorage(() => {
// Disable heartbeat and init the SDK
Expand All @@ -254,7 +206,7 @@ describe("Test Countly.q related methods and processes", () => {
// Check that events are still in .q
expect(Countly.q.length).to.equal(4);

// Trigger sendEventsForced by adding user details
// Trigger processAsyncQueue by adding user details
Countly.user_details({ name: "test_user" });

// Check that the .q is empty
Expand All @@ -278,7 +230,7 @@ describe("Test Countly.q related methods and processes", () => {
});
});
});
// This Test checks if calling userData.save triggers sendEventsForced (it sends events from .q to event queue and then to request queue)
// This Test checks if calling userData.save triggers processAsyncQueue (it sends events from .q to event queue and then to request queue)
it("Check sending custom user info empties .q", () => {
hp.haltAndClearStorage(() => {
// Disable heartbeat and init the SDK
Expand All @@ -305,7 +257,7 @@ describe("Test Countly.q related methods and processes", () => {
// Check that events are still in .q
expect(Countly.q.length).to.equal(4);

// Trigger sendEventsForced by saving UserData
// Trigger processAsyncQueue by saving UserData
Countly.userData.set("name", "test_user");
Countly.userData.save();

Expand Down
17 changes: 14 additions & 3 deletions lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@
log(logLevelEnums.DEBUG, "initialize, No device ID type info from the previous session, falling back to DEVELOPER_SUPPLIED, for event flushing");
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
}
// process async queue before sending events
processAsyncQueue();
sendEventsForced();
// set them back to their initial values
this.device_id = undefined;
Expand Down Expand Up @@ -1715,6 +1717,8 @@
// eslint-disable-next-line eqeqeq
if (this.device_id != newId) {
if (!merge) {
// process async queue before sending events
processAsyncQueue();
// empty event queue
sendEventsForced();
// end current session
Expand Down Expand Up @@ -1970,7 +1974,10 @@
this.user_details = function (user) {
log(logLevelEnums.INFO, "user_details, Trying to add user details: ", user);
if (this.check_consent(featureEnums.USERS)) {
sendEventsForced(); // flush events to event queue to prevent a drill issue
// process async queue before sending events
processAsyncQueue();
// flush events to event queue to prevent a drill issue
sendEventsForced();
log(logLevelEnums.INFO, "user_details, flushed the event queue");
// truncating user values and custom object key value pairs
user.name = truncateSingleValue(user.name, self.maxValueSize, "user_details", log);
Expand Down Expand Up @@ -2174,7 +2181,10 @@
save: function save() {
log(logLevelEnums.INFO, "[userData] save, Saving changes to user's custom property");
if (self.check_consent(featureEnums.USERS)) {
sendEventsForced(); // flush events to event queue to prevent a drill issue
// process async queue before sending events
processAsyncQueue();
// flush events to event queue to prevent a drill issue
sendEventsForced();
log(logLevelEnums.INFO, "user_details, flushed the event queue");
toRequestQueue({
user_details: JSON.stringify({
Expand Down Expand Up @@ -2524,6 +2534,8 @@
this.start_time();
// end session on unload
add_event_listener(window, "beforeunload", function () {
// process async queue before sending events
processAsyncQueue();
// empty the event queue
sendEventsForced();
self.end_session();
Expand Down Expand Up @@ -4051,7 +4063,6 @@
* Check and send the events to request queue if there are any, empty the event queue
*/
function sendEventsForced() {
processAsyncQueue(); // process async queue before sending events
if (eventQueue.length > 0) {
log(logLevelEnums.DEBUG, "Flushing events");
toRequestQueue({
Expand Down

0 comments on commit ef8c455

Please sign in to comment.