Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bin/testObservability/helper/cleanupQueueSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Sending all the remaining queues for synchronous manner
*/

const RequestQueueHandler = require('./requestQueueHandler');

const shutdown = async () => {
const requestHandler = new RequestQueueHandler();
requestHandler.queue = require(process.argv[2].trim());
await requestHandler.shutdown();
}

shutdown();
2 changes: 2 additions & 0 deletions bin/testObservability/helper/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ exports.OBSERVABILITY_ENV_VARS = [
exports.TEST_OBSERVABILITY_REPORTER = 'browserstack-cypress-cli/bin/testObservability/reporter';

exports.TEST_OBSERVABILITY_REPORTER_LOCAL = path.join(__dirname, '..', 'reporter');

exports.PENDING_QUEUS_FILE = `pending_queues_${process.pid}.json`;
11 changes: 8 additions & 3 deletions bin/testObservability/helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const request = require('request');
const request = require('requestretry');
const { v4: uuidv4 } = require('uuid');
const os = require('os');
const { promisify } = require('util');
Expand All @@ -17,6 +17,8 @@ const logger = require("../../helpers/logger").winstonLogger;
const utils = require('../../helpers/utils');
const helper = require('../../helpers/helper');

const util = require('util');

const CrashReporter = require('../crashReporter');

// Getting global packages path
Expand Down Expand Up @@ -114,7 +116,8 @@ const nodeRequest = (type, url, data, config) => {
url: `${API_URL}/${url}`,
body: data,
json: config.headers['Content-Type'] === 'application/json',
agent: this.httpsKeepAliveAgent
agent: this.httpsKeepAliveAgent,
maxAttempts: 2
}};

if(url === exports.requestQueueHandler.screenshotEventUrl) {
Expand All @@ -127,7 +130,7 @@ const nodeRequest = (type, url, data, config) => {
} else if(response.statusCode != 200) {
reject(response && response.body ? response.body : `Received response from BrowserStack Server with status : ${response.statusCode}`);
} else {
try {
try {
if(typeof(body) !== 'object') body = JSON.parse(body);
} catch(e) {
if(!url.includes('/stop')) {
Expand All @@ -142,6 +145,8 @@ const nodeRequest = (type, url, data, config) => {
});
}

exports.nodeRequest = nodeRequest;

exports.failureData = (errors,tag) => {
if(!errors) return [];
try {
Expand Down
17 changes: 15 additions & 2 deletions bin/testObservability/helper/requestQueueHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { BATCH_SIZE, BATCH_INTERVAL, consoleHolder } = require('./constants');
const { debug, batchAndPostEvents } = require('./helper');
const fs = require('fs');
const cp = require('child_process');
const path = require('path');

const { BATCH_SIZE, BATCH_INTERVAL, PENDING_QUEUES_FILE } = require('./constants');
const { batchAndPostEvents } = require('./helper');

class RequestQueueHandler {
constructor() {
Expand Down Expand Up @@ -48,6 +52,15 @@ class RequestQueueHandler {
}
}

shutdownSync = () => {
this.removeEventBatchPolling('REMOVING');

fs.writeFileSync(path.join(__dirname, PENDING_QUEUES_FILE), JSON.stringify(this.queue));
this.queue = [];
cp.spawnSync('node', [path.join(__dirname, 'cleanupQueueSync.js'), path.join(__dirname, PENDING_QUEUS_FILE)], {stdio: 'inherit'});
fs.unlinkSync(path.join(__dirname, PENDING_QUEUES_FILE));
}

shutdown = async () => {
this.removeEventBatchPolling('REMOVING');
while(this.queue.length > 0) {
Expand Down
4 changes: 4 additions & 0 deletions bin/testObservability/plugin/ipcServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ipc = require('node-ipc');
const { consoleHolder } = require('../helper/constants');
const { requestQueueHandler } = require('../helper/helper');

exports.startIPCServer = (subscribeServerEvents, unsubscribeServerEvents) => {
if (ipc.server) {
Expand All @@ -26,6 +27,9 @@ exports.startIPCServer = (subscribeServerEvents, unsubscribeServerEvents) => {
process.on('exit', () => {
unsubscribeServerEvents(ipc.server);
ipc.server.stop();
// Cleaning up all remaining event in request queue handler. Any synchronous operations
// on exit handler will block the process
requestQueueHandler.shutdownSync();
});

});
Expand Down
6 changes: 4 additions & 2 deletions bin/testObservability/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const { v4: uuidv4 } = require('uuid');
const { IPC_EVENTS } = require('../helper/constants');
const { startIPCServer } = require('../plugin/ipcServer');

const ipc = require('node-ipc');

const HOOK_TYPES_MAP = {
"before all": "BEFORE_ALL",
"after all": "AFTER_ALL",
Expand Down Expand Up @@ -186,7 +188,6 @@ class MyReporter {
}

await this.uploadTestSteps();
await requestQueueHandler.shutdown();
});
}

Expand All @@ -199,6 +200,7 @@ class MyReporter {
server.on(IPC_EVENTS.COMMAND, this.cypressCommandListener.bind(this));
server.on(IPC_EVENTS.CUCUMBER, this.cypressCucumberStepListener.bind(this));
server.on(IPC_EVENTS.PLATFORM_DETAILS, this.cypressPlatformDetailsListener.bind(this));
this.ipcServer = server;
},
(server) => {
server.off(IPC_EVENTS.CONFIG, '*');
Expand All @@ -213,7 +215,7 @@ class MyReporter {
const lastTest = this.current_test;
this.current_test = test;
test.retryOf = null;
test.testAnalyticsId = uuidv4();
test.testAnalyticsId = uuidv4()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed?

test.started_at = (new Date()).toISOString();
test.test_started_at = test.started_at;
if(test._currentRetry > 0 && lastTest && lastTest.title == test.title) {
Expand Down
Loading