Skip to content

Commit

Permalink
fix(test): move RedisClient to mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Mar 28, 2015
1 parent 79e3731 commit e0b24e0
Showing 1 changed file with 43 additions and 67 deletions.
110 changes: 43 additions & 67 deletions lib/RedisClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var RedisClient = require('../lib/RedisClient');
var _ = require('lodash');
var sinon = require('sinon');
var Socket = require('../test/helpers/Socket');
var t = require('chai').assert;

function tcreateConnection(t, host, hostname) {
return function (_host, _hostname, cb) {
Expand All @@ -23,79 +24,61 @@ function stubRedsminEndpoint(fn) {
// Quiet console output
RedisClient.log = sinon.stub(_.clone(console));

var R = null;
var endpoint = null;
var HOST = '127.0.1.1';
var PORT = 6378;

exports['RedisClient'] = {
setUp: function (done) {
// setup here
describe('RedisClient', function () {
var R, endpoint;
var HOST = '127.0.1.1';
var PORT = 6378;

beforeEach(function () {
endpoint = stubRedsminEndpoint();
R = new RedisClient(endpoint);
});

afterEach(function (done) {
done();
},
tearDown: function (callback) {
// clean up
//RedisClient.log = console;
callback();
},

global: function (t) {
t.expect(2);
});

it('should global', function (done) {
t.equal(typeof RedisClient, 'function', 'should be a function.');
t.equal(typeof RedisClient.net, 'object', '.net should be a object.');
t.done();
},

constructor: function (t) {
t.done();
},
done();
});

connect: function (t) {
t.expect(5);
it('should constructor', function (done) {
done();
});

it('should connect', function (done) {
RedisClient.net.createConnection = tcreateConnection(t, PORT, HOST);

R.on('connect', function () {
t.ok(true, 'connected');
t.equal(R.connected, true);
t.done();
done();
});

t.equal(R.connected, false);
R.connect('redis://' + HOST + ':' + PORT);
},
});

'updatePortAndHostname with a connection string without redis://': function (t) {
it('should updatePortAndHostname', function (done) {
R.updatePortAndHostname(HOST + ':' + PORT);
t.equal(R.port, PORT);
t.equal(R.hostname, HOST);
t.done();
},

/**
* onData from redis
* @param {[type]} t [description]
* @return {[type]} [description]
*/
'onData': function (t) {
t.expect(1);
done();
});

it('should forward data', function (done) {
R = new RedisClient(stubRedsminEndpoint(function fnWrite(data) {
t.equal(data, 'test data');
t.done();
done();
}));

R.onData('test data');
},

/**
* write data to redis
*/
'write': function (t) {
t.expect(1);
});

it('should write data to redis', function (done) {
RedisClient.net.createConnection = tcreateConnection();

R.connect('redis://127.0.0.1:6378');
Expand All @@ -105,29 +88,25 @@ exports['RedisClient'] = {
R.write('KEYS *');

t.ok(spy.calledWith('KEYS *'), "write to redis");
t.done();
},

'onClose': function (t) {
t.expect(1);
done();
});

it('should not be in connected state if the redis connection is closed', function (done) {
RedisClient.net.createConnection = tcreateConnection();

R.on('close', function () {
t.equal(R.connected, false);
t.done();
done();
});

R.on('connect', _.once(function () {
R.onClose(new Error('close'));
}));

R.connect('redis://127.0.0.1:6378');
},

'onClose (reconnect)': function (t) {
t.expect(2);
});

it('should try to connect to redis with backoff', function (done) {
RedisClient.net.createConnection = tcreateConnection();

R = new RedisClient(stubRedsminEndpoint(), {
Expand All @@ -138,42 +117,39 @@ exports['RedisClient'] = {
var spy = sinon.spy(R, "onConnected");

R.on('connect', function () {

t.ok(true, "connect called");

if (spy.callCount === 2) {
R._connect = function () {};
t.done();
done();
return;
}

process.nextTick(_.bind(R.onClose, R, new Error('close')));
});

R.connect('redis://127.0.0.1:6378');
},
});

'Reconnect': function (t) {
t.expect(1);
it('should use the backoff', function (done) {
// Simulate that the backoff will always directly hit reconnect
R.backoff.backoff = function () {
R.reconnect();
};

R._connect = function () {
t.equal(R.connected, false);
t.done();
done();
};

R.onClose();
},
});

'onError': function (t) {
it('should not throw when onError is called', function (done) {
var R = new RedisClient(stubRedsminEndpoint());
t.expect(1);
t.doesNotThrow(function () {
R.onError();
});
t.done();
}
};
done();
});
});

0 comments on commit e0b24e0

Please sign in to comment.