|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require("assert"); |
| 4 | +var config = require("./lib/config"); |
| 5 | +var fs = require('fs'); |
| 6 | +var helper = require('./helper'); |
| 7 | +var path = require('path'); |
| 8 | +var redis = config.redis; |
| 9 | + |
| 10 | +var tls_options = { |
| 11 | + servername: "redis.js.org", |
| 12 | + rejectUnauthorized: false, |
| 13 | + ca: [ String(fs.readFileSync(path.resolve(__dirname, "./conf/redis.js.org.cert"))) ] |
| 14 | +}; |
| 15 | + |
| 16 | +var tls_port = 6380; |
| 17 | + |
| 18 | +describe("TLS connection tests", function () { |
| 19 | + before(function (done) { |
| 20 | + helper.stopStunnel(function () { |
| 21 | + helper.startStunnel(done); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + after(function (done) { |
| 26 | + helper.stopStunnel(done); |
| 27 | + }); |
| 28 | + |
| 29 | + helper.allTests(function(parser, ip, args) { |
| 30 | + |
| 31 | + describe("using " + parser + " and " + ip, function () { |
| 32 | + |
| 33 | + var client; |
| 34 | + |
| 35 | + afterEach(function () { |
| 36 | + if (client) { |
| 37 | + client.end(); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + describe("on lost connection", function () { |
| 42 | + it("emit an error after max retry attempts and do not try to reconnect afterwards", function (done) { |
| 43 | + var max_attempts = 4; |
| 44 | + var options = { |
| 45 | + parser: parser, |
| 46 | + max_attempts: max_attempts, |
| 47 | + port: tls_port, |
| 48 | + tls: tls_options |
| 49 | + }; |
| 50 | + client = redis.createClient(options); |
| 51 | + var calls = 0; |
| 52 | + |
| 53 | + client.once('ready', function() { |
| 54 | + helper.killConnection(client); |
| 55 | + }); |
| 56 | + |
| 57 | + client.on("reconnecting", function (params) { |
| 58 | + calls++; |
| 59 | + }); |
| 60 | + |
| 61 | + client.on('error', function(err) { |
| 62 | + if (/Redis connection in broken state: maximum connection attempts.*?exceeded./.test(err.message)) { |
| 63 | + setTimeout(function () { |
| 64 | + assert.strictEqual(calls, max_attempts - 1); |
| 65 | + done(); |
| 66 | + }, 500); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("emit an error after max retry timeout and do not try to reconnect afterwards", function (done) { |
| 72 | + var connect_timeout = 500; // in ms |
| 73 | + client = redis.createClient({ |
| 74 | + parser: parser, |
| 75 | + connect_timeout: connect_timeout, |
| 76 | + port: tls_port, |
| 77 | + tls: tls_options |
| 78 | + }); |
| 79 | + var time = 0; |
| 80 | + |
| 81 | + client.once('ready', function() { |
| 82 | + helper.killConnection(client); |
| 83 | + }); |
| 84 | + |
| 85 | + client.on("reconnecting", function (params) { |
| 86 | + time += params.delay; |
| 87 | + }); |
| 88 | + |
| 89 | + client.on('error', function(err) { |
| 90 | + if (/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)) { |
| 91 | + setTimeout(function () { |
| 92 | + assert(time === connect_timeout); |
| 93 | + done(); |
| 94 | + }, 500); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("end connection while retry is still ongoing", function (done) { |
| 100 | + var connect_timeout = 1000; // in ms |
| 101 | + client = redis.createClient({ |
| 102 | + parser: parser, |
| 103 | + connect_timeout: connect_timeout, |
| 104 | + port: tls_port, |
| 105 | + tls: tls_options |
| 106 | + }); |
| 107 | + |
| 108 | + client.once('ready', function() { |
| 109 | + helper.killConnection(client); |
| 110 | + }); |
| 111 | + |
| 112 | + client.on("reconnecting", function (params) { |
| 113 | + client.end(); |
| 114 | + setTimeout(done, 100); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it("can not connect with wrong host / port in the options object", function (done) { |
| 119 | + var options = { |
| 120 | + host: 'somewhere', |
| 121 | + max_attempts: 1, |
| 122 | + port: tls_port, |
| 123 | + tls: tls_options |
| 124 | + }; |
| 125 | + client = redis.createClient(options); |
| 126 | + var end = helper.callFuncAfter(done, 2); |
| 127 | + |
| 128 | + client.on('error', function (err) { |
| 129 | + assert(/CONNECTION_BROKEN|ENOTFOUND|EAI_AGAIN/.test(err.code)); |
| 130 | + end(); |
| 131 | + }); |
| 132 | + |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe("when not connected", function () { |
| 137 | + |
| 138 | + it("connect with host and port provided in the options object", function (done) { |
| 139 | + client = redis.createClient({ |
| 140 | + host: 'localhost', |
| 141 | + parser: parser, |
| 142 | + connect_timeout: 1000, |
| 143 | + port: tls_port, |
| 144 | + tls: tls_options |
| 145 | + }); |
| 146 | + |
| 147 | + client.once('ready', function() { |
| 148 | + done(); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it("connects correctly with args", function (done) { |
| 153 | + var args_host = args[1]; |
| 154 | + var args_options = args[2] || {}; |
| 155 | + args_options.tls = tls_options; |
| 156 | + client = redis.createClient(tls_port, args_host, args_options); |
| 157 | + client.on("error", done); |
| 158 | + |
| 159 | + client.once("ready", function () { |
| 160 | + client.removeListener("error", done); |
| 161 | + client.get("recon 1", function (err, res) { |
| 162 | + done(err); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + if (ip === 'IPv4') { |
| 168 | + it('allows connecting with the redis url and no auth and options as second parameter', function (done) { |
| 169 | + var options = { |
| 170 | + detect_buffers: false, |
| 171 | + magic: Math.random(), |
| 172 | + port: tls_port, |
| 173 | + tls: tls_options |
| 174 | + }; |
| 175 | + client = redis.createClient('redis://' + config.HOST[ip] + ':' + tls_port, options); |
| 176 | + // verify connection is using TCP, not UNIX socket |
| 177 | + assert.strictEqual(client.connection_options.host, config.HOST[ip]); |
| 178 | + assert.strictEqual(client.connection_options.port, tls_port); |
| 179 | + // verify passed options are in use |
| 180 | + assert.strictEqual(client.options.magic, options.magic); |
| 181 | + client.on("ready", function () { |
| 182 | + return done(); |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + it('allows connecting with the redis url and no auth and options as third parameter', function (done) { |
| 187 | + client = redis.createClient('redis://' + config.HOST[ip] + ':' + tls_port, null, { |
| 188 | + detect_buffers: false, |
| 189 | + tls: tls_options |
| 190 | + }); |
| 191 | + client.on("ready", function () { |
| 192 | + return done(); |
| 193 | + }); |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | + }); |
| 198 | + |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments