From 03e8c035034327c3e8ddd71dccd9d34f8921cc5f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 10 Sep 2015 18:08:07 +0200 Subject: [PATCH] Add connection timeout and max attempts tests --- test/connection.spec.js | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/connection.spec.js diff --git a/test/connection.spec.js b/test/connection.spec.js new file mode 100644 index 0000000..af48d2f --- /dev/null +++ b/test/connection.spec.js @@ -0,0 +1,76 @@ +'use strict'; + +var assert = require("assert"); +var config = require("./lib/config"); +var helper = require('./helper'); +var redis = config.redis; + +describe("on lost connection", function () { + helper.allTests(function(parser, ip, args) { + + describe("using " + parser + " and " + ip, function () { + + it("emit an error after max retry attempts and do not try to reconnect afterwards", function (done) { + var max_attempts = 3; + var client = redis.createClient({ + parser: parser, + max_attempts: max_attempts + }); + var calls = 0; + + client.once('ready', function() { + // Pretend that redis can't reconnect + client.on_connect = client.on_error; + client.stream.destroy(); + }); + + client.on("reconnecting", function (params) { + calls++; + }); + + client.on('error', function(err) { + if (/Redis connection in broken state: maximum connection attempts.*?exceeded./.test(err.message)) { + setTimeout(function () { + assert.strictEqual(calls, max_attempts); + done(); + }, 1500); + } + }); + }); + + it("emit an error after max retry timeout and do not try to reconnect afterwards", function (done) { + var connect_timeout = 1000; // in ms + var client = redis.createClient({ + parser: parser, + connect_timeout: connect_timeout + }); + var time = 0; + var multiplier = 0; + + client.once('ready', function() { + // Pretend that redis can't reconnect + client.on_connect = client.on_error; + client.stream.destroy(); + }); + + client.on("reconnecting", function (params) { + if (time > 0 && multiplier === 0) { + multiplier = params.delay / time; + } + time += params.delay; + }); + + client.on('error', function(err) { + if (/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)) { + setTimeout(function () { + assert(time > connect_timeout); + assert(time / multiplier < connect_timeout); + done(); + }, 1500); + } + }); + }); + + }); + }); +});