Skip to content

Commit b4af193

Browse files
committed
hanndle no internet case
1 parent 370db2d commit b4af193

File tree

8 files changed

+116
-71
lines changed

8 files changed

+116
-71
lines changed

bin/helpers/constants.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
let config = require("./config");
2+
13
const syncCLI = {
24
FAILED_SPEC_DETAILS_COL_HEADER: ['Spec', 'Status', 'Browser', 'BrowserStack Session ID'],
35
LOGS: {
@@ -30,7 +32,10 @@ const userMessages = {
3032
UPLOADING_TESTS: "Uploading the tests to BrowserStack",
3133
LOCAL_TRUE: "you will now be able to test localhost / private URLs",
3234
LOCAL_FALSE: "you won't be able to test localhost / private URLs",
33-
EXIT_SYNC_CLI_MESSAGE: "Exiting the CLI, but your build is still running. You can use the --sync option to keep getting test updates. You can also use the build-info <build-id> command now."
35+
EXIT_SYNC_CLI_MESSAGE: "Exiting the CLI, but your build is still running. You can use the --sync option to keep getting test updates. You can also use the build-info <build-id> command now.",
36+
FATAL_NETWORK_ERROR: `fatal: unable to access '${config.buildUrl}': Could not resolve host: ${config.rails_host}`,
37+
RETRY_LIMIT_EXCEEDED: `Max retries exceeded trying to connect to the host (retries: ${config.retries})`,
38+
CHECK_DASHBOARD_AT: "Please check the build status at:"
3439
};
3540

3641
const validationMessages = {

bin/helpers/sync/failedSpecsDetails.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const tablePrinter = require('table'), // { table, getBorderCharacters }
22
chalk = require('chalk'),
33
Constants = require("../constants"),
4-
logger = require("../logger").syncCliLogger;
4+
logger = require("../logger").syncCliLogger,
5+
config = require("../config");
56

67
/**
78
*
@@ -49,7 +50,7 @@ let failedSpecsDetails = (data) => {
4950
]);
5051
});
5152

52-
let config = {
53+
let tableConfig = {
5354
border: tablePrinter.getBorderCharacters('ramac'),
5455
columns: {
5556
0: { alignment: 'left' },
@@ -68,12 +69,12 @@ let failedSpecsDetails = (data) => {
6869
}
6970
}
7071

71-
let result = tablePrinter.table(specData, config);
72+
let result = tablePrinter.table(specData, tableConfig);
7273

7374
logger.info('\nFailed / skipped test report:');
7475
logger.info(result);
7576

76-
if (failedSpecs && data.exitCode !== 2) data.exitCode = 1 ; // specs failed, send exitCode as 1
77+
if (failedSpecs && data.exitCode !== config.networkErrorExitCode) data.exitCode = 1 ; // specs failed, send exitCode as 1
7778
resolve(data); // No Specs failed, maybe skipped, but not failed, send exitCode as 0
7879
});
7980
}

bin/helpers/sync/syncSpecsLogs.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,15 @@ let printSpecsStatus = (bsConfig, buildDetails) => {
9292
let whileProcess = (whilstCallback) => {
9393
request.post(options, function(error, response, body) {
9494
if (error) {
95-
if (error.code === "ETIMEDOUT") {
96-
whileTries -= 1;
97-
if (whileTries === 0) {
98-
whileLoop = false;
99-
endTime = Date.now();
100-
specSummary.exitCode = config.networkErrorExitCode;
101-
return whilstCallback({ status: 504, message: "Tries limit reached" }); //Gateway Timeout
102-
} else {
103-
n = 2
104-
return setTimeout(whilstCallback, timeout * n, null);
105-
}
95+
whileTries -= 1;
96+
if (whileTries === 0) {
97+
whileLoop = false;
98+
endTime = Date.now();
99+
specSummary.exitCode = config.networkErrorExitCode;
100+
return whilstCallback({ status: 504, message: "Tries limit reached" }); //Gateway Timeout
106101
} else {
107-
return whilstCallback(error);
102+
n = 2
103+
return setTimeout(whilstCallback, timeout * n, null);
108104
}
109105
}
110106

bin/helpers/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ exports.handleSyncExit = (exitCode, dashboard_url) => {
333333
}
334334

335335
exports.getNetworkErrorMessage = (dashboard_url) => {
336-
let message = `fatal: unable to access '${config.buildUrl}': Could not resolve host: ${config.rails_host}` + '\n'
337-
+ `Max retries exceeded trying to connect to the host (retries: ${config.retries})` + '\n'
338-
+ `Please check the build status at: ${dashboard_url}`
336+
let message = Constants.userMessages.FATAL_NETWORK_ERROR + '\n'
337+
+ Constants.userMessages.RETRY_LIMIT_EXCEEDED + '\n'
338+
+ Constants.userMessages.CHECK_DASHBOARD_AT + dashboard_url
339339
return chalk.red(message)
340340
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,18 @@ describe("failedSpecsDetails", () => {
5959
});
6060
});
6161
});
62+
63+
context("failed because of network issue", () => {
64+
let data = {
65+
specs: [ {specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'},
66+
{specName: 'spec2.name.js', status: 'Passed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'}],
67+
exitCode: 2
68+
};
69+
70+
it("returns 2 exit code", () => {
71+
return specDetails.failedSpecsDetails(data).then((result) => {
72+
expect(result).to.equal(data);
73+
});
74+
});
75+
});
6276
});

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ describe("printSpecsRunSummary", () => {
1818
});
1919
});
2020

21+
context("request failure", () => {
22+
let data = { specs: [], duration: 6000, exitCode: 2}, machines = 2;
23+
it('returns passed specs data with proper exit code', () => {
24+
return specSummary.printSpecsRunSummary(data, machines).then((specsData) => {
25+
expect(data.exitCode).to.equal(specsData);
26+
});
27+
});
28+
});
29+
2130
context("with data", () => {
2231
let time = 6000,
2332
machines = 2,

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

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -259,73 +259,53 @@ describe("syncSpecsLogs", () => {
259259
context("whileProcess", () => {
260260
const whileProcess = syncSpecsLogs.__get__("whileProcess");
261261

262-
context('network issue', () => {
263-
it('Should retry when error is because of network issue', () => {
264-
let delayed_n = 2, timeout = 3000, n = 1;
265-
let error = new Error("error");
266-
error.code = "ETIMEDOUT";
267-
268-
let requestStub = sandbox.stub();
269-
270-
let postStub = sandbox
271-
.stub(request, "post")
272-
.yields(error, { statusCode: 502 }, JSON.stringify({}));
273-
274-
requestStub.post = postStub;
275-
276-
let setTimeout = sandbox.stub();
277-
syncSpecsLogs.__set__('setTimeout', setTimeout);
278-
syncSpecsLogs.__set__('n', n);
279-
syncSpecsLogs.__set__('timeout', timeout);
280-
syncSpecsLogs.__set__('request', requestStub);
281-
syncSpecsLogs.__set__('whileTries', 5);
282-
283-
let whilstCallback = sandbox.stub();
284-
whileProcess(whilstCallback);
285-
286-
sinon.assert.calledWith(setTimeout, whilstCallback, timeout * delayed_n, null);
287-
expect(syncSpecsLogs.__get__("whileTries")).to.equal(4);
288-
});
262+
it('Should retry when request fails with error', () => {
263+
let delayed_n = 2, timeout = 3000, n = 1;
264+
let error = new Error("error");
289265

290-
it('Should give-up on retry when error is b because of network issue after 5 retries and set proper exit code', () => {
291-
let error = new Error("error"), requestStub = sandbox.stub();
292-
error.code = "ETIMEDOUT";
266+
let requestStub = sandbox.stub();
293267

294-
let postStub = sandbox
295-
.stub(request, "post")
296-
.yields(error, { statusCode: 502 }, JSON.stringify({}));
268+
let postStub = sandbox
269+
.stub(request, "post")
270+
.yields(error, { statusCode: 502 }, JSON.stringify({}));
297271

298-
requestStub.post = postStub;
272+
requestStub.post = postStub;
299273

300-
syncSpecsLogs.__set__('request', requestStub);
301-
syncSpecsLogs.__set__('whileTries', 1);
302-
syncSpecsLogs.__set__('specSummary', {});
303-
syncSpecsLogs.__set__('whileLoop', true);
274+
let setTimeout = sandbox.stub();
275+
syncSpecsLogs.__set__('setTimeout', setTimeout);
276+
syncSpecsLogs.__set__('n', n);
277+
syncSpecsLogs.__set__('timeout', timeout);
278+
syncSpecsLogs.__set__('request', requestStub);
279+
syncSpecsLogs.__set__('whileTries', 5);
304280

305-
let whilstCallback = sandbox.stub();
306-
whileProcess(whilstCallback);
281+
let whilstCallback = sandbox.stub();
282+
whileProcess(whilstCallback);
307283

308-
sinon.assert.calledWith(whilstCallback, { status: 504, message: "Tries limit reached" });
309-
expect(syncSpecsLogs.__get__("whileTries")).to.equal(0);
310-
expect(syncSpecsLogs.__get__("whileLoop")).to.equal(false);
311-
expect(syncSpecsLogs.__get__("specSummary.exitCode")).to.equal(2);
312-
});
284+
sinon.assert.calledWith(setTimeout, whilstCallback, timeout * delayed_n, null);
285+
expect(syncSpecsLogs.__get__("whileTries")).to.equal(4);
313286
});
314287

315-
it('Should break the loop if request has error other than network issue', () => {
316-
let error = new Error("error");
317-
let requestStub = sandbox.stub();
288+
it('Should exit after defined number of retries in case of error', () => {
289+
let error = new Error("error"), requestStub = sandbox.stub();
290+
318291
let postStub = sandbox
319292
.stub(request, "post")
320-
.yields(error, { statusCode: 200 }, JSON.stringify({}));
293+
.yields(error, { statusCode: 502 }, JSON.stringify({}));
294+
321295
requestStub.post = postStub;
322296

323297
syncSpecsLogs.__set__('request', requestStub);
298+
syncSpecsLogs.__set__('whileTries', 1);
299+
syncSpecsLogs.__set__('specSummary', {});
300+
syncSpecsLogs.__set__('whileLoop', true);
324301

325302
let whilstCallback = sandbox.stub();
326303
whileProcess(whilstCallback);
327304

328-
sinon.assert.calledWith(whilstCallback, error);
305+
sinon.assert.calledWith(whilstCallback, { status: 504, message: "Tries limit reached" });
306+
expect(syncSpecsLogs.__get__("whileTries")).to.equal(0);
307+
expect(syncSpecsLogs.__get__("whileLoop")).to.equal(false);
308+
expect(syncSpecsLogs.__get__("specSummary.exitCode")).to.equal(2);
329309
});
330310

331311
it('Should print spec details when data is returned from server', () => {

test/unit/bin/helpers/utils.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const chai = require('chai'),
55
expect = chai.expect,
66
sinon = require('sinon'),
77
chaiAsPromised = require('chai-as-promised'),
8+
chalk = require('chalk'),
89
fs = require('fs');
910

1011
const utils = require('../../../../bin/helpers/utils'),
1112
constant = require('../../../../bin/helpers/constants'),
1213
logger = require('../../../../bin/helpers/logger').winstonLogger,
13-
testObjects = require('../../support/fixtures/testObjects');
14+
testObjects = require('../../support/fixtures/testObjects'),
15+
syncLogger = require("../../../../bin/helpers/logger").syncCliLogger;
1416

1517
chai.use(chaiAsPromised);
1618
logger.transports['console.info'].silent = true;
@@ -943,4 +945,42 @@ describe('utils', () => {
943945
});
944946

945947
});
948+
949+
describe('#handleSyncExit', () => {
950+
let processStub;
951+
beforeEach(function () {
952+
processStub = sinon.stub(process, 'exit');
953+
});
954+
955+
afterEach(function () {
956+
processStub.restore();
957+
});
958+
it('should print network error message when exit code is set to network error code', () => {
959+
let dashboard_url = "dashboard_url", exitCode = 2;
960+
let getNetworkErrorMessageStub = sinon.stub(utils, 'getNetworkErrorMessage');
961+
utils.handleSyncExit(exitCode, dashboard_url);
962+
sinon.assert.calledOnce(getNetworkErrorMessageStub);
963+
sinon.assert.calledOnceWithExactly(processStub, exitCode);
964+
getNetworkErrorMessageStub.restore();
965+
});
966+
967+
it('should print dashboard link when exit code is not network error code', () => {
968+
let dashboard_url = "dashboard_url", exitCode = 1;
969+
let syncCliLoggerStub = sinon.stub(syncLogger, 'info');
970+
utils.handleSyncExit(exitCode, dashboard_url);
971+
sinon.assert.calledTwice(syncCliLoggerStub);
972+
sinon.assert.calledOnceWithExactly(processStub, exitCode);
973+
});
974+
});
975+
976+
describe('#getNetworkErrorMessage', () => {
977+
it('should return the error message in red color', () => {
978+
let dashboard_url = "dashboard_url";
979+
let message = constant.userMessages.FATAL_NETWORK_ERROR + '\n'
980+
+ constant.userMessages.RETRY_LIMIT_EXCEEDED + '\n'
981+
+ constant.userMessages.CHECK_DASHBOARD_AT + dashboard_url
982+
utils.getNetworkErrorMessage(dashboard_url);
983+
expect(utils.getNetworkErrorMessage(dashboard_url)).to.eq(chalk.red(message))
984+
});
985+
});
946986
});

0 commit comments

Comments
 (0)