|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var config = require('./lib/config'); |
| 5 | +var helper = require('./helper'); |
| 6 | +var redis = config.redis; |
| 7 | + |
| 8 | +describe('send_command_buf', function () { |
| 9 | + |
| 10 | + helper.allTests(function (parser, ip, basicArgs) { |
| 11 | + |
| 12 | + [true, false].forEach(function(return_buffers) { |
| 13 | + oneType(parser, ip, basicArgs, basicArgs[2].detect_buffers, return_buffers); |
| 14 | + }); |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
| 18 | +function oneType(parser, ip, basicArgs, detect_buffers, return_buffers) { |
| 19 | + if (detect_buffers && return_buffers) return; |
| 20 | + |
| 21 | + describe('using ' + parser + ' and ' + ip + ' return_buffers: ' + return_buffers, function () { |
| 22 | + var client; |
| 23 | + var args = config.configureClient(parser, ip, { |
| 24 | + return_buffers: return_buffers, |
| 25 | + detect_buffers: detect_buffers |
| 26 | + }); |
| 27 | + |
| 28 | + beforeEach(function (done) { |
| 29 | + client = redis.createClient.apply(null, args); |
| 30 | + client.once('error', done); |
| 31 | + client.once('connect', function () { |
| 32 | + client.flushdb(function (err) { |
| 33 | + client.hmset('hash key 2', 'key 1', 'val 1', 'key 2', 'val 2'); |
| 34 | + client.set('string key 1', 'string value'); |
| 35 | + done(err); |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('get', function () { |
| 41 | + describe('first argument is a string', function () { |
| 42 | + it('returns a buffer', function (done) { |
| 43 | + client.b_get('string key 1', function (err, reply) { |
| 44 | + assert.strictEqual(true, Buffer.isBuffer(reply)); |
| 45 | + assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply.inspect()); |
| 46 | + return done(err); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns a buffer with send_command_buf', function (done) { |
| 51 | + client.send_command_buf('get', ['string key 1'], function (err, reply) { |
| 52 | + assert.strictEqual(true, Buffer.isBuffer(reply)); |
| 53 | + assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply.inspect()); |
| 54 | + return done(err); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns a buffer with internal_send_command_buf', function (done) { |
| 59 | + client.internal_send_command_buf('get', ['string key 1'], function (err, reply) { |
| 60 | + assert.strictEqual(true, Buffer.isBuffer(reply)); |
| 61 | + assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply.inspect()); |
| 62 | + return done(err); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns a bufffer when executed as part of transaction', function (done) { |
| 67 | + client.multi().b_get('string key 1').exec(function (err, reply) { |
| 68 | + assert.strictEqual(1, reply.length); |
| 69 | + assert.strictEqual(true, Buffer.isBuffer(reply[0])); |
| 70 | + assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply[0].inspect()); |
| 71 | + return done(err); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('multi.hget', function () { |
| 78 | + it('returns buffers', function (done) { |
| 79 | + client.multi() |
| 80 | + .b_hget('hash key 2', 'key 1') |
| 81 | + .b_hget('hash key 2', 'key 1') |
| 82 | + .b_hget('hash key 2', 'key 2') |
| 83 | + .b_hget('hash key 2', 'key 2') |
| 84 | + .exec(function (err, reply) { |
| 85 | + assert.strictEqual(true, Array.isArray(reply)); |
| 86 | + assert.strictEqual(4, reply.length); |
| 87 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect()); |
| 88 | + assert.strictEqual(true, Buffer.isBuffer(reply[1])); |
| 89 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[1].inspect()); |
| 90 | + assert.strictEqual(true, Buffer.isBuffer(reply[2])); |
| 91 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect()); |
| 92 | + assert.strictEqual(true, Buffer.isBuffer(reply[3])); |
| 93 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[3].inspect()); |
| 94 | + return done(err); |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('batch.hget', function () { |
| 100 | + it('returns buffers', function (done) { |
| 101 | + client.batch() |
| 102 | + .b_hget('hash key 2', 'key 1') |
| 103 | + .b_hget('hash key 2', 'key 1') |
| 104 | + .b_hget('hash key 2', 'key 2') |
| 105 | + .b_hget('hash key 2', 'key 2') |
| 106 | + .exec(function (err, reply) { |
| 107 | + assert.strictEqual(true, Array.isArray(reply)); |
| 108 | + assert.strictEqual(4, reply.length); |
| 109 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect()); |
| 110 | + assert.strictEqual(true, Buffer.isBuffer(reply[1])); |
| 111 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[1].inspect()); |
| 112 | + assert.strictEqual(true, Buffer.isBuffer(reply[2])); |
| 113 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect()); |
| 114 | + assert.strictEqual(true, Buffer.isBuffer(reply[3])); |
| 115 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[3].inspect()); |
| 116 | + return done(err); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('hmget', function () { |
| 122 | + describe('using send_command_buf', function () { |
| 123 | + it('returns buffers for keys requested', function (done) { |
| 124 | + client.send_command_buf('hmget', ['hash key 2', 'key 1', 'key 2'], function (err, reply) { |
| 125 | + assert.strictEqual(true, Array.isArray(reply)); |
| 126 | + assert.strictEqual(2, reply.length); |
| 127 | + assert.strictEqual(true, Buffer.isBuffer(reply[0])); |
| 128 | + assert.strictEqual(true, Buffer.isBuffer(reply[1])); |
| 129 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect()); |
| 130 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[1].inspect()); |
| 131 | + return done(err); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('returns buffers for keys requested', function (done) { |
| 136 | + client.send_command('b_hmget', ['hash key 2', 'key 1', 'key 2'], function (err, reply) { |
| 137 | + assert.strictEqual(true, Array.isArray(reply)); |
| 138 | + assert.strictEqual(2, reply.length); |
| 139 | + assert.strictEqual(true, Buffer.isBuffer(reply[0])); |
| 140 | + assert.strictEqual(true, Buffer.isBuffer(reply[1])); |
| 141 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect()); |
| 142 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[1].inspect()); |
| 143 | + return done(err); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + if (!return_buffers) it('returns strings for keys requested', function (done) { |
| 148 | + client.send_command('hmget', ['hash key 2', 'key 1', 'key 2'], function (err, reply) { |
| 149 | + assert.strictEqual(true, Array.isArray(reply)); |
| 150 | + assert.strictEqual(2, reply.length); |
| 151 | + assert.strictEqual('val 1', reply[0]); |
| 152 | + assert.strictEqual('val 2', reply[1]); |
| 153 | + return done(err); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('using b_hmget', function () { |
| 159 | + it('handles array of strings with undefined values in transaction (repro #344)', function (done) { |
| 160 | + client.multi().b_hmget('hash key 2', 'key 3', 'key 4').exec(function (err, reply) { |
| 161 | + assert.strictEqual(true, Array.isArray(reply)); |
| 162 | + assert.strictEqual(1, reply.length); |
| 163 | + assert.strictEqual(2, reply[0].length); |
| 164 | + assert.equal(null, reply[0][0]); |
| 165 | + assert.equal(null, reply[0][1]); |
| 166 | + return done(err); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('returns buffers for keys requested', function (done) { |
| 171 | + client.b_hmget('hash key 2', 'key 1', 'key 2', function (err, reply) { |
| 172 | + assert.strictEqual(true, Array.isArray(reply)); |
| 173 | + assert.strictEqual(2, reply.length); |
| 174 | + assert.strictEqual(true, Buffer.isBuffer(reply[0])); |
| 175 | + assert.strictEqual(true, Buffer.isBuffer(reply[1])); |
| 176 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect()); |
| 177 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[1].inspect()); |
| 178 | + return done(err); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + if (!return_buffers) it('returns strings for keys requested', function (done) { |
| 183 | + client.hmget('hash key 2', 'key 1', 'key 2', function (err, reply) { |
| 184 | + assert.strictEqual(true, Array.isArray(reply)); |
| 185 | + assert.strictEqual(2, reply.length); |
| 186 | + assert.strictEqual('val 1', reply[0]); |
| 187 | + assert.strictEqual('val 2', reply[1]); |
| 188 | + return done(err); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + it('returns buffers for keys requested in transaction', function (done) { |
| 193 | + client.multi().b_hmget('hash key 2', 'key 1', 'key 2').exec(function (err, reply) { |
| 194 | + assert.strictEqual(true, Array.isArray(reply)); |
| 195 | + assert.strictEqual(1, reply.length); |
| 196 | + assert.strictEqual(2, reply[0].length); |
| 197 | + assert.strictEqual(true, Buffer.isBuffer(reply[0][0])); |
| 198 | + assert.strictEqual(true, Buffer.isBuffer(reply[0][1])); |
| 199 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0][0].inspect()); |
| 200 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0][1].inspect()); |
| 201 | + return done(err); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + it('returns buffers for keys requested in .batch', function (done) { |
| 206 | + client.batch().b_hmget('hash key 2', 'key 1', 'key 2').exec(function (err, reply) { |
| 207 | + assert.strictEqual(true, Array.isArray(reply)); |
| 208 | + assert.strictEqual(1, reply.length); |
| 209 | + assert.strictEqual(2, reply[0].length); |
| 210 | + assert.strictEqual(true, Buffer.isBuffer(reply[0][0])); |
| 211 | + assert.strictEqual(true, Buffer.isBuffer(reply[0][1])); |
| 212 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0][0].inspect()); |
| 213 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0][1].inspect()); |
| 214 | + return done(err); |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + describe('hgetall', function (done) { |
| 221 | + describe('using send_command_buf', function (done) { |
| 222 | + it('returns buffer values', function (done) { |
| 223 | + client.send_command_buf('hgetall', ['hash key 2'], function (err, reply) { |
| 224 | + assert.strictEqual('object', typeof reply); |
| 225 | + assert.strictEqual(2, Object.keys(reply).length); |
| 226 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply['key 1'].inspect()); |
| 227 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply['key 2'].inspect()); |
| 228 | + return done(err); |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + it('returns buffer values', function (done) { |
| 233 | + client.send_command('b_hgetall', ['hash key 2'], function (err, reply) { |
| 234 | + assert.strictEqual('object', typeof reply); |
| 235 | + assert.strictEqual(2, Object.keys(reply).length); |
| 236 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply['key 1'].inspect()); |
| 237 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply['key 2'].inspect()); |
| 238 | + return done(err); |
| 239 | + }); |
| 240 | + }); |
| 241 | + }); |
| 242 | + |
| 243 | + describe('using b_hgetall', function (done) { |
| 244 | + it('returns buffer values', function (done) { |
| 245 | + client.b_hgetall('hash key 2', function (err, reply) { |
| 246 | + assert.strictEqual('object', typeof reply); |
| 247 | + assert.strictEqual(2, Object.keys(reply).length); |
| 248 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply['key 1'].inspect()); |
| 249 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply['key 2'].inspect()); |
| 250 | + return done(err); |
| 251 | + }); |
| 252 | + }); |
| 253 | + |
| 254 | + it('returns buffer values when executed in transaction', function (done) { |
| 255 | + client.multi().b_hgetall('hash key 2').exec(function (err, reply) { |
| 256 | + assert.strictEqual(1, reply.length); |
| 257 | + assert.strictEqual('object', typeof reply[0]); |
| 258 | + assert.strictEqual(2, Object.keys(reply[0]).length); |
| 259 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect()); |
| 260 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect()); |
| 261 | + return done(err); |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + it('returns buffer values when executed in .batch', function (done) { |
| 266 | + client.batch().b_hgetall('hash key 2').exec(function (err, reply) { |
| 267 | + assert.strictEqual(1, reply.length); |
| 268 | + assert.strictEqual('object', typeof reply[0]); |
| 269 | + assert.strictEqual(2, Object.keys(reply[0]).length); |
| 270 | + assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect()); |
| 271 | + assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect()); |
| 272 | + return done(err); |
| 273 | + }); |
| 274 | + }); |
| 275 | + }); |
| 276 | + }); |
| 277 | + }); |
| 278 | +} |
0 commit comments