Skip to content

Commit

Permalink
Merge pull request #463 from browserstack/AFD_1755_sync_duration_with…
Browse files Browse the repository at this point in the history
…_cli

AFD 1755 sync duration with cli
  • Loading branch information
agrawalsaurabhs authored Mar 1, 2023
2 parents 5f73db8 + 1754609 commit 0d592bc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
2 changes: 2 additions & 0 deletions bin/helpers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ if(config.env !== "production") {
}

config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`;
config.cypress_v2 = `${config.rails_host}/automate/cypress/v2`;
config.buildUrl = `${config.cypress_v1}/builds/`;
config.buildUrlV2 = `${config.cypress_v2}/builds/`;
config.buildStopUrl = `${config.cypress_v1}/builds/stop/`;
config.checkMd5sum = `${config.cypress_v1}/md5sumcheck/`;
config.getInitialDetails = `${config.cypress_v1}/get_initial_details/`;
Expand Down
3 changes: 2 additions & 1 deletion bin/helpers/sync/specsSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ let printSpecsRunSummary = (data, machines, customErrorsToPrint) => {
});

logger.info(`Total tests: ${summary.total}, passed: ${summary.passed}, failed: ${summary.failed}, skipped: ${summary.skipped}, passed_with_skipped: ${summary.passed_with_skipped}, pending: ${summary.pending}`);
logger.info(`Done in ${data.duration/1000} seconds using ${machines} machines\n`);
logger.info(`Done in ${data.duration} seconds using ${data.parallels} machines\n`);
winstonlogger.debug(`CLI calculated duration is ${data.cliDuration/1000}`);

if (customErrorsToPrint && customErrorsToPrint.length > 0) {
for (const error of customErrorsToPrint) {
Expand Down
25 changes: 19 additions & 6 deletions bin/helpers/sync/syncSpecsLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ let specSummary = {
"buildError": null,
"specs": [],
"duration": null,
"parallels": null,
"cliDuration": null,
"customErrorsToPrint": []
}

Expand All @@ -24,7 +26,7 @@ if (!isNaN(terminalWidth)) lineSeparator = "\n" + "-".repeat(terminalWidth);

let getOptions = (auth, build_id) => {
return {
url: `${config.buildUrl}${build_id}`,
url: `${config.buildUrlV2}${build_id}`,
auth: {
user: auth.username,
password: auth.access_key
Expand Down Expand Up @@ -120,7 +122,7 @@ let printSpecsStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => {
}
}
logger.info(lineSeparator);
specSummary.duration = endTime - startTime
specSummary.cliDuration = endTime - startTime
resolve(specSummary);
}
);
Expand All @@ -147,15 +149,15 @@ let whileProcess = (whilstCallback) => {
switch (response.statusCode) {
case 202: // get data here and print it
n = 2
showSpecsStatus(body);
showSpecsStatus(body, 202);
return setTimeout(whilstCallback, timeout * n, null);
case 204: // No data available, wait for some time and ask again
n = 1
return setTimeout(whilstCallback, timeout * n, null);
case 200: // Build is completed.
whileLoop = false;
endTime = Date.now();
showSpecsStatus(body);
showSpecsStatus(body, 200);
return specSummary.exitCode == Constants.BUILD_FAILED_EXIT_CODE ?
whilstCallback({ status: 204, message: "No specs ran in the build"} ) : whilstCallback(null, body);
default:
Expand All @@ -169,9 +171,9 @@ let getStackTraceUrl = () => {
return specSummary.buildError
}

let showSpecsStatus = (data) => {
let showSpecsStatus = (data, statusCode) => {
let specData = JSON.parse(data);
specData.forEach(specDetails => {
specData["specData"].forEach(specDetails => {
if (specDetails.type === Constants.CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY) {
addCustomErrorToPrint(specDetails);
} else {
Expand All @@ -190,6 +192,17 @@ let showSpecsStatus = (data) => {
}
}
});
if ( statusCode != 200 ) return;
// Below block is for printing build details, return if non 200 status code
if ("buildData" in specData) {
const buildDetails = specData.buildData;
const totalDuration = (utils.isUndefined(buildDetails.duration)) ? "-" : buildDetails.duration.total_duration
const parallels = (utils.isUndefined(buildDetails.parallels)) ? "-" : buildDetails.parallels
specSummary.duration = totalDuration;
specSummary.parallels = parallels;
} else {
logger.debug(`Build details not sent`)
}
}

let printInitialLog = () => {
Expand Down
11 changes: 7 additions & 4 deletions test/unit/bin/helpers/sync/specSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("printSpecsRunSummary", () => {
});

context("with data", () => {
let time = 6000,
let time = 6,
machines = 2,
specs = [
{specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'},
Expand All @@ -40,6 +40,7 @@ describe("printSpecsRunSummary", () => {
data = {
specs: specs,
duration: time,
parallels: machines,
exitCode: 0
};

Expand All @@ -48,14 +49,14 @@ describe("printSpecsRunSummary", () => {

specSummary.printSpecsRunSummary(data, machines);
sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0');
sinon.assert.calledWith(loggerInfoSpy, `Done in ${time / 1000} seconds using ${machines} machines\n`);
sinon.assert.calledWith(loggerInfoSpy, `Done in ${data.duration} seconds using ${data.parallels} machines\n`);

loggerInfoSpy.restore();
});
});

context("with custom error data", () => {
let time = 6000,
let time = 6,
machines = 2,
specs = [
{specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'},
Expand All @@ -66,6 +67,7 @@ describe("printSpecsRunSummary", () => {
data = {
specs: specs,
duration: time,
parallels: machines,
exitCode: 0
},
customErrorsToPrint = [
Expand All @@ -78,7 +80,8 @@ describe("printSpecsRunSummary", () => {

specSummary.printSpecsRunSummary(data, machines, customErrorsToPrint);
sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0');
sinon.assert.calledWith(loggerInfoSpy, `Done in ${time / 1000} seconds using ${machines} machines\n`);
sinon.assert.calledWith(loggerInfoSpy, `Done in ${data.duration} seconds using ${data.parallels} machines\n`);

sinon.assert.calledWith(loggerWarnSpy, `custom error message`);

loggerInfoSpy.restore();
Expand Down
27 changes: 16 additions & 11 deletions test/unit/bin/helpers/sync/syncSpecsLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("syncSpecsLogs", () => {

it('should return proper request option for polling', () => {
let options = getOptions(auth, build_id);
expect(options.url).to.equal(`${config.buildUrl}${build_id}`);
expect(options.url).to.equal(`${config.buildUrlV2}${build_id}`);
expect(options.auth.user).to.equal(auth.username);
expect(options.auth.password).to.equal(auth.access_key);
expect(options.headers["Content-Type"]).to.equal("application/json");
Expand All @@ -93,6 +93,8 @@ describe("syncSpecsLogs", () => {
"buildError": null,
"specs": [],
"duration": null,
"parallels": null,
"cliDuration": null,
"customErrorsToPrint": [
{ id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" }
]
Expand Down Expand Up @@ -216,35 +218,38 @@ describe("syncSpecsLogs", () => {

context("showSpecsStatus", () => {
const showSpecsStatus = syncSpecsLogs.__get__("showSpecsStatus");
const buildCreatedStatusCode = 202
const buildRunningStatusCode = 204
const buildCompletedStatusCode = 200

it('should not print initial log for running specs when it is the 1st polling response', () => {
let data = JSON.stringify(["created"])
let data = JSON.stringify({ "specData": ["created"], "buildData": {"duration": "NA", "parallels": "NA"}})
var printInitialLog = sandbox.stub();
syncSpecsLogs.__set__('printInitialLog', printInitialLog);

showSpecsStatus(data);
showSpecsStatus(data, buildCreatedStatusCode);

expect(printInitialLog.calledOnce).to.be.false;
});

it('should print spec details when spec related data is sent in polling response', () => {
let specResult = JSON.stringify({"path": "path"})
let data = JSON.stringify([specResult])
let data = JSON.stringify({ "specData": [specResult], "buildData": {"duration": "NA", "parallels": "NA"}})
var printSpecData = sandbox.stub();
syncSpecsLogs.__set__('printSpecData', printSpecData);
showSpecsStatus(data);
showSpecsStatus(data, buildRunningStatusCode);
expect(printSpecData.calledOnce).to.be.true;
});

it('should print initial and spec details when spec related data is sent in polling response', () => {
let specResult = JSON.stringify({"path": "path"})
syncSpecsLogs.__set__('buildStarted', false)
let data = JSON.stringify(["created", specResult])
let data = JSON.stringify({ "specData": [specResult], "buildData": {"duration": "NA", "parallels": "NA"}})
var printSpecData = sandbox.stub();
syncSpecsLogs.__set__('printSpecData', printSpecData);
var printInitialLog = sandbox.stub();
syncSpecsLogs.__set__('printInitialLog', printInitialLog);
showSpecsStatus(data);
showSpecsStatus(data, buildCreatedStatusCode);
expect(printSpecData.calledOnce).to.be.true;
expect(printInitialLog.calledOnce).to.be.true;
});
Expand All @@ -253,14 +258,14 @@ describe("syncSpecsLogs", () => {
let specResult = JSON.stringify({"path": "path"})
let customError = { id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" }
syncSpecsLogs.__set__('buildStarted', false)
let data = JSON.stringify(["created", specResult, customError])
let data = JSON.stringify({ "specData": ["created", specResult, customError], "buildData": {"duration": "NA", "parallels": "NA"}})
var printSpecData = sandbox.stub();
syncSpecsLogs.__set__('printSpecData', printSpecData);
var printInitialLog = sandbox.stub();
syncSpecsLogs.__set__('printInitialLog', printInitialLog);
var addCustomErrorToPrint = sandbox.stub();
syncSpecsLogs.__set__('addCustomErrorToPrint', addCustomErrorToPrint);
showSpecsStatus(data);
showSpecsStatus(data, buildRunningStatusCode);
expect(printSpecData.calledOnce).to.be.true;
expect(printInitialLog.calledOnce).to.be.true;
expect(addCustomErrorToPrint.calledOnce).to.be.true;
Expand Down Expand Up @@ -307,7 +312,7 @@ describe("syncSpecsLogs", () => {
expect(tableStream.calledOnce).to.be.true;
expect(whileProcess.calledOnce).to.be.false;
expect(specSummary.specs).deep.to.equal([])
expect(specSummary.duration).to.eql(endTime - startTime);
expect(specSummary.cliDuration).to.eql(endTime - startTime);
});
});

Expand All @@ -322,7 +327,7 @@ describe("syncSpecsLogs", () => {
expect(getTableConfig.calledOnce).to.be.true;
expect(tableStream.calledOnce).to.be.true;
expect(whileProcess.callCount).to.eql(3);
expect(specSummary.duration).to.eql(endTime - startTime);
expect(specSummary.cliDuration).to.eql(endTime - startTime);
});
});
});
Expand Down

0 comments on commit 0d592bc

Please sign in to comment.