Skip to content

Commit

Permalink
fix and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev committed Sep 4, 2018
1 parent cbb711f commit 39e7cf7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
37 changes: 33 additions & 4 deletions src/browser/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export default class BrowserConnection extends EventEmitter {
constructor (gateway, browserInfo, permanent) {
super();

this.HEARTBEAT_TIMEOUT = 2 * 60 * 1000;
this.HEARTBEAT_TIMEOUT = 2 * 60 * 1000;
this.BROWSER_RESTART_TIMEOUT = 60 * 1000;

this.id = BrowserConnection._generateId();
this.jobQueue = [];
Expand Down Expand Up @@ -110,9 +111,13 @@ export default class BrowserConnection extends EventEmitter {
}
}

_createBrowserDisconnectedError () {
return new GeneralError(MESSAGE.browserDisconnected, this.userAgent);
}

_waitForHeartbeat () {
this.heartbeatTimeout = setTimeout(() => {
const err = new GeneralError(MESSAGE.browserDisconnected, this.userAgent);
const err = this._createBrowserDisconnectedError();

this.opened = false;
this.errorSupressed = false;
Expand Down Expand Up @@ -149,8 +154,32 @@ export default class BrowserConnection extends EventEmitter {

this._forceIdle();

await this._closeBrowser();
await this._runBrowser();
let resolveTimeout = null;
let onTimeout = false;
let timeout = null;

const restartPromise = this._closeBrowser()
.then(() => this._runBrowser());

const timeoutPromise = new Promise(resolve => {
resolveTimeout = resolve;

timeout = setTimeout(() => {
onTimeout = true;

resolve();
}, this.BROWSER_RESTART_TIMEOUT);
});

Promise.race([ restartPromise, timeoutPromise ])
.then(() => {
clearTimeout(timeout);

if (onTimeout)
this.emit('error', this._createBrowserDisconnectedError());
else
resolveTimeout();
});
}

supressError () {
Expand Down
2 changes: 1 addition & 1 deletion src/runner/test-run-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class TestRunController extends EventEmitter {

testRun.once('start', () => this.emit('test-run-start'));
testRun.once('done', () => this._testRunDone());
testRun.once('disconnected', () => this._testRunDisconnected(connection, testRun));
testRun.once('disconnected', () => this._testRunDisconnected(connection));

testRun.start();

Expand Down
18 changes: 6 additions & 12 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ export default class TestRun extends EventEmitter {
await this._runAfterHook();
}

if (!this.connected)
if (this.disconnected)
return;

if (this.errs.length && this.debugOnFail)
await this._enqueueSetBreakpointCommand(null, this.debugReporterPluginHost.formatError(this.errs[0]));

await this.executeTestDoneCommand();
await this.executeCommand(new TestDoneCommand());

this._addPendingPageErrorIfAny();

Expand Down Expand Up @@ -367,10 +367,6 @@ export default class TestRun extends EventEmitter {
this.browserManipulationQueue.removeAllNonServiceManipulations();
}

get connected () {
return this.browserConnection.opened;
}

// Current driver task
get currentDriverTask () {
return this.driverTaskQueue[0];
Expand Down Expand Up @@ -434,8 +430,8 @@ export default class TestRun extends EventEmitter {
const pageError = this.pendingPageError || driverStatus.pageError;
const currentTaskRejectedByError = pageError && this._handlePageErrorStatus(pageError);

if (!this.connected)
return null;
if (this.disconnected)
return new Promise((_, reject) => reject());

this.consoleMessages.concat(driverStatus.consoleMessages);

Expand Down Expand Up @@ -682,16 +678,14 @@ export default class TestRun extends EventEmitter {
}

disconnect (err) {
this.disconnected = true;

this._rejectCurrentDriverTask(err);

this.emit('disconnected', err);

delete testRunTracker.activeTestRuns[this.session.id];
}

async executeTestDoneCommand () {
await this.executeCommand(new TestDoneCommand());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function customReporter () {
function createConnection (browser) {
return browserProviderPool
.getBrowserInfo(browser)
.then(browserInfo => new BrowserConnection(testCafe.browserConnectionGateway, browserInfo, true));
.then(browserInfo => new BrowserConnection(testCafe.browserConnectionGateway, browserInfo, false));
}

async function run (pathToTest, filter) {
Expand Down Expand Up @@ -59,16 +59,6 @@ describe('Browser reconnect', function () {
});
});

it('Should fail on 3 disconnects', function () {
return run('./testcafe-fixtures/index-test.js', 'Should fail on 3 disconnects')
.then(() => {
throw new Error('Test should have failed but it succeeded');
})
.catch(err => {
expect(err.message).contains('browser disconnected. This problem may appear when a browser hangs or is closed, or due to network issues');
});
});

it('Should fail on 3 disconnects in one browser', function () {
return run('./testcafe-fixtures/index-test.js', 'Should fail on 3 disconnects in one browser')
.then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ test('Should restart browser when it does not respond', async t => {
await t.expect(counter[userAgent]).eql(3);
});

test('Should fail on 3 disconnects', async t => {
const userAgent = await getUserAgent();

counter[userAgent] = counter[userAgent] || 0;

counter[userAgent]++;

await t.expect(counter[userAgent]).lte(3);

await hang();

throw new Error('browser has not restarted');
});

test('Should fail on 3 disconnects in one browser', async t => {
const userAgent = await getUserAgent();

Expand Down

0 comments on commit 39e7cf7

Please sign in to comment.