|
| 1 | +var nodeAssert = require("../lib/nodeify-assertions"); |
| 2 | +var config = require("../lib/config"); |
| 3 | +var redis = config.redis; |
| 4 | +var async = require("async"); |
| 5 | + |
| 6 | +describe("A node_redis client", function () { |
| 7 | + function allTests(parser, ip, isSocket) { |
| 8 | + var args = config.configureClient(parser, ip, isSocket); |
| 9 | + |
| 10 | + describe("using " + parser + " and " + ip, function () { |
| 11 | + var client; |
| 12 | + |
| 13 | + after(function () { |
| 14 | + client.end(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("connects correctly", function (done) { |
| 18 | + client = redis.createClient.apply(redis.createClient, args); |
| 19 | + client.on("error", done); |
| 20 | + |
| 21 | + client.once("ready", function () { |
| 22 | + client.removeListener("error", done); |
| 23 | + client.get("recon 1", function (err, res) { |
| 24 | + done(err); |
| 25 | + }); |
| 26 | + }); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("when connected", function () { |
| 31 | + var client; |
| 32 | + |
| 33 | + beforeEach(function (done) { |
| 34 | + client = redis.createClient.apply(redis.createClient, args); |
| 35 | + client.once("error", function onError(err) { |
| 36 | + done(err); |
| 37 | + }); |
| 38 | + client.once("ready", function onReady() { |
| 39 | + done(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(function () { |
| 44 | + client.end(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("when redis closes unexpectedly", function () { |
| 48 | + it("reconnects and can retrieve the pre-existing data", function (done) { |
| 49 | + client.on("reconnecting", function on_recon(params) { |
| 50 | + client.on("connect", function on_connect() { |
| 51 | + async.parallel([function (cb) { |
| 52 | + client.get("recon 1", function (err, res) { |
| 53 | + nodeAssert.isString("one")(err, res); |
| 54 | + cb(); |
| 55 | + }); |
| 56 | + }, function (cb) { |
| 57 | + client.get("recon 1", function (err, res) { |
| 58 | + nodeAssert.isString("one")(err, res); |
| 59 | + cb(); |
| 60 | + }); |
| 61 | + }, function (cb) { |
| 62 | + client.get("recon 2", function (err, res) { |
| 63 | + nodeAssert.isString("two")(err, res); |
| 64 | + cb(); |
| 65 | + }); |
| 66 | + }, function (cb) { |
| 67 | + client.get("recon 2", function (err, res) { |
| 68 | + nodeAssert.isString("two")(err, res); |
| 69 | + cb(); |
| 70 | + }); |
| 71 | + }], function (err, results) { |
| 72 | + client.removeListener("connect", on_connect); |
| 73 | + client.removeListener("reconnecting", on_recon); |
| 74 | + done(err); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + client.set("recon 1", "one"); |
| 80 | + client.set("recon 2", "two", function (err, res) { |
| 81 | + // Do not do this in normal programs. This is to simulate the server closing on us. |
| 82 | + // For orderly shutdown in normal programs, do client.quit() |
| 83 | + client.stream.destroy(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("and it's subscribed to a channel", function () { |
| 88 | + // reconnect_select_db_after_pubsub |
| 89 | + // Does not pass. |
| 90 | + // "Connection in subscriber mode, only subscriber commands may be used" |
| 91 | + xit("reconnects, unsubscribes, and can retrieve the pre-existing data", function (done) { |
| 92 | + client.on("reconnecting", function on_recon(params) { |
| 93 | + client.on("ready", function on_connect() { |
| 94 | + async.parallel([function (cb) { |
| 95 | + client.unsubscribe("recon channel", function (err, res) { |
| 96 | + nodeAssert.isNotError()(err, res); |
| 97 | + cb(); |
| 98 | + }); |
| 99 | + }, function (cb) { |
| 100 | + client.get("recon 1", function (err, res) { |
| 101 | + nodeAssert.isString("one")(err, res); |
| 102 | + cb(); |
| 103 | + }); |
| 104 | + }], function (err, results) { |
| 105 | + client.removeListener("connect", on_connect); |
| 106 | + client.removeListener("reconnecting", on_recon); |
| 107 | + done(err); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + client.set("recon 1", "one"); |
| 113 | + client.subscribe("recon channel", function (err, res) { |
| 114 | + // Do not do this in normal programs. This is to simulate the server closing on us. |
| 115 | + // For orderly shutdown in normal programs, do client.quit() |
| 116 | + client.stream.destroy(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it("remains subscribed", function () { |
| 121 | + var client2 = redis.createClient.apply(redis.createClient, args); |
| 122 | + |
| 123 | + client.on("reconnecting", function on_recon(params) { |
| 124 | + client.on("ready", function on_connect() { |
| 125 | + async.parallel([function (cb) { |
| 126 | + client.on("message", function (channel, message) { |
| 127 | + try { |
| 128 | + nodeAssert.isString("recon channel")(null, channel); |
| 129 | + nodeAssert.isString("a test message")(null, message); |
| 130 | + } catch (err) { |
| 131 | + cb(err); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + client2.subscribe("recon channel", function (err, res) { |
| 136 | + if (err) { |
| 137 | + cb(err); |
| 138 | + return; |
| 139 | + } |
| 140 | + client2.publish("recon channel", "a test message"); |
| 141 | + }); |
| 142 | + }], function (err, results) { |
| 143 | + done(err); |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + client.subscribe("recon channel", function (err, res) { |
| 149 | + // Do not do this in normal programs. This is to simulate the server closing on us. |
| 150 | + // For orderly shutdown in normal programs, do client.quit() |
| 151 | + client.stream.destroy(); |
| 152 | + }); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + ['javascript', 'hiredis'].forEach(function (parser) { |
| 160 | + allTests(parser, "/tmp/redis.sock", true); |
| 161 | + ['IPv4', 'IPv6'].forEach(function (ip) { |
| 162 | + allTests(parser, ip); |
| 163 | + }) |
| 164 | + }); |
| 165 | +}); |
0 commit comments