Skip to content

Commit 0d592bc

Browse files
Merge pull request #463 from browserstack/AFD_1755_sync_duration_with_cli
AFD 1755 sync duration with cli
2 parents 5f73db8 + 1754609 commit 0d592bc

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

bin/helpers/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ if(config.env !== "production") {
1313
}
1414

1515
config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`;
16+
config.cypress_v2 = `${config.rails_host}/automate/cypress/v2`;
1617
config.buildUrl = `${config.cypress_v1}/builds/`;
18+
config.buildUrlV2 = `${config.cypress_v2}/builds/`;
1719
config.buildStopUrl = `${config.cypress_v1}/builds/stop/`;
1820
config.checkMd5sum = `${config.cypress_v1}/md5sumcheck/`;
1921
config.getInitialDetails = `${config.cypress_v1}/get_initial_details/`;

bin/helpers/sync/specsSummary.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ let printSpecsRunSummary = (data, machines, customErrorsToPrint) => {
3333
});
3434

3535
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}`);
36-
logger.info(`Done in ${data.duration/1000} seconds using ${machines} machines\n`);
36+
logger.info(`Done in ${data.duration} seconds using ${data.parallels} machines\n`);
37+
winstonlogger.debug(`CLI calculated duration is ${data.cliDuration/1000}`);
3738

3839
if (customErrorsToPrint && customErrorsToPrint.length > 0) {
3940
for (const error of customErrorsToPrint) {

bin/helpers/sync/syncSpecsLogs.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ let specSummary = {
1414
"buildError": null,
1515
"specs": [],
1616
"duration": null,
17+
"parallels": null,
18+
"cliDuration": null,
1719
"customErrorsToPrint": []
1820
}
1921

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

2527
let getOptions = (auth, build_id) => {
2628
return {
27-
url: `${config.buildUrl}${build_id}`,
29+
url: `${config.buildUrlV2}${build_id}`,
2830
auth: {
2931
user: auth.username,
3032
password: auth.access_key
@@ -120,7 +122,7 @@ let printSpecsStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => {
120122
}
121123
}
122124
logger.info(lineSeparator);
123-
specSummary.duration = endTime - startTime
125+
specSummary.cliDuration = endTime - startTime
124126
resolve(specSummary);
125127
}
126128
);
@@ -147,15 +149,15 @@ let whileProcess = (whilstCallback) => {
147149
switch (response.statusCode) {
148150
case 202: // get data here and print it
149151
n = 2
150-
showSpecsStatus(body);
152+
showSpecsStatus(body, 202);
151153
return setTimeout(whilstCallback, timeout * n, null);
152154
case 204: // No data available, wait for some time and ask again
153155
n = 1
154156
return setTimeout(whilstCallback, timeout * n, null);
155157
case 200: // Build is completed.
156158
whileLoop = false;
157159
endTime = Date.now();
158-
showSpecsStatus(body);
160+
showSpecsStatus(body, 200);
159161
return specSummary.exitCode == Constants.BUILD_FAILED_EXIT_CODE ?
160162
whilstCallback({ status: 204, message: "No specs ran in the build"} ) : whilstCallback(null, body);
161163
default:
@@ -169,9 +171,9 @@ let getStackTraceUrl = () => {
169171
return specSummary.buildError
170172
}
171173

172-
let showSpecsStatus = (data) => {
174+
let showSpecsStatus = (data, statusCode) => {
173175
let specData = JSON.parse(data);
174-
specData.forEach(specDetails => {
176+
specData["specData"].forEach(specDetails => {
175177
if (specDetails.type === Constants.CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY) {
176178
addCustomErrorToPrint(specDetails);
177179
} else {
@@ -190,6 +192,17 @@ let showSpecsStatus = (data) => {
190192
}
191193
}
192194
});
195+
if ( statusCode != 200 ) return;
196+
// Below block is for printing build details, return if non 200 status code
197+
if ("buildData" in specData) {
198+
const buildDetails = specData.buildData;
199+
const totalDuration = (utils.isUndefined(buildDetails.duration)) ? "-" : buildDetails.duration.total_duration
200+
const parallels = (utils.isUndefined(buildDetails.parallels)) ? "-" : buildDetails.parallels
201+
specSummary.duration = totalDuration;
202+
specSummary.parallels = parallels;
203+
} else {
204+
logger.debug(`Build details not sent`)
205+
}
193206
}
194207

195208
let printInitialLog = () => {

test/unit/bin/helpers/sync/specSummary.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("printSpecsRunSummary", () => {
2929
});
3030

3131
context("with data", () => {
32-
let time = 6000,
32+
let time = 6,
3333
machines = 2,
3434
specs = [
3535
{specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'},
@@ -40,6 +40,7 @@ describe("printSpecsRunSummary", () => {
4040
data = {
4141
specs: specs,
4242
duration: time,
43+
parallels: machines,
4344
exitCode: 0
4445
};
4546

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

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

5354
loggerInfoSpy.restore();
5455
});
5556
});
5657

5758
context("with custom error data", () => {
58-
let time = 6000,
59+
let time = 6,
5960
machines = 2,
6061
specs = [
6162
{specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'},
@@ -66,6 +67,7 @@ describe("printSpecsRunSummary", () => {
6667
data = {
6768
specs: specs,
6869
duration: time,
70+
parallels: machines,
6971
exitCode: 0
7072
},
7173
customErrorsToPrint = [
@@ -78,7 +80,8 @@ describe("printSpecsRunSummary", () => {
7880

7981
specSummary.printSpecsRunSummary(data, machines, customErrorsToPrint);
8082
sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0');
81-
sinon.assert.calledWith(loggerInfoSpy, `Done in ${time / 1000} seconds using ${machines} machines\n`);
83+
sinon.assert.calledWith(loggerInfoSpy, `Done in ${data.duration} seconds using ${data.parallels} machines\n`);
84+
8285
sinon.assert.calledWith(loggerWarnSpy, `custom error message`);
8386

8487
loggerInfoSpy.restore();

test/unit/bin/helpers/sync/syncSpecsLogs.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe("syncSpecsLogs", () => {
8080

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

217219
context("showSpecsStatus", () => {
218220
const showSpecsStatus = syncSpecsLogs.__get__("showSpecsStatus");
221+
const buildCreatedStatusCode = 202
222+
const buildRunningStatusCode = 204
223+
const buildCompletedStatusCode = 200
219224

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

225-
showSpecsStatus(data);
230+
showSpecsStatus(data, buildCreatedStatusCode);
226231

227232
expect(printInitialLog.calledOnce).to.be.false;
228233
});
229234

230235
it('should print spec details when spec related data is sent in polling response', () => {
231236
let specResult = JSON.stringify({"path": "path"})
232-
let data = JSON.stringify([specResult])
237+
let data = JSON.stringify({ "specData": [specResult], "buildData": {"duration": "NA", "parallels": "NA"}})
233238
var printSpecData = sandbox.stub();
234239
syncSpecsLogs.__set__('printSpecData', printSpecData);
235-
showSpecsStatus(data);
240+
showSpecsStatus(data, buildRunningStatusCode);
236241
expect(printSpecData.calledOnce).to.be.true;
237242
});
238243

239244
it('should print initial and spec details when spec related data is sent in polling response', () => {
240245
let specResult = JSON.stringify({"path": "path"})
241246
syncSpecsLogs.__set__('buildStarted', false)
242-
let data = JSON.stringify(["created", specResult])
247+
let data = JSON.stringify({ "specData": [specResult], "buildData": {"duration": "NA", "parallels": "NA"}})
243248
var printSpecData = sandbox.stub();
244249
syncSpecsLogs.__set__('printSpecData', printSpecData);
245250
var printInitialLog = sandbox.stub();
246251
syncSpecsLogs.__set__('printInitialLog', printInitialLog);
247-
showSpecsStatus(data);
252+
showSpecsStatus(data, buildCreatedStatusCode);
248253
expect(printSpecData.calledOnce).to.be.true;
249254
expect(printInitialLog.calledOnce).to.be.true;
250255
});
@@ -253,14 +258,14 @@ describe("syncSpecsLogs", () => {
253258
let specResult = JSON.stringify({"path": "path"})
254259
let customError = { id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" }
255260
syncSpecsLogs.__set__('buildStarted', false)
256-
let data = JSON.stringify(["created", specResult, customError])
261+
let data = JSON.stringify({ "specData": ["created", specResult, customError], "buildData": {"duration": "NA", "parallels": "NA"}})
257262
var printSpecData = sandbox.stub();
258263
syncSpecsLogs.__set__('printSpecData', printSpecData);
259264
var printInitialLog = sandbox.stub();
260265
syncSpecsLogs.__set__('printInitialLog', printInitialLog);
261266
var addCustomErrorToPrint = sandbox.stub();
262267
syncSpecsLogs.__set__('addCustomErrorToPrint', addCustomErrorToPrint);
263-
showSpecsStatus(data);
268+
showSpecsStatus(data, buildRunningStatusCode);
264269
expect(printSpecData.calledOnce).to.be.true;
265270
expect(printInitialLog.calledOnce).to.be.true;
266271
expect(addCustomErrorToPrint.calledOnce).to.be.true;
@@ -307,7 +312,7 @@ describe("syncSpecsLogs", () => {
307312
expect(tableStream.calledOnce).to.be.true;
308313
expect(whileProcess.calledOnce).to.be.false;
309314
expect(specSummary.specs).deep.to.equal([])
310-
expect(specSummary.duration).to.eql(endTime - startTime);
315+
expect(specSummary.cliDuration).to.eql(endTime - startTime);
311316
});
312317
});
313318

@@ -322,7 +327,7 @@ describe("syncSpecsLogs", () => {
322327
expect(getTableConfig.calledOnce).to.be.true;
323328
expect(tableStream.calledOnce).to.be.true;
324329
expect(whileProcess.callCount).to.eql(3);
325-
expect(specSummary.duration).to.eql(endTime - startTime);
330+
expect(specSummary.cliDuration).to.eql(endTime - startTime);
326331
});
327332
});
328333
});

0 commit comments

Comments
 (0)