Skip to content

Commit

Permalink
test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
turtledreams committed Oct 14, 2024
1 parent a17b932 commit 50394dc
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 388 deletions.
8 changes: 4 additions & 4 deletions lib/countly-bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const StorageTypes = cc.storageTypeEnums;
* @param {number} [conf.fail_timeout=60] - set time in seconds to wait after failed connection to server in seconds
* @param {number} [conf.session_update=60] - how often in seconds should session be extended
* @param {number} [conf.max_events=100] - maximum amount of events to send in one batch
* @deprecated {boolean} [conf.persist_queue=false] - persistent mode instead of using in-memory queue. Use storage_type and storage_path instead
* @param {boolean} [conf.force_post=false] - force using post method for all requests
* @param {string} [conf.storage_path] - where SDK would store data, including id, queues, etc
* @param {string} [conf.http_options=] - function to get http options by reference and overwrite them, before running each request
Expand All @@ -51,6 +50,7 @@ const StorageTypes = cc.storageTypeEnums;
* @param {number} [conf.max_stack_trace_line_length=200] - maximum amount of characters are allowed per stack trace line. This limits also the crash message length
* @param {StorageTypes} conf.storage_type - to determine which storage type is going to be applied
* @param {Object} conf.custom_storage_method - user given storage methods
* @param {boolean} [conf.persist_queue=false] - *DEPRECATED* persistent mode instead of using in-memory queue. Use storage_type and storage_path instead
* @example
* var server = new CountlyBulk({
* app_key: "{YOUR-API-KEY}",
Expand Down Expand Up @@ -607,21 +607,21 @@ function CountlyBulk(conf) {
* getBulkEventQueue is a testing purposed method which returns the event queue object
* @returns {Object} eventQueue
*/
this.getBulkEventQueue = function() {
this._getBulkEventQueue = function() {
return eventQueue;
};
/**
* getBulkRequestQueue is a testing purposed method which returns the request queue object
* @returns {Object} requestQueue
*/
this.getBulkRequestQueue = function() {
this._getBulkRequestQueue = function() {
return requestQueue;
};
/**
* getBulkQueue is a testing purposed method which returns the bulk queue object
* @returns {Object} bulkQueue
*/
this.getBulkQueue = function() {
this._getBulkQueue = function() {
return bulkQueue;
};
}
Expand Down
7 changes: 6 additions & 1 deletion lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Countly.Bulk = Bulk;
var maxStackTraceLinesPerThread = 30;
var maxStackTraceLineLength = 200;
var deviceIdType = null;
var heartBeatTimer = null;
/**
* Array with list of available features that you can require consent for
*/
Expand Down Expand Up @@ -320,6 +321,10 @@ Countly.Bulk = Bulk;
maxStackTraceLinesPerThread = 30;
maxStackTraceLineLength = 200;
deviceIdType = null;
if (heartBeatTimer) {
clearInterval(heartBeatTimer);
heartBeatTimer = null;
}

// cc DEBUG
cc.debug = false;
Expand Down Expand Up @@ -1479,7 +1484,7 @@ Countly.Bulk = Bulk;
}, "heartBeat", false);
}

setTimeout(heartBeat, beatInterval);
heartBeatTimer = setTimeout(heartBeat, beatInterval);
}

/**
Expand Down
82 changes: 50 additions & 32 deletions test/helpers/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@
var path = require("path");
var assert = require("assert");
var fs = require("fs");
var fsp = require("fs/promises");
var Countly = require("../../lib/countly");

// paths for convenience
var dir = path.resolve(__dirname, "../../");
var defaultStoragePath = (`${dir}/data`);
var defaultBulkStoragePath = (`${dir}/bulk_data`);
var idDir = (`${dir}/data/__cly_id.json`);
var idTypeDir = (`${dir}/data/__cly_id_type.json`);
var eventDir = (`${dir}/data/__cly_event.json`);
var reqDir = (`${dir}/data/__cly_queue.json`);
var bulkEventDir = (`${dir}/bulk_data/__cly_bulk_event.json`);
var bulkReqQueueDir = (`${dir}/bulk_data/__cly_req_queue.json`);
var bulkQueueDir = (`${dir}/bulk_data/__cly_req_queue.json`);
var dir_test = path.resolve(__dirname, "../");

// paths for convenience
const DIR_CLY = (`${dir}/data`);
const DIR_CLY_ID = (`${dir}/data/__cly_id.json`);
const DIR_CLY_ID_type = (`${dir}/data/__cly_id_type.json`);
const DIR_CLY_event = (`${dir}/data/__cly_event.json`);
const DIR_CLY_request = (`${dir}/data/__cly_queue.json`);

// Bulk paths for convenience
const DIR_Bulk = (`${dir}/bulk_data`);
const DIR_Bulk_bulk = (`${dir}/bulk_data/__cly_bulk_queue.json`);
const DIR_Bulk_event = (`${dir}/bulk_data/__cly_bulk_event.json`);
const DIR_Bulk_request = (`${dir}/bulk_data/__cly_req_queue.json`);

// Custom
const DIR_Test = (`${dir_test}/customStorageDirectory`);
const DIR_Test_bulk = (`${dir_test}/customStorageDirectory/__cly_bulk_queue.json`);
const DIR_Test_event = (`${dir_test}/customStorageDirectory/ __cly_bulk_event.json`);
const DIR_Test_request = (`${dir_test}/customStorageDirectory/__cly_req_queue.json`);

// timeout variables
var sWait = 50;
var mWait = 3000;
var lWait = 10000;
const sWait = 50;
const mWait = 3000;
const lWait = 10000;

// parsing event queue
function readEventQueue(givenPath = null, isBulk = false) {
var destination = eventDir;
var destination = DIR_CLY_event;
if (givenPath !== null) {
destination = givenPath;
}
Expand All @@ -35,7 +47,7 @@ function readEventQueue(givenPath = null, isBulk = false) {
}
// parsing request queue
function readRequestQueue(givenPath = null, isBulk = false) {
var destination = reqDir;
var destination = DIR_CLY_request;
if (givenPath !== null) {
destination = givenPath;
}
Expand All @@ -45,10 +57,15 @@ function readRequestQueue(givenPath = null, isBulk = false) {
}
return a;
}
function doesFileStoragePathsExist(callback, isBulk = false) {
const paths = isBulk
? [bulkQueueDir, bulkReqQueueDir, bulkEventDir]
: [idDir, idTypeDir, eventDir];
function doesFileStoragePathsExist(callback, isBulk = false, testPath = false) {
var paths = [DIR_CLY_ID, DIR_CLY_ID_type, DIR_CLY_event, DIR_CLY_request];

if (isBulk) {
paths = [DIR_Bulk_request, DIR_Bulk_event, DIR_Bulk_bulk];
}
else if (testPath) {
paths = [DIR_Test_bulk, DIR_Test_event, DIR_Test_request];
}

let errors = 0;
paths.forEach((p, index) => {
Expand All @@ -62,25 +79,26 @@ function doesFileStoragePathsExist(callback, isBulk = false) {
});
});
}
function clearStorage(customPath = null) {
async function clearStorage(customPath = null) {
Countly.halt(true);
// Construct the relative path to the target folder

const relativePath = `../${customPath}`;
// Use path.resolve to create the absolute path
const resolvedCustomPath = path.resolve(__dirname, relativePath);

if (fs.existsSync(defaultStoragePath)) {
fs.rmSync(defaultStoragePath, { recursive: true, force: true });
}
if (fs.existsSync(defaultBulkStoragePath)) {
fs.rmSync(defaultBulkStoragePath, { recursive: true, force: true });
}
if (resolvedCustomPath !== null && typeof resolvedCustomPath === 'string' && fs.existsSync(resolvedCustomPath)) {
fs.rmSync(resolvedCustomPath, { recursive: true, force: true });
await fsp.rm(DIR_CLY, { recursive: true, force: true }).catch(() => { });
await fsp.rm(DIR_Bulk, { recursive: true, force: true }).catch(() => { });
await fsp.rm(DIR_Test, { recursive: true, force: true }).catch(() => { });

if (resolvedCustomPath !== null && typeof resolvedCustomPath === 'string') {
await fsp.rm(resolvedCustomPath, { recursive: true, force: true }).catch(() => { });
}

// make sure the directories are removed
if (fs.existsSync(defaultStoragePath) || fs.existsSync(defaultBulkStoragePath) || (resolvedCustomPath !== null && fs.existsSync(resolvedCustomPath))) {
const storageExists = await fsp.access(DIR_CLY).then(() => true).catch(() => false);
const bulkStorageExists = await fsp.access(DIR_Bulk).then(() => true).catch(() => false);
const customTestStorage = await fsp.access(DIR_Test).then(() => true).catch(() => false);
const customStorageExists = resolvedCustomPath !== null ? await fsp.access(resolvedCustomPath).then(() => true).catch(() => false) : false;

if (storageExists || bulkStorageExists || customTestStorage || customStorageExists) {
throw new Error("Failed to clear storage");
}
}
Expand Down Expand Up @@ -143,7 +161,7 @@ function requestBaseParamValidator(resultingObject, id) {
assert.ok(typeof resultingObject.sdk_version !== 'undefined');
assert.ok(typeof resultingObject.timestamp !== 'undefined');
assert.ok(resultingObject.dow > -1 && resultingObject.dow < 24);
assert.ok(resultingObject.dow > 0 && resultingObject.dow < 8);
assert.ok(resultingObject.dow >= 0 && resultingObject.dow < 8);
}
/**
* bunch of tests specifically gathered for testing crashes
Expand Down
Loading

0 comments on commit 50394dc

Please sign in to comment.