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 #616 from gemini-testing/feature/proper-bluebird
Browse files Browse the repository at this point in the history
Replace Q with Bluebird
  • Loading branch information
sipayRT committed Oct 6, 2016
2 parents 87fa8df + 92e9e37 commit cb36c7e
Show file tree
Hide file tree
Showing 69 changed files with 524 additions and 511 deletions.
11 changes: 5 additions & 6 deletions lib/browser-pool/basic-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var util = require('util'),
Pool = require('./pool'),
signalHandler = require('../signal-handler'),
_ = require('lodash'),
Q = require('q');
Promise = require('bluebird');

var activeSessions = {};

Expand Down Expand Up @@ -32,12 +32,11 @@ Pool.prototype.getBrowser = function(id) {
};

return launchPromise
.then(browser.reset.bind(browser))
.thenResolve(browser)
.then(browser.reset.bind(browser)).thenReturn(browser)
.catch(function(e) {
return _this.freeBrowser(browser)
.then(function() {
return Q.reject(e);
return Promise.reject(e);
});
});
};
Expand All @@ -52,9 +51,9 @@ signalHandler.on('exit', function() {
return _(activeSessions)
.map(function(session) {
var quit_ = session.browser.quit.bind(session.browser);
return Q.when(session.launchPromise, quit_);
return session.launchPromise.then(quit_);
})
.thru(Q.all)
.thru(Promise.all)
.value();
});

Expand Down
7 changes: 3 additions & 4 deletions lib/browser-pool/caching-pool.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var q = require('q'),
var Promise = require('bluebird'),
util = require('util'),
Pool = require('./pool'),
LimitedUseSet = require('./limited-use-set'),
Expand Down Expand Up @@ -38,11 +38,10 @@ CachingPool.prototype.getBrowser = function(id) {
log('has cached browser %o', browser);
return browser.reset()
.catch(function(e) {
var reject = q.reject.bind(null, e);
var reject = Promise.reject.bind(null, e);
return this.underlyingPool.freeBrowser(browser)
.then(reject, reject);
}.bind(this))
.thenResolve(browser);
}.bind(this)).thenReturn(browser);
};

/**
Expand Down
26 changes: 12 additions & 14 deletions lib/browser-pool/limited-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Pool = require('./pool');
const CancelledError = require('../errors/cancelled-error');
const q = require('q');
const Promise = require('bluebird');
const log = require('debug')('gemini:pool:limited');

module.exports = class LimitedPool extends Pool {
Expand All @@ -22,7 +22,7 @@ module.exports = class LimitedPool extends Pool {
this._limit = limit;
this._launched = 0;
this._requests = 0;
this._deferQueue = [];
this._requestQueue = [];
}

getBrowser(id) {
Expand All @@ -32,7 +32,7 @@ module.exports = class LimitedPool extends Pool {
return this._getBrowser(id)
.catch((e) => {
--this._requests;
return q.reject(e);
return Promise.reject(e);
});
}

Expand All @@ -47,9 +47,9 @@ module.exports = class LimitedPool extends Pool {

cancel() {
log('cancel');
this._deferQueue.forEach((entry) => entry.defer.reject(new CancelledError()));
this._requestQueue.forEach((entry) => entry.reject(new CancelledError()));

this._deferQueue.length = 0;
this._requestQueue.length = 0;
this.underlyingPool.cancel();
}

Expand All @@ -61,11 +61,9 @@ module.exports = class LimitedPool extends Pool {
}

log('queuing the request');
const defer = q.defer();
this._deferQueue.unshift({id, defer});

log(`queue length: ${this._deferQueue.length}`);
return defer.promise;
return new Promise((resolve, reject) => {
this._requestQueue.unshift({id, resolve, reject});
});
}

/**
Expand All @@ -77,17 +75,17 @@ module.exports = class LimitedPool extends Pool {
return this.underlyingPool.getBrowser(id)
.catch((e) => {
this._launchNextBrowser();
return q.reject(e);
return Promise.reject(e);
});
}

_launchNextBrowser() {
var queued = this._deferQueue.pop();
var queued = this._requestQueue.pop();
if (queued) {
log('has queued requests');
log(`remaining queue length: ${this._deferQueue.length}`);
log(`remaining queue length: ${this._requestQueue.length}`);
this._newBrowser(queued.id)
.then(queued.defer.resolve, queued.defer.reject);
.then(queued.resolve, queued.reject);
} else {
this._launched--;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/browser-pool/limited-use-set.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var q = require('q'),
var Promise = require('bluebird'),
log = require('debug')('gemini:pool:limited-use-set');

/**
Expand Down Expand Up @@ -28,7 +28,7 @@ LimitedUseSet.prototype.push = function(value) {
}
log('under limit');
this._objects.push(value);
return q();
return Promise.resolve();
};

LimitedUseSet.prototype._isOverLimit = function(value) {
Expand Down
4 changes: 2 additions & 2 deletions lib/browser-pool/pool.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var q = require('q');
var Promise = require('bluebird');
/**
* @constructor
*/
Expand All @@ -17,7 +17,7 @@ BasicPool.prototype.getBrowser = function() {
* @returns {Promise}
*/
BasicPool.prototype.freeBrowser = function() {
return q.resolve();
return Promise.resolve();
};

BasicPool.prototype.cancel = function() {
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const _ = require('lodash');
const Promise = require('bluebird');
const wd = require('wd');

const Camera = require('./camera');
Expand Down Expand Up @@ -44,6 +45,6 @@ module.exports = class Browser {
}

_configureHttp(timeout) {
return this._wd.configureHttp({retries: 'never', timeout});
return Promise.resolve(this._wd.configureHttp({retries: 'never', timeout}));
}
};
4 changes: 2 additions & 2 deletions lib/browser/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var Image = require('../image'),
inherit = require('inherit'),
q = require('q'),
Promise = require('bluebird'),
_ = require('lodash'),
util = require('./util');

Expand All @@ -25,7 +25,7 @@ module.exports = inherit({
// if _takeScreenshotWithNativeContext fails too, the original error
// most likely was not related to the different Appium contexts and
// it is more useful to report it instead of second one
return q.reject(originalError);
return Promise.reject(originalError);
});
});
},
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/client-bridge.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var util = require('util'),
q = require('q'),
Promise = require('bluebird'),

StateError = require('../errors/state-error'),

Expand Down Expand Up @@ -35,11 +35,11 @@ ClientBridge.prototype._callCommand = function(command, injectAllowed) {
return this._browser.evalScript(command)
.then(function(result) {
if (!result || !result.error) {
return q.resolve(result);
return Promise.resolve(result);
}

if (result.error !== NO_CLIENT_FUNC) {
return q.reject(new StateError(result.message));
return Promise.reject(new StateError(result.message));
}

if (injectAllowed) {
Expand All @@ -48,7 +48,7 @@ ClientBridge.prototype._callCommand = function(command, injectAllowed) {
return _this._callCommand(command, false);
});
}
return q.reject(new StateError('Unable to inject gemini client script'));
return Promise.reject(new StateError('Unable to inject gemini client script'));
});
};

Expand Down
4 changes: 2 additions & 2 deletions lib/browser/existing-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Browser = require('./browser');
const Browser = require('./browser');

module.exports = class ExistingBrowser extends Browser {
static fromObject(serializedObject) {
Expand All @@ -22,6 +22,6 @@ module.exports = class ExistingBrowser extends Browser {
attach() {
return this._setHttpTimeout()
.then(() => this._wd.attach(this.sessionId))
.thenResolve(this);
.thenReturn(this);
}
};
Loading

0 comments on commit cb36c7e

Please sign in to comment.