Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #579 from gemini-testing/fix-abort-session
Browse files Browse the repository at this point in the history
Fix abort session
  • Loading branch information
rostik404 authored Sep 1, 2016
2 parents 84dc267 + ab3c0d1 commit b970f4e
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 201 deletions.
32 changes: 18 additions & 14 deletions lib/runner/browser-runner/browser-agent.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
'use strict';

var inherit = require('inherit');
module.exports = class BrowserAgent {
static create(browserId, pool) {
return new BrowserAgent(browserId, pool);
}

var BrowserAgent = inherit({
__constructor: function(browserId, pool) {
constructor(browserId, pool) {
this.browserId = browserId;
this._pool = pool;
},
this._force = false;
}

invalidateSession() {
this._force = true;
}

getBrowser: function() {
getBrowser() {
return this._pool.getBrowser(this.browserId);
},
}

freeBrowser(browser, opts) {
opts = opts || {};
opts.force = this._force;

freeBrowser: function(browser, opts) {
return this._pool.freeBrowser(browser, opts);
}
}, {
create: function(browserId, pool) {
return new BrowserAgent(browserId, pool);
}
});

module.exports = BrowserAgent;
};
22 changes: 19 additions & 3 deletions lib/runner/suite-runner/regular-suite-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SuiteRunner = require('./suite-runner');
const StateRunner = require('../state-runner');

const cloneError = require('../../errors/utils').cloneError;
const NoRefImageError = require('../../errors/no-ref-image-error');

module.exports = class RegularSuiteRunner extends SuiteRunner {
constructor(suite, browserAgent, config) {
Expand All @@ -25,7 +26,11 @@ module.exports = class RegularSuiteRunner extends SuiteRunner {
return this._browserAgent.getBrowser()
.then((browser) => this._initSession(browser))
.then(() => this._processStates(stateProcessor))
.catch((e) => this._passErrorToStates(e))
.catch((e) => {
this._validateSession(e);

return this._passErrorToStates(e);
})
.fin(() => this._session && this._browserAgent.freeBrowser(this._session.browser));
}

Expand Down Expand Up @@ -87,6 +92,12 @@ module.exports = class RegularSuiteRunner extends SuiteRunner {
return promiseUtils.seqMap(this._suite.states, (state) => this._runStateInSession(state, stateProcessor));
}

_validateSession(error) {
if (!(error instanceof NoRefImageError)) {
this._browserAgent.invalidateSession();
}
}

_runStateInSession(state, stateProcessor) {
const runner = StateRunner.create(state, this._session, this._config);

Expand All @@ -97,10 +108,15 @@ module.exports = class RegularSuiteRunner extends SuiteRunner {
RunnerEvents.TEST_RESULT,
RunnerEvents.CAPTURE,
RunnerEvents.UPDATE_RESULT,
RunnerEvents.WARNING,
RunnerEvents.ERROR
RunnerEvents.WARNING
]);

runner.on(RunnerEvents.ERROR, (e) => {
this._validateSession(e);

this.emit(RunnerEvents.ERROR, e);
});

return runner.run(stateProcessor);
}
};
17 changes: 17 additions & 0 deletions test/unit/runner/browser-runner/browser-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ describe('runner/browser-runner/browser-agent', function() {

assert.calledWith(browserPool.freeBrowser, browser);
});

it('should free browser unconditionally if session was invalidated', () => {
const browserAgent = BrowserAgent.create('browser', browserPool);

browserAgent.invalidateSession();
browserAgent.freeBrowser();

assert.calledWith(browserPool.freeBrowser, sinon.match.any, {force: true});
});

it('should free browser if session was not invalidated', () => {
const browserAgent = BrowserAgent.create('browser', browserPool);

browserAgent.freeBrowser();

assert.calledWith(browserPool.freeBrowser, sinon.match.any, {force: false});
});
});
Loading

0 comments on commit b970f4e

Please sign in to comment.