From d739ff3a762e566eba7891407e70b703b4ba2523 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 21 Jan 2016 22:31:17 +0100 Subject: [PATCH] Fix test race condition --- test/connection.spec.js | 45 +++++++++++------------------------------ 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/test/connection.spec.js b/test/connection.spec.js index 2f05329ba1..5f5892dea2 100644 --- a/test/connection.spec.js +++ b/test/connection.spec.js @@ -133,6 +133,7 @@ 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 time = Date.now(); client = redis.createClient({ parser: parser, host: '192.168.74.167', // Should be auto detected as ipv4 @@ -143,7 +144,6 @@ describe("connection tests", function () { }); assert.strictEqual(client.address, '192.168.74.167:6379'); assert.strictEqual(client.connection_options.family, 4); - var time = Date.now(); client.on("reconnecting", function (params) { throw new Error('No reconnect, since no connection was ever established'); @@ -152,7 +152,7 @@ describe("connection tests", function () { client.on('error', function(err) { assert(/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)); assert(Date.now() - time < connect_timeout + 50); - assert(Date.now() - time >= connect_timeout); + assert(Date.now() - time >= connect_timeout - 50); // Somehow this is triggered to early at times done(); }); }); @@ -192,9 +192,7 @@ describe("connection tests", function () { connect_timeout: 1000 }); - client.once('ready', function() { - done(); - }); + client.once('ready', done); }); if (process.platform !== 'win32') { @@ -207,10 +205,7 @@ describe("connection tests", function () { var end = helper.callFuncAfter(done, 2); - client.once('ready', function() { - end(); - }); - + client.once('ready', end); client.set('foo', 'bar', end); }); } @@ -221,9 +216,7 @@ describe("connection tests", function () { client.once("ready", function () { client.removeListener("error", done); - client.get("recon 1", function (err, res) { - done(err); - }); + client.get("recon 1", done); }); }); @@ -233,9 +226,7 @@ describe("connection tests", function () { client.once("ready", function () { client.removeListener("error", done); - client.get("recon 1", function (err, res) { - done(err); - }); + client.get("recon 1", done); }); }); @@ -246,9 +237,7 @@ describe("connection tests", function () { client.once("ready", function () { client.removeListener("error", done); - client.get("recon 1", function (err, res) { - done(err); - }); + client.get("recon 1", done); }); }); @@ -258,9 +247,7 @@ describe("connection tests", function () { client.once("ready", function () { client.removeListener("error", done); - client.get("recon 1", function (err, res) { - done(err); - }); + client.get("recon 1", done); }); }); @@ -385,9 +372,7 @@ describe("connection tests", function () { connect_timeout: 1000 }); assert.strictEqual(client.options.connect_timeout, 1000); - client.on('ready', function () { - done(); - }); + client.on('ready', done); }); it('allows connecting with the redis url in the options object and works with protocols other than the redis protocol (e.g. http)', function (done) { @@ -398,9 +383,7 @@ describe("connection tests", function () { assert.strictEqual(+client.selected_db, 3); assert(!client.options.port); assert.strictEqual(client.options.host, config.HOST[ip]); - client.on("ready", function () { - return done(); - }); + client.on("ready", done); }); it('allows connecting with the redis url and no auth and options as second parameter', function (done) { @@ -409,18 +392,14 @@ describe("connection tests", function () { }; client = redis.createClient('redis://' + config.HOST[ip] + ':' + config.PORT, options); assert.strictEqual(Object.keys(options).length, 1); - client.on("ready", function () { - return done(); - }); + client.on("ready", done); }); it('allows connecting with the redis url and no auth and options as third parameter', function (done) { client = redis.createClient('redis://' + config.HOST[ip] + ':' + config.PORT, null, { detect_buffers: false }); - client.on("ready", function () { - return done(); - }); + client.on("ready", done); }); }