Skip to content

Commit

Permalink
Merge pull request #225 from gemini-testing/fix/worker.mem.leaks2
Browse files Browse the repository at this point in the history
fix: drop webdriverio command history after each test in workers
  • Loading branch information
j0tunn authored Feb 9, 2018
2 parents b243f5f + 51c4daf commit a512aff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
3 changes: 3 additions & 0 deletions lib/browser/existing-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ module.exports = class ExistingBrowser extends Browser {

quit() {
this.sessionId = null;
this._session.commandList.splice(0);
this._meta = _.extend({}, this._config.meta);
this._changes = {originWindowSize: null};
}

open(url) {
Expand Down
58 changes: 30 additions & 28 deletions lib/worker/runner/mocha-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ module.exports = class MochaAdapter extends EventEmitter {

this.tests = [];
this._ctx = {};
this._fail = null;
this._errMonitor = new EventEmitter();

this._replaceTimeouts();
this._injectBrowser();
this._injectSkip();
this._linkContexts();
this._extendTestApi();
Expand All @@ -77,24 +75,6 @@ module.exports = class MochaAdapter extends EventEmitter {
});
}

_injectBrowser() {
this.suite.beforeEach(() => this._requestBrowser());
this.suite.afterEach(() => this._freeBrowser());
}

_requestBrowser() {
return this._browserAgent.getBrowser(this._sessionId)
.then((browser) => {
this._browser = browser;

this.suite.ctx.browser = this._browser.publicAPI;
});
}

_freeBrowser() {
this._browser && this._browserAgent.freeBrowser(this._browser);
}

_injectSkip() {
const skip = new Skip();
const skipBuilder = new SkipBuilder(skip, this._browserAgent.browserId);
Expand All @@ -119,10 +99,13 @@ module.exports = class MochaAdapter extends EventEmitter {
return function() {
runnable.hermioneCtx = _this._ctx;
const browser = _this.suite.ctx.browser;
if (browser) {
Object.getPrototypeOf(browser).executionContext = _.extend(runnable, {browserId});
}
return baseFn.apply(this, arguments);
const setExecutionContext = browser
? (context) => Object.getPrototypeOf(browser).executionContext = context
: () => {};

setExecutionContext(_.extend(runnable, {browserId}));
return baseFn.apply(this, arguments)
.finally(() => setExecutionContext(null));
};
}));
}
Expand Down Expand Up @@ -199,20 +182,39 @@ module.exports = class MochaAdapter extends EventEmitter {
}

runInSession(sessionId) {
this._sessionId = sessionId;
return this._requestBrowser(sessionId)
.then(() => this._run())
.finally(() => this._freeBrowser());
}

_requestBrowser(sessionId) {
return this._browserAgent.getBrowser(sessionId)
.then((browser) => {
this._browser = browser;

this.suite.ctx.browser = this._browser.publicAPI;
});
}

_freeBrowser() {
this._browser && this._browserAgent.freeBrowser(this._browser);
}

_run() {
const defer = q.defer();

this.on(RunnerEvents.ERROR, (err) => this._fail = this._fail || err);
this.on(RunnerEvents.TEST_FAIL, (data) => this._fail = data.err);
let fail = null;

this.on(RunnerEvents.ERROR, (err) => fail = fail || err);
this.on(RunnerEvents.TEST_FAIL, (data) => fail = data.err);
this._errMonitor.on('err', (err) => defer.reject(err));

this._mocha.run(() => {
const browser = this._browser || {};
const {meta, changes} = browser;
const data = {hermioneCtx: this._ctx, meta, changes};

return this._fail ? defer.reject(_.extend(this._fail, data)) : defer.resolve(data);
return fail ? defer.reject(_.extend(fail, data)) : defer.resolve(data);
});

return defer.promise;
Expand Down
4 changes: 1 addition & 3 deletions test/lib/worker/runner/mocha-adapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const q = require('q');
const proxyquire = require('proxyquire');
const BrowserAgent = require('gemini-core').BrowserAgent;
const RunnerEvents = require('lib/constants/runner-events');
Expand All @@ -19,8 +18,7 @@ describe('worker/mocha-adapter', () => {

const mkMochaAdapter_ = (config) => {
const browserAgent = sinon.createStubInstance(BrowserAgent);
browserAgent.getBrowser.returns(mkBrowserStub_());
browserAgent.freeBrowser.returns(q());
browserAgent.getBrowser.resolves(mkBrowserStub_());

return MochaAdapter.create(browserAgent, _.extend({patternsOnReject: []}, config));
};
Expand Down

0 comments on commit a512aff

Please sign in to comment.