Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 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: 10 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 All @@ -39,6 +41,7 @@ exports.pending_test_uploads = {
};

exports.debug = (text, shouldReport = false, throwable = null) => {
consoleHolder.log(`[ OBSERVABILITY ] ${text}`);
if (process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "true" || process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "1") {
logger.info(`[ OBSERVABILITY ] ${text}`);
}
Expand Down Expand Up @@ -114,7 +117,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 +131,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 +146,8 @@ const nodeRequest = (type, url, data, config) => {
});
}

exports.nodeRequest = nodeRequest;

exports.failureData = (errors,tag) => {
if(!errors) return [];
try {
Expand Down Expand Up @@ -391,6 +397,7 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
exports.debug('Build creation successfull!');
process.env.BS_TESTOPS_BUILD_COMPLETED = true;
setEnvironmentVariablesForRemoteReporter(response.data.jwt, response.data.build_hashed_id, response.data.allow_screenshots, data.observability_version.sdkVersion);
consoleHolder.log(response.data.build_hashed_id);
if(this.isBrowserstackInfra()) helper.setBrowserstackCypressCliDependency(user_config);
} catch(error) {
if(!error.errorType) {
Expand Down
9 changes: 9 additions & 0 deletions bin/testObservability/helper/requestQueueHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ class RequestQueueHandler {
}
}

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

require('fs').writeFileSync(require('path').join(__dirname, 'queue.json'), JSON.stringify(this.queue));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's rename file queue.json to something more meaningful?

this.queue = [];
require('child_process').spawnSync('node', [require('path').join(__dirname, 'shutdown.js'), require('path').join(__dirname, 'queue.json')], {stdio: 'inherit'});
}

shutdown = async () => {
this.removeEventBatchPolling('REMOVING');
while(this.queue.length > 0) {
const data = this.queue.slice(0,BATCH_SIZE);
this.queue.splice(0,BATCH_SIZE);
consoleHolder.log(this.queue.length + " the queue length ");
await batchAndPostEvents(this.eventUrl,'Shutdown-Queue',data);
}
}
Expand Down
9 changes: 9 additions & 0 deletions bin/testObservability/helper/shutdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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/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,7 @@ exports.startIPCServer = (subscribeServerEvents, unsubscribeServerEvents) => {
process.on('exit', () => {
unsubscribeServerEvents(ipc.server);
ipc.server.stop();
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