From 7718e219e9fbf3ae18aec354ff66495cc9397c4b Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 29 Oct 2015 15:04:47 +0100 Subject: [PATCH] Remove listener if not needed anymore and alawys end a client after a test --- index.js | 3 ++- test/auth.spec.js | 3 +++ test/connection.spec.js | 23 ++++++++++++++--------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index e13d93a02f..1fc40e8ebd 100644 --- a/index.js +++ b/index.js @@ -115,7 +115,8 @@ RedisClient.prototype.install_stream_listeners = function() { }); } - this.stream.on('connect', function () { + this.stream.once('connect', function () { + this.removeAllListeners("timeout"); self.on_connect(); }); diff --git a/test/auth.spec.js b/test/auth.spec.js index ef34b82ccb..35481230ae 100644 --- a/test/auth.spec.js +++ b/test/auth.spec.js @@ -20,6 +20,9 @@ describe("client authentication", function () { var auth = 'porkchopsandwiches'; var client = null; + beforeEach(function () { + client = null; + }); afterEach(function () { client.end(); }); diff --git a/test/connection.spec.js b/test/connection.spec.js index 8a04366ba1..22a64afdc4 100644 --- a/test/connection.spec.js +++ b/test/connection.spec.js @@ -13,10 +13,11 @@ describe("connection tests", function () { var client; + beforeEach(function () { + client = null; + }); afterEach(function () { - if (client) { - client.end(); - } + client.end(); }); describe("on lost connection", function () { @@ -127,13 +128,13 @@ describe("connection tests", function () { it("emit an error after the socket timeout exceeded the connect_timeout time", function (done) { var connect_timeout = 1000; // in ms - var client = redis.createClient({ + client = redis.createClient({ parser: parser, - host: '1.2.3.4', + host: '192.168.74.167', connect_timeout: connect_timeout }); assert(client.stream._events.timeout); - assert.strictEqual(client.address, '1.2.3.4:6379'); + assert.strictEqual(client.address, '192.168.74.167:6379'); var time = Date.now(); client.on("reconnecting", function (params) { @@ -148,21 +149,22 @@ describe("connection tests", function () { }); it("use the system socket timeout if the connect_timeout has not been provided", function () { - var client = redis.createClient({ + client = redis.createClient({ parser: parser, - host: '1.2.3.4' + host: '192.168.74.167' }); assert(client.stream._events.timeout === undefined); }); it("clears the socket timeout after a connection has been established", function (done) { - var client = redis.createClient({ + client = redis.createClient({ parser: parser, connect_timeout: 1000 }); assert.strictEqual(client.stream._idleTimeout, 1000); client.on('connect', function () { assert.strictEqual(client.stream._idleTimeout, -1); + assert(client.stream._events.timeout === undefined); done(); }); }); @@ -255,6 +257,9 @@ describe("connection tests", function () { }); it("throws on strange connection info", function () { + client = { + end: function() {} + }; try { redis.createClient(true); throw new Error('failed');