Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use bluebird instead q and qemitter #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var inherit = require('inherit'),
q = require('q'),
Promise = require('bluebird'),
childProcess = require('child_process'),
util = require('util');

Expand All @@ -11,6 +11,20 @@ var DEFAULTS = {
SSH_PORT: 22
};

function defer() {
var resolve, reject;
var promise = new Promise(function () {
resolve = arguments[0];
reject = arguments[1];
});

return {
resolve: resolve,
reject: reject,
promise: promise
};
}

var Tunnel = inherit({
/**
* Constuctor
Expand All @@ -33,8 +47,8 @@ var Tunnel = inherit({
this._localPort = opts.localport;
this._connectTimeout = opts.connectTimeout || DEFAULTS.CONNECT_TIMEOUT;
this._tunnel = null;
this._tunnelDeferred = q.defer();
this._closeDeferred = q.defer();
this._tunnelDeferred = defer();
this._closeDeferred = defer();
},

/**
Expand Down Expand Up @@ -85,13 +99,13 @@ var Tunnel = inherit({
*/
close: function () {
if (!this._tunnel) {
return q();
return Promise.resolve();
}

var _this = this;

this._tunnel.kill('SIGTERM');
return this._closeDeferred.promise.timeout(3000).fail(function () {
return this._closeDeferred.promise.timeout(3000).catch(function () {
_this._tunnel.kill('SIGKILL');
return _this._closeTunnel(-1);
});
Expand Down Expand Up @@ -144,16 +158,16 @@ Tunnel.openWithRetries = function (opts, retries) {

function retry_(retriesLeft) {
if (!retriesLeft) {
return q.reject(util.format('ERROR: failed to create tunnel after %d attempts', retries));
return Promise.reject(util.format('ERROR: failed to create tunnel after %d attempts', retries));
}

var tunnel = new Tunnel(opts);

return tunnel.open()
.then(function () {
return q.resolve(tunnel);
return Promise.resolve(tunnel);
})
.fail(function () {
.catch(function () {
return tunnel.close()
.then(retry_.bind(null, retriesLeft - 1));
});
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"unit-test": "istanbul test _mocha --recursive test"
},
"dependencies": {
"inherit": "^2.2.2",
"q": "^1.4.1"
"bluebird": "^3.5.0",
"inherit": "^2.2.2"
},
"devDependencies": {
"chai": "^3.3.0",
Expand All @@ -50,7 +50,6 @@
"jscs": "^2.1.1",
"jshint": "^2.8.0",
"mocha": "^2.3.3",
"qemitter": "^1.0.0",
"sinon": "^1.16.1",
"sinon-chai": "^2.8.0",
"lodash": "^3.10.1"
Expand Down
28 changes: 14 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var Tunnel = require('../'),
_ = require('lodash'),
q = require('q'),
Promise = require('bluebird'),
childProcess = require('child_process'),
events = require('events'),
util = require('util');
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('Tunnel', function () {

it('should set default timeout as 10 seconds', function () {
var tunnel = createTunnel(),
timeout = sandbox.stub(q.makePromise.prototype, 'timeout'); // promise constructor available like this?!
timeout = sandbox.stub(Promise.prototype, 'timeout');

tunnel.open();

Expand All @@ -59,7 +59,7 @@ describe('Tunnel', function () {

var result = tunnel.open();

expect(q.isPromise(result)).to.be.true;
expect(result).to.be.an.instanceof(Promise);
});

describe('tunnel spawn and events', function () {
Expand Down Expand Up @@ -215,7 +215,7 @@ describe('Tunnel', function () {
tunnel = createTunnel();
tunnel.open();

expect(q.isPromise(tunnel.close())).to.be.true;
expect(tunnel.close()).to.be.an.instanceof(Promise);
});

it('should try to kill tunnel using SIGTERM', function () {
Expand Down Expand Up @@ -252,14 +252,14 @@ describe('Tunnel', function () {
describe('openWithRetries', function () {
beforeEach(function () {
sandbox.stub(Tunnel.prototype);
Tunnel.prototype.open.returns(q());
Tunnel.prototype.close.returns(q());
Tunnel.prototype.open.returns(Promise.resolve());
Tunnel.prototype.close.returns(Promise.resolve());
});

it('should return promise', function () {
var result = Tunnel.openWithRetries(defaultOpts());

expect(q.isPromise(result)).to.be.true;
expect(result).to.be.an.instanceof(Promise);
});

it('should try to open tunnel with passed opts', function () {
Expand All @@ -271,37 +271,37 @@ describe('Tunnel', function () {
});

it('should resolve promise if tunnel opened successfully', function () {
Tunnel.prototype.open.returns(q());
Tunnel.prototype.open.returns(Promise.resolve());

return expect(Tunnel.openWithRetries(defaultOpts())).to.be.eventually.resolved;
});

it('should resolve promise with tunnel instance', function () {
Tunnel.prototype.open.returns(q());
Tunnel.prototype.open.returns(Promise.resolve());

return Tunnel.openWithRetries(defaultOpts()).then(function (tunnel) {
expect(tunnel).to.be.instanceOf(Tunnel);
});
});

it('should reject tunnel if failed to open tunnel after retries', function () {
Tunnel.prototype.open.returns(q.reject());
Tunnel.prototype.open.returns(Promise.reject());

return expect(Tunnel.openWithRetries(defaultOpts)).to.be.eventually.rejected;
});

it('should retry to create tunnel 5 times by default', function () {
Tunnel.prototype.open.returns(q.reject());
Tunnel.prototype.open.returns(Promise.reject());

return Tunnel.openWithRetries(defaultOpts()).fail(function () {
return Tunnel.openWithRetries(defaultOpts()).catch(function () {
expect(Tunnel.prototype.open.callCount).to.be.equal(5);
});
});

it('should retry create tunnel retries times', function () {
Tunnel.prototype.open.returns(q.reject());
Tunnel.prototype.open.returns(Promise.reject());

return Tunnel.openWithRetries(defaultOpts(), 10).fail(function () {
return Tunnel.openWithRetries(defaultOpts(), 10).catch(function () {
expect(Tunnel.prototype.open.callCount).to.be.equal(10);
});
});
Expand Down