1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Improve coverage; make tests ready for Redis 3.2

Add command sanity check
This commit is contained in:
Ruben Bridgewater
2016-04-21 02:56:06 +02:00
parent eae16938cd
commit ce1678c778
9 changed files with 79 additions and 24 deletions

View File

@@ -272,13 +272,13 @@ describe('client authentication', function () {
var args = config.configureClient(parser, ip, {
password: auth
});
client = redis.createClient.apply(redis.createClient, args);
client = redis.createClient.apply(null, args);
client.set('foo', 'bar');
client.subscribe('somechannel', 'another channel', function (err, res) {
client.once('ready', function () {
assert.strictEqual(client.pub_sub_mode, 1);
client.get('foo', function (err, res) {
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
assert(/ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message));
done();
});
});
@@ -292,7 +292,7 @@ describe('client authentication', function () {
});
});
it('indivdual commands work properly with batch', function (done) {
it('individual commands work properly with batch', function (done) {
// quit => might return an error instead of "OK" in the exec callback... (if not connected)
// auth => might return an error instead of "OK" in the exec callback... (if no password is required / still loading on Redis <= 2.4)
// 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 () {
var args = config.configureClient(parser, 'localhost', {
noReadyCheck: true
});
client = redis.createClient.apply(redis.createClient, args);
client = redis.createClient.apply(null, args);
assert.strictEqual(client.selected_db, undefined);
var end = helper.callFuncAfter(done, 8);
client.on('monitor', function () {
@@ -317,9 +317,9 @@ describe('client authentication', function () {
})
.monitor()
.set('foo', 'bar', helper.isString('OK'))
.INFO(function (err, res) {
assert.strictEqual(res.indexOf('# Server\r\nredis_version:'), 0);
assert.deepEqual(client.serverInfo.db5, { avg_ttl: 0, expires: 0, keys: 1 });
.INFO('stats', function (err, res) {
assert.strictEqual(res.indexOf('# Stats\r\n'), 0);
assert.strictEqual(client.serverInfo.sync_full, '0');
})
.get('foo', helper.isString('bar'))
.subscribe(['foo', 'bar'])
@@ -328,8 +328,8 @@ describe('client authentication', function () {
.psubscribe('*')
.quit(helper.isString('OK')) // this might be interesting
.exec(function (err, res) {
res[4] = res[4].substr(0, 10);
assert.deepEqual(res, ['OK', 'OK', 'OK', 'OK', '# Server\r\n', 'bar', 'bar', 'foo', '/foo', '*', 'OK']);
res[4] = res[4].substr(0, 9);
assert.deepEqual(res, ['OK', 'OK', 'OK', 'OK', '# Stats\r\n', 'bar', 'bar', 'foo', '/foo', '*', 'OK']);
end();
});
});

View File

@@ -121,7 +121,7 @@ describe("The 'client' method", function () {
var client2;
beforeEach(function (done) {
client2 = redis.createClient.apply(redis.createClient, args);
client2 = redis.createClient.apply(null, args);
client2.once('ready', function () {
done();
});
@@ -136,9 +136,9 @@ describe("The 'client' method", function () {
// per chunk. So the execution order is only garanteed on each client
var end = helper.callFuncAfter(done, 2);
client.client('setname', 'RUTH', helper.isString('OK'));
client2.client('setname', 'RENEE', helper.isString('OK'));
client2.client('setname', 'MARTIN', helper.isString('OK'));
client.client('setname', 'RUTH');
client2.client('setname', ['RENEE'], helper.isString('OK'));
client2.client(['setname', 'MARTIN'], helper.isString('OK'));
client2.client('getname', function (err, res) {
assert.equal(res, 'MARTIN');
end();

View File

@@ -28,23 +28,23 @@ describe("The 'hgetall' method", function () {
assert.strictEqual('1', obj.__proto__.toString()); // eslint-disable-line no-proto
assert.strictEqual('23', obj.another.toString());
assert.strictEqual('1234', obj.home.toString());
return done(err);
done(err);
});
});
it('handles fetching keys set using an object', function (done) {
client.HMSET('msg_test', { message: 'hello' }, helper.isString('OK'));
client.batch().HMSET('msg_test', { message: 'hello' }, undefined).exec();
client.hgetall('msg_test', function (err, obj) {
assert.strictEqual(1, Object.keys(obj).length);
assert.strictEqual(obj.message, 'hello');
return done(err);
done(err);
});
});
it('handles fetching a messing key', function (done) {
client.hgetall('missing', function (err, obj) {
assert.strictEqual(null, obj);
return done(err);
done(err);
});
});
});

View File

@@ -39,7 +39,7 @@ describe("The 'hmset' method", function () {
});
it('handles object-style syntax and the key being a number', function (done) {
client.HMSET(231232, {'0123456789': 'abcdefghij', 'some manner of key': 'a type of value', 'otherTypes': 555}, helper.isString('OK'));
client.HMSET(231232, {'0123456789': 'abcdefghij', 'some manner of key': 'a type of value', 'otherTypes': 555}, undefined);
client.HGETALL(231232, function (err, obj) {
assert.equal(obj['0123456789'], 'abcdefghij');
assert.equal(obj['some manner of key'], 'a type of value');

View File

@@ -318,6 +318,24 @@ describe('connection tests', function () {
port: 9999
});
});
it('retry_strategy used to reconnect with defaults', function (done) {
client = redis.createClient({
retry_strategy: function (options) {
client.set('foo', 'bar');
return null;
}
});
setTimeout(function () {
client.stream.destroy();
}, 50);
client.on('error', function (err) {
assert.strictEqual(err.code, 'NR_OFFLINE');
assert.strictEqual(err.errors.length, 1);
assert.notStrictEqual(err.message, err.errors[0].message);
done();
});
});
});
describe('when not connected', function () {

View File

@@ -705,7 +705,7 @@ describe("The 'multi' method", function () {
});
multi.get('foo', helper.isString('bar'));
multi.exec(function (err, res) {
res[3] = res[3].substr(0, 10);
res[2] = res[2].substr(0, 10);
assert.deepEqual(res, ['OK', 'OK', '# Server\r\n', 'bar']);
done();
});

View File

@@ -1,6 +1,8 @@
'use strict';
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var config = require('./lib/config');
var helper = require('./helper');
var utils = require('../lib/utils');
@@ -9,6 +11,23 @@ var redis = config.redis;
describe('The node_redis client', function () {
it('individual commands sanity check', function (done) {
// All commands should work the same in multi context or without
// Therefor individual commands always have to be handled in both cases
fs.readFile(path.resolve(__dirname, '../lib/individualCommands.js'), 'utf8', function (err, data) {
var client_prototype = data.match(/(\n| = )RedisClient\.prototype.[a-zA-Z_]+/g);
var multi_prototype = data.match(/(\n| = )Multi\.prototype\.[a-zA-Z_]+/g);
// Check that every entry RedisClient entry has a correspondend Multi entry
assert.strictEqual(client_prototype.filter(function (entry) {
return multi_prototype.indexOf(entry.replace('RedisClient', 'Multi')) === -1;
}).length, 4); // multi and batch are included too
assert.strictEqual(client_prototype.length, multi_prototype.length + 4);
// Check that all entries exist in uppercase and in lowercase variants
assert.strictEqual(data.match(/(\n| = )RedisClient\.prototype.[a-z_]+/g).length * 2, client_prototype.length);
done();
});
});
helper.allTests(function (parser, ip, args) {
describe('using ' + parser + ' and ' + ip, function () {

View File

@@ -19,8 +19,8 @@ describe('publish/subscribe', function () {
beforeEach(function (done) {
var end = helper.callFuncAfter(done, 2);
pub = redis.createClient.apply(redis.createClient, args);
sub = redis.createClient.apply(redis.createClient, args);
pub = redis.createClient.apply(null, args);
sub = redis.createClient.apply(null, args);
pub.once('connect', function () {
pub.flushdb(function () {
end();
@@ -576,7 +576,7 @@ describe('publish/subscribe', function () {
});
// Get is forbidden
sub.get('foo', function (err, res) {
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message));
assert.strictEqual(err.command, 'GET');
});
// Quit is allowed
@@ -586,7 +586,7 @@ describe('publish/subscribe', function () {
it('emit error if only pub sub commands are allowed without callback', function (done) {
sub.subscribe('channel');
sub.on('error', function (err) {
assert.strictEqual(err.message, 'ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context');
assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message));
assert.strictEqual(err.command, 'GET');
done();
});
@@ -639,6 +639,24 @@ describe('publish/subscribe', function () {
});
});
it('arguments variants', function (done) {
sub.batch()
.info(['stats'])
.info()
.client('KILL', ['type', 'pubsub'])
.client('KILL', ['type', 'pubsub'], function () {})
.unsubscribe()
.psubscribe(['pattern:*'])
.punsubscribe('unkown*')
.punsubscribe(['pattern:*'])
.exec(function (err, res) {
sub.client('kill', ['type', 'pubsub']);
sub.psubscribe('*');
sub.punsubscribe('pa*');
sub.punsubscribe(['a', '*'], done);
});
});
afterEach(function () {
// Explicitly ignore still running commands
pub.end(false);

View File

@@ -252,7 +252,7 @@ describe('return_buffers', function () {
var subConnected;
pub = redis.createClient.apply(redis.createClient, basicArgs);
sub = redis.createClient.apply(redis.createClient, args);
sub = redis.createClient.apply(null, args);
pub.once('connect', function () {
pub.flushdb(function () {
pubConnected = true;