Skip to content

Commit dec4ac1

Browse files
author
Ruben Bridgewater
committed
Improve coverage; make tests ready for Redis 3.2
Add command sanity check
1 parent 556e747 commit dec4ac1

File tree

9 files changed

+79
-24
lines changed

9 files changed

+79
-24
lines changed

test/auth.spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ describe('client authentication', function () {
272272
var args = config.configureClient(parser, ip, {
273273
password: auth
274274
});
275-
client = redis.createClient.apply(redis.createClient, args);
275+
client = redis.createClient.apply(null, args);
276276
client.set('foo', 'bar');
277277
client.subscribe('somechannel', 'another channel', function (err, res) {
278278
client.once('ready', function () {
279279
assert.strictEqual(client.pub_sub_mode, 1);
280280
client.get('foo', function (err, res) {
281-
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
281+
assert(/ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message));
282282
done();
283283
});
284284
});
@@ -292,7 +292,7 @@ describe('client authentication', function () {
292292
});
293293
});
294294

295-
it('indivdual commands work properly with batch', function (done) {
295+
it('individual commands work properly with batch', function (done) {
296296
// quit => might return an error instead of "OK" in the exec callback... (if not connected)
297297
// auth => might return an error instead of "OK" in the exec callback... (if no password is required / still loading on Redis <= 2.4)
298298
// This could be fixed by checking the return value of the callback in the exec callback and
@@ -302,7 +302,7 @@ describe('client authentication', function () {
302302
var args = config.configureClient(parser, 'localhost', {
303303
noReadyCheck: true
304304
});
305-
client = redis.createClient.apply(redis.createClient, args);
305+
client = redis.createClient.apply(null, args);
306306
assert.strictEqual(client.selected_db, undefined);
307307
var end = helper.callFuncAfter(done, 8);
308308
client.on('monitor', function () {
@@ -317,9 +317,9 @@ describe('client authentication', function () {
317317
})
318318
.monitor()
319319
.set('foo', 'bar', helper.isString('OK'))
320-
.INFO(function (err, res) {
321-
assert.strictEqual(res.indexOf('# Server\r\nredis_version:'), 0);
322-
assert.deepEqual(client.serverInfo.db5, { avg_ttl: 0, expires: 0, keys: 1 });
320+
.INFO('stats', function (err, res) {
321+
assert.strictEqual(res.indexOf('# Stats\r\n'), 0);
322+
assert.strictEqual(client.serverInfo.sync_full, '0');
323323
})
324324
.get('foo', helper.isString('bar'))
325325
.subscribe(['foo', 'bar'])
@@ -328,8 +328,8 @@ describe('client authentication', function () {
328328
.psubscribe('*')
329329
.quit(helper.isString('OK')) // this might be interesting
330330
.exec(function (err, res) {
331-
res[4] = res[4].substr(0, 10);
332-
assert.deepEqual(res, ['OK', 'OK', 'OK', 'OK', '# Server\r\n', 'bar', 'bar', 'foo', '/foo', '*', 'OK']);
331+
res[4] = res[4].substr(0, 9);
332+
assert.deepEqual(res, ['OK', 'OK', 'OK', 'OK', '# Stats\r\n', 'bar', 'bar', 'foo', '/foo', '*', 'OK']);
333333
end();
334334
});
335335
});

test/commands/client.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe("The 'client' method", function () {
121121
var client2;
122122

123123
beforeEach(function (done) {
124-
client2 = redis.createClient.apply(redis.createClient, args);
124+
client2 = redis.createClient.apply(null, args);
125125
client2.once('ready', function () {
126126
done();
127127
});
@@ -136,9 +136,9 @@ describe("The 'client' method", function () {
136136
// per chunk. So the execution order is only garanteed on each client
137137
var end = helper.callFuncAfter(done, 2);
138138

139-
client.client('setname', 'RUTH', helper.isString('OK'));
140-
client2.client('setname', 'RENEE', helper.isString('OK'));
141-
client2.client('setname', 'MARTIN', helper.isString('OK'));
139+
client.client('setname', 'RUTH');
140+
client2.client('setname', ['RENEE'], helper.isString('OK'));
141+
client2.client(['setname', 'MARTIN'], helper.isString('OK'));
142142
client2.client('getname', function (err, res) {
143143
assert.equal(res, 'MARTIN');
144144
end();

test/commands/hgetall.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ describe("The 'hgetall' method", function () {
2828
assert.strictEqual('1', obj.__proto__.toString()); // eslint-disable-line no-proto
2929
assert.strictEqual('23', obj.another.toString());
3030
assert.strictEqual('1234', obj.home.toString());
31-
return done(err);
31+
done(err);
3232
});
3333
});
3434

3535
it('handles fetching keys set using an object', function (done) {
36-
client.HMSET('msg_test', { message: 'hello' }, helper.isString('OK'));
36+
client.batch().HMSET('msg_test', { message: 'hello' }, undefined).exec();
3737
client.hgetall('msg_test', function (err, obj) {
3838
assert.strictEqual(1, Object.keys(obj).length);
3939
assert.strictEqual(obj.message, 'hello');
40-
return done(err);
40+
done(err);
4141
});
4242
});
4343

4444
it('handles fetching a messing key', function (done) {
4545
client.hgetall('missing', function (err, obj) {
4646
assert.strictEqual(null, obj);
47-
return done(err);
47+
done(err);
4848
});
4949
});
5050
});

test/commands/hmset.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("The 'hmset' method", function () {
3939
});
4040

4141
it('handles object-style syntax and the key being a number', function (done) {
42-
client.HMSET(231232, {'0123456789': 'abcdefghij', 'some manner of key': 'a type of value', 'otherTypes': 555}, helper.isString('OK'));
42+
client.HMSET(231232, {'0123456789': 'abcdefghij', 'some manner of key': 'a type of value', 'otherTypes': 555}, undefined);
4343
client.HGETALL(231232, function (err, obj) {
4444
assert.equal(obj['0123456789'], 'abcdefghij');
4545
assert.equal(obj['some manner of key'], 'a type of value');

test/connection.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,24 @@ describe('connection tests', function () {
318318
port: 9999
319319
});
320320
});
321+
322+
it('retry_strategy used to reconnect with defaults', function (done) {
323+
client = redis.createClient({
324+
retry_strategy: function (options) {
325+
client.set('foo', 'bar');
326+
return null;
327+
}
328+
});
329+
setTimeout(function () {
330+
client.stream.destroy();
331+
}, 50);
332+
client.on('error', function (err) {
333+
assert.strictEqual(err.code, 'NR_OFFLINE');
334+
assert.strictEqual(err.errors.length, 1);
335+
assert.notStrictEqual(err.message, err.errors[0].message);
336+
done();
337+
});
338+
});
321339
});
322340

323341
describe('when not connected', function () {

test/multi.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ describe("The 'multi' method", function () {
705705
});
706706
multi.get('foo', helper.isString('bar'));
707707
multi.exec(function (err, res) {
708-
res[3] = res[3].substr(0, 10);
708+
res[2] = res[2].substr(0, 10);
709709
assert.deepEqual(res, ['OK', 'OK', '# Server\r\n', 'bar']);
710710
done();
711711
});

test/node_redis.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
var assert = require('assert');
4+
var fs = require('fs');
5+
var path = require('path');
46
var config = require('./lib/config');
57
var helper = require('./helper');
68
var utils = require('../lib/utils');
@@ -9,6 +11,23 @@ var redis = config.redis;
911

1012
describe('The node_redis client', function () {
1113

14+
it('individual commands sanity check', function (done) {
15+
// All commands should work the same in multi context or without
16+
// Therefor individual commands always have to be handled in both cases
17+
fs.readFile(path.resolve(__dirname, '../lib/individualCommands.js'), 'utf8', function (err, data) {
18+
var client_prototype = data.match(/(\n| = )RedisClient\.prototype.[a-zA-Z_]+/g);
19+
var multi_prototype = data.match(/(\n| = )Multi\.prototype\.[a-zA-Z_]+/g);
20+
// Check that every entry RedisClient entry has a correspondend Multi entry
21+
assert.strictEqual(client_prototype.filter(function (entry) {
22+
return multi_prototype.indexOf(entry.replace('RedisClient', 'Multi')) === -1;
23+
}).length, 4); // multi and batch are included too
24+
assert.strictEqual(client_prototype.length, multi_prototype.length + 4);
25+
// Check that all entries exist in uppercase and in lowercase variants
26+
assert.strictEqual(data.match(/(\n| = )RedisClient\.prototype.[a-z_]+/g).length * 2, client_prototype.length);
27+
done();
28+
});
29+
});
30+
1231
helper.allTests(function (parser, ip, args) {
1332

1433
describe('using ' + parser + ' and ' + ip, function () {

test/pubsub.spec.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ describe('publish/subscribe', function () {
1919
beforeEach(function (done) {
2020
var end = helper.callFuncAfter(done, 2);
2121

22-
pub = redis.createClient.apply(redis.createClient, args);
23-
sub = redis.createClient.apply(redis.createClient, args);
22+
pub = redis.createClient.apply(null, args);
23+
sub = redis.createClient.apply(null, args);
2424
pub.once('connect', function () {
2525
pub.flushdb(function () {
2626
end();
@@ -576,7 +576,7 @@ describe('publish/subscribe', function () {
576576
});
577577
// Get is forbidden
578578
sub.get('foo', function (err, res) {
579-
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
579+
assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message));
580580
assert.strictEqual(err.command, 'GET');
581581
});
582582
// Quit is allowed
@@ -586,7 +586,7 @@ describe('publish/subscribe', function () {
586586
it('emit error if only pub sub commands are allowed without callback', function (done) {
587587
sub.subscribe('channel');
588588
sub.on('error', function (err) {
589-
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
589+
assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message));
590590
assert.strictEqual(err.command, 'GET');
591591
done();
592592
});
@@ -639,6 +639,24 @@ describe('publish/subscribe', function () {
639639
});
640640
});
641641

642+
it('arguments variants', function (done) {
643+
sub.batch()
644+
.info(['stats'])
645+
.info()
646+
.client('KILL', ['type', 'pubsub'])
647+
.client('KILL', ['type', 'pubsub'], function () {})
648+
.unsubscribe()
649+
.psubscribe(['pattern:*'])
650+
.punsubscribe('unkown*')
651+
.punsubscribe(['pattern:*'])
652+
.exec(function (err, res) {
653+
sub.client('kill', ['type', 'pubsub']);
654+
sub.psubscribe('*');
655+
sub.punsubscribe('pa*');
656+
sub.punsubscribe(['a', '*'], done);
657+
});
658+
});
659+
642660
afterEach(function () {
643661
// Explicitly ignore still running commands
644662
pub.end(false);

test/return_buffers.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ describe('return_buffers', function () {
252252
var subConnected;
253253

254254
pub = redis.createClient.apply(redis.createClient, basicArgs);
255-
sub = redis.createClient.apply(redis.createClient, args);
255+
sub = redis.createClient.apply(null, args);
256256
pub.once('connect', function () {
257257
pub.flushdb(function () {
258258
pubConnected = true;

0 commit comments

Comments
 (0)