From 79e37314a0a0e3f0e1a11a8bcd01f311d41a0622 Mon Sep 17 00:00:00 2001 From: FG Ribreau Date: Sat, 28 Mar 2015 18:03:01 +0100 Subject: [PATCH] chore(test): move test along their implementations --- lib/Endpoint.js | 121 +++++++++-------- {test => lib}/Endpoint.test.js | 210 +++++++++++++++++------------- lib/Proxy.test.js | 23 ++++ lib/RedisClient.js | 87 +++++++------ {test => lib}/RedisClient.test.js | 86 ++++++------ test/Proxy.test.js | 22 ---- 6 files changed, 296 insertions(+), 253 deletions(-) rename {test => lib}/Endpoint.test.js (54%) create mode 100644 lib/Proxy.test.js rename {test => lib}/RedisClient.test.js (63%) delete mode 100644 test/Proxy.test.js diff --git a/lib/Endpoint.js b/lib/Endpoint.js index ca25251..d81ef92 100644 --- a/lib/Endpoint.js +++ b/lib/Endpoint.js @@ -1,11 +1,10 @@ -var env = require('./config').env -, log = require('./log') -, url = require('url') -, _ = require('lodash') -, fs = require('fs') -, Backoff = require('backoff') -, EventEmitter = require('events').EventEmitter -, jsonPackage = JSON.parse(fs.readFileSync(__dirname + '/../package.json')); +'use strict'; +var log = require('./log'); +var _ = require('lodash'); +var fs = require('fs'); +var Backoff = require('backoff'); +var EventEmitter = require('events').EventEmitter; +var jsonPackage = JSON.parse(fs.readFileSync(__dirname + '/../package.json')); /** * Endpoint (redsmin) @@ -13,77 +12,77 @@ var env = require('./config').env * @param {String} key Connection key * @param {Object} opts Optional parameters */ -function Endpoint(fnWrite, key, opts){ +function Endpoint(fnWrite, key, opts) { _.extend(this, EventEmitter); _.bindAll(this); opts = opts || {}; - if(!fnWrite || typeof fnWrite !== 'function'){ + if (!fnWrite || typeof fnWrite !== 'function') { throw new Error("Endpoint `fnWrite` parameter is not defined or is not a function"); } - if(!key){ + if (!key) { throw new Error("Endpoint `key` parameter is not defined"); } - this.uri = null; - this.key = key; - this.hostname = null; - this.port = null; - this.auth = opts.auth; + this.uri = null; + this.key = key; + this.hostname = null; + this.port = null; + this.auth = opts.auth; this.handshaken = false; - this.connected = false; - this.socket = null; - this.fnWrite = fnWrite; + this.connected = false; + this.socket = null; + this.fnWrite = fnWrite; this.handshakenBackoff = new Backoff({ - initialTimeout: opts.initialTimeout || 500, - maxTimeout: opts.maxTimeout || 10000 + initialTimeout: opts.initialTimeout || 500, + maxTimeout: opts.maxTimeout || 10000 }); this.handshakenBackoff.on('backoff', this.reconnect); this.reconnectBackoff = new Backoff({ - initialTimeout: opts.initialTimeout || 500, - maxTimeout: opts.maxTimeout || 10000 + initialTimeout: opts.initialTimeout || 500, + maxTimeout: opts.maxTimeout || 10000 }); this.reconnectBackoff.on('backoff', this.reconnect); } -Endpoint.tls = require('tls'); -Endpoint.log = log; +Endpoint.tls = require('tls'); +Endpoint.log = log; Endpoint.process = process; _.extend(Endpoint.prototype, EventEmitter.prototype, { - connect:function(port, hostname){ - if(!port){ - throw new Error("connect(port, hostname) port must be defined") + connect: function (port, hostname) { + if (!port) { + throw new Error("connect(port, hostname) port must be defined"); } - if(!hostname){ - throw new Error("connect(port, hostname) hostname must be defined") + if (!hostname) { + throw new Error("connect(port, hostname) hostname must be defined"); } - this.port = port; // Standard TLS port for "IMAP" + this.port = port; // Standard TLS port for "IMAP" this.hostname = hostname; - this.uri = this.hostname + ':' + this.port; + this.uri = this.hostname + ':' + this.port; this._connect(); }, - _connect:function(){ - if(this.socket){ + _connect: function () { + if (this.socket) { this.socket.removeAllListeners(); this.socket.destroy(); } - Endpoint.log.info("[Endpoint] Connecting to "+this.uri+"..."); + Endpoint.log.info("[Endpoint] Connecting to " + this.uri + "..."); this.socket = Endpoint.tls.connect(this.port, this.hostname, this.onConnected); this.socket.on('data', this.onData); this.socket.on('close', this.onClose); this.socket.on('error', this.onError); - this.socket.setTimeout(0, function(){ + this.socket.setTimeout(0, function () { console.log('timeout'); }); @@ -91,8 +90,8 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { this.socket.setKeepAlive(true, 30); }, - reconnect:function(number, delay){ - if(this.connected){ + reconnect: function ( /*number, delay*/ ) { + if (this.connected) { // If, between the .backoff() call and the call to reconnect // we are already back online, don't do anything else return this.reconnectBackoff.reset(); @@ -102,31 +101,37 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { this._connect(); }, - onConnected: function(){ + onConnected: function () { Endpoint.log.debug("[Endpoint] Connected"); this.connected = true; this.reconnectBackoff.reset(); - if(!this.handshaken){ + if (!this.handshaken) { this._sendHandshake(); } this.emit('connect'); }, - _sendHandshake: function(){ - if(this.handshaken){return this.handshakenBackoff.reset();} + _sendHandshake: function () { + if (this.handshaken) { + return this.handshakenBackoff.reset(); + } Endpoint.log.debug("[Endpoint] Handshaking..."); - this.socket.write(JSON.stringify({version:jsonPackage.version, key: this.key, auth: this.auth})); + this.socket.write(JSON.stringify({ + version: jsonPackage.version, + key: this.key, + auth: this.auth + })); }, - onData:function(data){ - if(!this.handshaken){ + onData: function (data) { + if (!this.handshaken) { data = (data || '').toString(); var handshake = data; - if(data.indexOf('*') === -1){ + if (data.indexOf('*') === -1) { data = null; } else { // in case of multiple messages after the handshake var idx = handshake.indexOf('*'); @@ -134,10 +139,10 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { data = data.substr(idx); } - try{ + try { var json = JSON.parse(handshake); - if(json && json.error){ + if (json && json.error) { Endpoint.log.error('[Endpoint] Handshake failed: ' + json.error); Endpoint.log.error('Edit configuration file with `redsmin set_key`, see http://bit.ly/YAIeAM'); Endpoint.log.error('Exiting...'); @@ -146,7 +151,7 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { } - } catch(err){ + } catch (err) { Endpoint.log.error('[Endpoint] Bad handshake response:' + handshake); Endpoint.log.error(err); this.handshakenBackoff.reset(); @@ -156,7 +161,9 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { Endpoint.log.debug('[Endpoint] Handshake succeeded'); this.handshaken = true; - if(!data){return;} + if (!data) { + return; + } } this.fnWrite(data); @@ -165,7 +172,7 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { /** * Forward data from redis to the endpoint */ - write: function(data){ + write: function (data) { this.socket.write(data); }, @@ -173,13 +180,13 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { * If the connection to redsmin just closed, try to reconnect * @param {Error} err */ - onClose: function(sourceWasAnError){ + onClose: function (sourceWasAnError) { Endpoint.log.error("[Endpoint] Connection closed " + (sourceWasAnError ? 'because of an error' : '')); this.handshakenBackoff.reset(); - try{ + try { this.reconnectBackoff.backoff(); - } catch(no_error){} + } catch (no_error) {} this.connected = false; this.handshaken = false; @@ -187,13 +194,13 @@ _.extend(Endpoint.prototype, EventEmitter.prototype, { this.emit('close', sourceWasAnError); }, - onError: function(err){ + onError: function (err) { console.log('onerror', err); Endpoint.log.error('[Endpoint] Error ' + (err ? err.message : '')); this.socket.destroy(); // End the socket - try{ + try { this.onClose(err ? err.message : ''); - }catch(err){ + } catch (err) { console.error('err::onError', err); } } diff --git a/test/Endpoint.test.js b/lib/Endpoint.test.js similarity index 54% rename from test/Endpoint.test.js rename to lib/Endpoint.test.js index 52b2b36..bf1c03b 100644 --- a/test/Endpoint.test.js +++ b/lib/Endpoint.test.js @@ -1,13 +1,14 @@ -var Endpoint = require('../lib/Endpoint') -, _ = require('lodash') -, fs = require('fs') -, sinon = require('sinon') -, jsonPackage = JSON.parse(fs.readFileSync(__dirname + '/../package.json')) -, Socket = require('./helpers/Socket'); - -function tconnect(t, host, hostname, fnWriteStub){ - return function(_host, _hostname, cb){ - if(t){ +'use strict'; +var Endpoint = require('../lib/Endpoint'); +var _ = require('lodash'); +var fs = require('fs'); +var sinon = require('sinon'); +var jsonPackage = JSON.parse(fs.readFileSync(__dirname + '/../package.json')); +var Socket = require('../test/helpers/Socket'); + +function tconnect(t, host, hostname, fnWriteStub) { + return function (_host, _hostname, cb) { + if (t) { t.equal(_host, host); t.equal(_hostname, hostname); } @@ -19,24 +20,26 @@ function tconnect(t, host, hostname, fnWriteStub){ } -function stubLocalClient(fn){ +function stubLocalClient(fn) { return fn || _.noop; } // Quiet console output -Endpoint.log = sinon.stub(_.clone(console)); +Endpoint.log = sinon.stub(_.clone(console)); exports['Endpoint'] = { - setUp: function(done) { + setUp: function (done) { // setup here Endpoint.tls.connect = tconnect(); - this.E = new Endpoint(stubLocalClient(), 'myKey'); + this.E = new Endpoint(stubLocalClient(), 'myKey'); // Mock console - Endpoint.log = _.reduce(console, function(m, v, k){ + Endpoint.log = _.reduce(console, function (m, v, k) { m[k] = _.noop; return m; - }, {debug:_.noop}); + }, { + debug: _.noop + }); done(); }, @@ -47,24 +50,24 @@ exports['Endpoint'] = { callback(); }, - 'constructor': function(t) { + 'constructor': function (t) { t.done(); }, - 'connect (without handshake)':function(t){ + 'connect (without handshake)': function (t) { t.expect(4); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433 - , stub = sinon.stub(); + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433, + stub = sinon.stub(); Endpoint.tls.connect = tconnect(); t.equal(E.handshaken, false); t.equal(E.connected, false); - E.on('connect', function(){ + E.on('connect', function () { t.equal(E.hostname, hostname); t.equal(E.port, port); t.done(); @@ -73,33 +76,36 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'connect without arguments shoudl throw an error':function(t){ + 'connect without arguments shoudl throw an error': function (t) { t.expect(2); - t.throws(function(){ + t.throws(function () { this.E.connect(); }) - t.throws(function(){ + t.throws(function () { this.E.connect(443); }) t.done(); }, - 'connect (with handshake)':function(t){ + 'connect (with handshake)': function (t) { t.expect(3); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433 - , stub = sinon.stub(); + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433, + stub = sinon.stub(); - Endpoint.tls.connect = tconnect(null, null, null, function fnWrite(data){ - t.equal(data, JSON.stringify({version:jsonPackage.version, key:E.key})); + Endpoint.tls.connect = tconnect(null, null, null, function fnWrite(data) { + t.equal(data, JSON.stringify({ + version: jsonPackage.version, + key: E.key + })); t.equal(E.connected, true); }); E.key = 'myKey'; - E.on('connect', function(){ + E.on('connect', function () { t.ok(true); t.done(); }); @@ -107,21 +113,27 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'connect with auth (with handshake)':function(t){ + 'connect with auth (with handshake)': function (t) { t.expect(3); - var E = new Endpoint(stubLocalClient(), 'myKey', {auth:"passw_or_d"}) - , hostname = 'ssl.redsmin.dev' - , port = 433; - - Endpoint.tls.connect = tconnect(null, null, null, function fnWrite(data){ - t.equal(data, JSON.stringify({version:jsonPackage.version, key:E.key, auth:"passw_or_d"})); + var E = new Endpoint(stubLocalClient(), 'myKey', { + auth: "passw_or_d" + }), + hostname = 'ssl.redsmin.dev', + port = 433; + + Endpoint.tls.connect = tconnect(null, null, null, function fnWrite(data) { + t.equal(data, JSON.stringify({ + version: jsonPackage.version, + key: E.key, + auth: "passw_or_d" + })); t.equal(E.connected, true); }); E.key = 'myKey'; - E.on('connect', function(){ + E.on('connect', function () { t.ok(true); t.done(); }); @@ -129,28 +141,35 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'connect (with handshake backoff)':function(t){ + 'connect (with handshake backoff)': function (t) { t.expect(5); var spyWrite = null; var iConnect = 0; var iWrite = 0; - function fnWriteToRedsmin(data){ + + function fnWriteToRedsmin(data) { iWrite++; - if(iWrite === 2){ + if (iWrite === 2) { t.ok(!this.handshaken, 'handshaken'); - t.equal(data, JSON.stringify({version:jsonPackage.version, key:E.key})); + t.equal(data, JSON.stringify({ + version: jsonPackage.version, + key: E.key + })); t.equal(E.connected, true); t.done(); } } - var E = new Endpoint(stubLocalClient(), 'myKey', {initialTimeout:1, maxTimeout:10}) - , hostname = 'ssl.redsmin.dev' - , port = 433 - , stub = sinon.stub(); + var E = new Endpoint(stubLocalClient(), 'myKey', { + initialTimeout: 1, + maxTimeout: 10 + }), + hostname = 'ssl.redsmin.dev', + port = 433, + stub = sinon.stub(); spyWrite = sinon.spy(fnWriteToRedsmin); @@ -158,10 +177,10 @@ exports['Endpoint'] = { E.key = 'myKey'; - E.on('connect', function(){ + E.on('connect', function () { ++iConnect; - if(iConnect === 1){ + if (iConnect === 1) { t.ok(this.connected, 'connected'); t.ok(!this.handshaken, 'handshaken'); // Simulate a disconnection @@ -173,18 +192,18 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'onData (handhsake with error)': function(t){ + 'onData (handhsake with error)': function (t) { t.expect(2); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433; + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433; Endpoint.tls.connect = tconnect(); var processExit = Endpoint.process.exit = sinon.spy(); - E.on('connect', function(){ + E.on('connect', function () { E.onData('{"error":"oups user not found"}'); t.equal(E.handshaken, false); @@ -196,16 +215,16 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'onData (handshake)': function(t){ + 'onData (handshake)': function (t) { t.expect(3); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433; + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433; Endpoint.tls.connect = tconnect(); - E.on('connect', function(){ + E.on('connect', function () { var spy = sinon.spy(E.fnWrite); t.equal(E.handshaken, false, "shouldn't be handshaken at this stage"); @@ -221,22 +240,22 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'onData (handshake merge with info)': function(t){ + 'onData (handshake merge with info)': function (t) { t.expect(3); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433 - , redisInfo = "*1\n$4\ninfo"; + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433, + redisInfo = "*1\n$4\ninfo"; Endpoint.tls.connect = tconnect(); - E.on('connect', function(){ + E.on('connect', function () { var spy = sinon.spy(E, 'fnWrite'); t.equal(E.handshaken, false, "shouldn't be handshaken at this stage"); - E.onData("{\"success\":\"true\"}"+redisInfo); + E.onData("{\"success\":\"true\"}" + redisInfo); t.equal(E.handshaken, true, "should now be handshaken"); @@ -249,17 +268,17 @@ exports['Endpoint'] = { }, - 'onData (handshaken)': function(t){ + 'onData (handshaken)': function (t) { t.expect(1); - var spyFnWrite = sinon.spy() - , E = new Endpoint(spyFnWrite, 'myKey') - , hostname = 'ssl.redsmin.dev' - , port = 433; + var spyFnWrite = sinon.spy(), + E = new Endpoint(spyFnWrite, 'myKey'), + hostname = 'ssl.redsmin.dev', + port = 433; Endpoint.tls.connect = tconnect(); - E.on('connect', function(){ + E.on('connect', function () { E.onData('KEYS *'); @@ -273,21 +292,21 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'onClose': function(t){ + 'onClose': function (t) { t.expect(1); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433; + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433; Endpoint.tls.connect = tconnect(); - E.on('close', function(){ + E.on('close', function () { t.equal(E.connected, false); t.done(); }); - E.on('connect', _.once(function(){ + E.on('connect', _.once(function () { // Emulate an "on close" event E.onClose(new Error('close')); })); @@ -295,28 +314,31 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'onClose (reconnect)': function(t){ + 'onClose (reconnect)': function (t) { t.expect(3); Endpoint.tls.connect = tconnect(); - var E = new Endpoint(stubLocalClient(), 'myKey', {initialTimeout: 1,maxTimeout: 10}) - , hostname = 'ssl.redsmin.dev' - , port = 433; + var E = new Endpoint(stubLocalClient(), 'myKey', { + initialTimeout: 1, + maxTimeout: 10 + }), + hostname = 'ssl.redsmin.dev', + port = 433; var spy = sinon.spy(E, "onConnected"); - E.on('connect', function(){ + E.on('connect', function () { t.ok(true, "connect called"); - if(spy.callCount === 0){ + if (spy.callCount === 0) { t.ok(!this.handshaken, "handshake"); } - if(spy.callCount === 2){ + if (spy.callCount === 2) { t.ok(!this.handshaken, "handshake"); - E._connect = function(){}; + E._connect = function () {}; t.done(); return; } @@ -329,14 +351,14 @@ exports['Endpoint'] = { E.connect(port, hostname); }, - 'onError': function(t){ + 'onError': function (t) { t.expect(1); - var E = this.E - , hostname = 'ssl.redsmin.dev' - , port = 433; + var E = this.E, + hostname = 'ssl.redsmin.dev', + port = 433; - E.on('connect', function(){ + E.on('connect', function () { var spy = sinon.spy(E.socket, "destroy"); E.socket.emit('error'); diff --git a/lib/Proxy.test.js b/lib/Proxy.test.js new file mode 100644 index 0000000..9003b35 --- /dev/null +++ b/lib/Proxy.test.js @@ -0,0 +1,23 @@ +'use strict'; +var Proxy = require('../lib/Proxy'); +var _ = require('lodash'); +var sinon = require('sinon'); + +// Quiet console output +Proxy.log = sinon.stub(_.clone(console)); + +exports['Proxy'] = { + setUp: function (done) { + done(); + }, + + tearDown: function (callback) { + // clean up + //Endpoint.log = console; + callback(); + }, + + 'constructor': function (t) { + t.done(); + } +}; diff --git a/lib/RedisClient.js b/lib/RedisClient.js index 7215d54..9c883d6 100644 --- a/lib/RedisClient.js +++ b/lib/RedisClient.js @@ -1,67 +1,70 @@ -var _ = require('lodash') -, url = require('url') -, Backoff = require('backoff'); +'use strict'; + +var _ = require('lodash'); +var url = require('url'); +var Backoff = require('backoff'); +var EventEmitter = require('events').EventEmitter; /** * @class Redis client * @param {Function} fnWrite `callback(data)` where to write datas from redis * @param {Object} opts [optiona] optionals parameters */ -function RedisClient(fnWrite, opts){ - _.extend(this, require('events').EventEmitter); - _.bindAll(this); - opts = opts || {}; +function RedisClient(fnWrite, opts) { + _.extend(this, EventEmitter); + _.bindAll(this); + opts = opts || {}; - if(!fnWrite || typeof fnWrite !== 'function'){ - throw new Error("RedisClient `fnWrite` parameter is not defined or is not a function"); - } + if (!fnWrite || typeof fnWrite !== 'function') { + throw new Error("RedisClient `fnWrite` parameter is not defined or is not a function"); + } - this.connected = false; + this.connected = false; - /** - * Endpoint instance - * @type {Endpoint} - */ - this.fnWrite = fnWrite; + /** + * Endpoint instance + * @type {Endpoint} + */ + this.fnWrite = fnWrite; - this.port = null; - this.hostname = null; - this.socket = null; + this.port = null; + this.hostname = null; + this.socket = null; - this.backoff = new Backoff({ - initialTimeout: opts.initialTimeout || 1, - maxTimeout: opts.maxTimeout || 1000 - }); + this.backoff = new Backoff({ + initialTimeout: opts.initialTimeout || 1, + maxTimeout: opts.maxTimeout || 1000 + }); - this.backoff.on('backoff', this.reconnect); + this.backoff.on('backoff', this.reconnect); } RedisClient.net = require('net'); RedisClient.log = console; _.extend(RedisClient.prototype, require('events').EventEmitter.prototype, { - PREFIX:'redis://', + PREFIX: 'redis://', - updatePortAndHostname:function(uri){ + updatePortAndHostname: function (uri) { uri = uri || this.PREFIX + '127.0.0.1:6379'; - if(uri.indexOf(this.PREFIX) !== 0){ // add "redis://" if it was not specified + if (uri.indexOf(this.PREFIX) !== 0) { // add "redis://" if it was not specified uri = this.PREFIX + uri; } var parsedUri = url.parse(uri); - this.uri = uri; - this.port = parsedUri.port || 6379; + this.uri = uri; + this.port = parsedUri.port ||  6379; this.hostname = parsedUri.hostname || '127.0.0.1'; }, - connect:function(uri){ + connect: function (uri) { this.updatePortAndHostname(uri); this._connect(); }, - _connect: function(){ - if(this.socket){ + _connect: function () { + if (this.socket) { this.socket.removeAllListeners(); this.socket.destroy(); } @@ -87,14 +90,16 @@ _.extend(RedisClient.prototype, require('events').EventEmitter.prototype, { this.socket.on('error', this.onError); }, - reconnect:function(number, delay){ - if(this.connected){return this.backoff.reset();} + reconnect: function ( /*number , delay*/ ) { + if (this.connected) { + return this.backoff.reset(); + } RedisClient.log.info("[RedisClient] Reconnecting..."); this._connect(); }, - onConnected: function(){ + onConnected: function () { this.connected = true; this.backoff.reset(); RedisClient.log.info("[RedisClient] Redis client connected to " + this.uri); @@ -105,14 +110,14 @@ _.extend(RedisClient.prototype, require('events').EventEmitter.prototype, { * Forward data from Redis to the fnWrite * @param {String} data data from redis */ - onData: function(data){ + onData: function (data) { this.fnWrite(data); }, /** * Forward data from elsewhere to Redis */ - write: function(data){ + write: function (data) { this.socket.write(data); }, @@ -120,15 +125,15 @@ _.extend(RedisClient.prototype, require('events').EventEmitter.prototype, { * If the connection to redis just closed, try to reconnect * @param {Error} err */ - onClose: function(err){ - RedisClient.log.error("Redis client closed " + (err ? err.message :'')); + onClose: function (err) { + RedisClient.log.error("Redis client closed " + (err ? err.message : '')); this.connected = false; this.backoff.backoff(); this.emit('close', err); }, - onError: function(err){ - if(!err){ + onError: function (err) { + if (!err) { return; } diff --git a/test/RedisClient.test.js b/lib/RedisClient.test.js similarity index 63% rename from test/RedisClient.test.js rename to lib/RedisClient.test.js index 78f872e..ec2165f 100644 --- a/test/RedisClient.test.js +++ b/lib/RedisClient.test.js @@ -1,11 +1,12 @@ -var RedisClient = require('../lib/RedisClient') -, _ = require('lodash') -, sinon = require('sinon') -, Socket = require('./helpers/Socket'); - -function tcreateConnection(t, host, hostname){ - return function(_host, _hostname, cb){ - if(t){ +'use strict'; +var RedisClient = require('../lib/RedisClient'); +var _ = require('lodash'); +var sinon = require('sinon'); +var Socket = require('../test/helpers/Socket'); + +function tcreateConnection(t, host, hostname) { + return function (_host, _hostname, cb) { + if (t) { t.equal(_host, host); t.equal(_hostname, hostname); } @@ -15,20 +16,20 @@ function tcreateConnection(t, host, hostname){ }; } -function stubRedsminEndpoint(fn){ - return fn || function(){}; +function stubRedsminEndpoint(fn) { + return fn || function () {}; } // Quiet console output RedisClient.log = sinon.stub(_.clone(console)); -var R = null; +var R = null; var endpoint = null; -var HOST = '127.0.1.1'; -var PORT = 6378; +var HOST = '127.0.1.1'; +var PORT = 6378; exports['RedisClient'] = { - setUp: function(done) { + setUp: function (done) { // setup here endpoint = stubRedsminEndpoint(); R = new RedisClient(endpoint); @@ -40,34 +41,34 @@ exports['RedisClient'] = { callback(); }, - global: function(t) { + global: function (t) { t.expect(2); 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){ + constructor: function (t) { t.done(); }, - connect:function(t){ + connect: function (t) { t.expect(5); RedisClient.net.createConnection = tcreateConnection(t, PORT, HOST); - R.on('connect', function(){ + R.on('connect', function () { t.ok(true, 'connected'); t.equal(R.connected, true); t.done(); }); t.equal(R.connected, false); - R.connect('redis://'+HOST+':'+PORT); + R.connect('redis://' + HOST + ':' + PORT); }, - 'updatePortAndHostname with a connection string without redis://': function(t){ - R.updatePortAndHostname(HOST+':'+PORT); + 'updatePortAndHostname with a connection string without redis://': function (t) { + R.updatePortAndHostname(HOST + ':' + PORT); t.equal(R.port, PORT); t.equal(R.hostname, HOST); t.done(); @@ -78,10 +79,10 @@ exports['RedisClient'] = { * @param {[type]} t [description] * @return {[type]} [description] */ - 'onData': function(t){ + 'onData': function (t) { t.expect(1); - R = new RedisClient(stubRedsminEndpoint(function fnWrite(data){ + R = new RedisClient(stubRedsminEndpoint(function fnWrite(data) { t.equal(data, 'test data'); t.done(); })); @@ -92,7 +93,7 @@ exports['RedisClient'] = { /** * write data to redis */ - 'write': function(t){ + 'write': function (t) { t.expect(1); RedisClient.net.createConnection = tcreateConnection(); @@ -103,42 +104,45 @@ exports['RedisClient'] = { R.write('KEYS *'); - t.ok(spy.calledWith('KEYS *'),"write to redis"); + t.ok(spy.calledWith('KEYS *'), "write to redis"); t.done(); - }, + }, - 'onClose': function(t){ + 'onClose': function (t) { t.expect(1); RedisClient.net.createConnection = tcreateConnection(); - R.on('close', function(){ + R.on('close', function () { t.equal(R.connected, false); t.done(); }); - R.on('connect', _.once(function(){ + R.on('connect', _.once(function () { R.onClose(new Error('close')); })); R.connect('redis://127.0.0.1:6378'); }, - 'onClose (reconnect)': function(t){ + 'onClose (reconnect)': function (t) { t.expect(2); RedisClient.net.createConnection = tcreateConnection(); - R = new RedisClient(stubRedsminEndpoint(), {initialTimeout: 1,maxTimeout: 10}); + R = new RedisClient(stubRedsminEndpoint(), { + initialTimeout: 1, + maxTimeout: 10 + }); var spy = sinon.spy(R, "onConnected"); - R.on('connect', function(){ + R.on('connect', function () { t.ok(true, "connect called"); - if(spy.callCount === 2){ - R._connect = function(){}; + if (spy.callCount === 2) { + R._connect = function () {}; t.done(); return; } @@ -149,12 +153,14 @@ exports['RedisClient'] = { R.connect('redis://127.0.0.1:6378'); }, - 'Reconnect': function(t){ + 'Reconnect': function (t) { t.expect(1); // Simulate that the backoff will always directly hit reconnect - R.backoff.backoff = function(){R.reconnect();}; + R.backoff.backoff = function () { + R.reconnect(); + }; - R._connect = function(){ + R._connect = function () { t.equal(R.connected, false); t.done(); }; @@ -162,10 +168,12 @@ exports['RedisClient'] = { R.onClose(); }, - 'onError': function(t){ + 'onError': function (t) { var R = new RedisClient(stubRedsminEndpoint()); t.expect(1); - t.doesNotThrow(function(){R.onError();}); + t.doesNotThrow(function () { + R.onError(); + }); t.done(); } }; diff --git a/test/Proxy.test.js b/test/Proxy.test.js deleted file mode 100644 index 0048ee2..0000000 --- a/test/Proxy.test.js +++ /dev/null @@ -1,22 +0,0 @@ -var Proxy = require('../lib/Proxy') -, _ = require('lodash') -, sinon = require('sinon'); - -// Quiet console output -Proxy.log = sinon.stub(_.clone(console)); - -exports['Proxy'] = { - setUp: function(done) { - done(); - }, - - tearDown: function (callback) { - // clean up - //Endpoint.log = console; - callback(); - }, - - 'constructor': function(t) { - t.done(); - } -};