Skip to content

Commit 14170f9

Browse files
author
Ruben Bridgewater
committed
Improve tests a bit
Reduce timeouts if possible Extend timeouts if needed (windows tests need their time) Don't expose the redis socket to others than the owner Don't create the stunnel log
1 parent 79c1767 commit 14170f9

21 files changed

+136
-86
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ RedisClient.prototype.connection_gone = function (why, error) {
568568
error.code = 'UNCERTAIN_STATE';
569569
this.flush_and_error(error, ['command_queue']);
570570
error.message = 'Redis connection lost and commands aborted in uncertain state. They might have been processed.';
571+
// TODO: Reconsider emitting this always, as each running command is handled anyway
572+
// This should likely be removed in v.3. This is different to the broken connection as we'll reconnect here
571573
this.emit('error', error);
572574
}
573575

lib/individualCommands.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ RedisClient.prototype.batch = RedisClient.prototype.BATCH = function batch (args
2525
// Store db in this.select_db to restore it on reconnect
2626
RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (db, callback) {
2727
var self = this;
28-
db = Array.isArray(db) && db.length === 1 ? db[0] : db;
2928
return this.internal_send_command('select', [db], function (err, res) {
3029
if (err === null) {
3130
self.selected_db = db;
@@ -149,7 +148,6 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
149148
debug('Sending auth to ' + self.address + ' id ' + self.connection_id);
150149

151150
// Stash auth for connect and reconnect.
152-
pass = Array.isArray(pass) && pass.length === 1 ? pass[0] : pass;
153151
this.auth_pass = pass;
154152
this.ready = this.offline_queue.length === 0; // keep the execution order intakt
155153
var tmp = this.internal_send_command('auth', [pass], function (err, res) {
@@ -163,7 +161,7 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
163161
debug('Redis still loading, trying to authenticate later');
164162
setTimeout(function () {
165163
self.auth(pass, callback);
166-
}, 200);
164+
}, 100);
167165
return;
168166
}
169167
}

test/auth.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ describe('client authentication', function () {
5454
assert.strictEqual('retry worked', res);
5555
var now = Date.now();
5656
// Hint: setTimeout sometimes triggers early and therefor the value can be like one or two ms to early
57-
assert(now - time >= 198, 'Time should be above 200 ms (the reconnect time) and is ' + (now - time));
58-
assert(now - time < 300, 'Time should be below 300 ms (the reconnect should only take a bit above 200 ms) and is ' + (now - time));
57+
assert(now - time >= 98, 'Time should be above 100 ms (the reconnect time) and is ' + (now - time));
58+
assert(now - time < 225, 'Time should be below 255 ms (the reconnect should only take a bit above 100 ms) and is ' + (now - time));
5959
done();
6060
});
6161
var tmp = client.command_queue.get(0).callback;

test/batch.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ describe("The 'batch' method", function () {
1212
describe('using ' + parser + ' and ' + ip, function () {
1313

1414
describe('when not connected', function () {
15+
// TODO: This is somewhat broken and should be fixed in v.3
16+
// The commands should return an error instead of returning an empty result
1517
var client;
1618

1719
beforeEach(function (done) {

test/commands/expire.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ describe("The 'expire' method", function () {
2323
client.EXPIRE('expiry key', '1', helper.isNumber(1));
2424
setTimeout(function () {
2525
client.exists(['expiry key'], helper.isNumber(0, done));
26-
}, 1100);
26+
}, 1050);
2727
});
2828

2929
it('expires key after timeout with array syntax', function (done) {
3030
client.set(['expiry key', 'bar'], helper.isString('OK'));
3131
client.EXPIRE(['expiry key', '1'], helper.isNumber(1));
3232
setTimeout(function () {
3333
client.exists(['expiry key'], helper.isNumber(0, done));
34-
}, 1100);
34+
}, 1050);
3535
});
3636

3737
afterEach(function () {

test/commands/flushdb.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe("The 'flushdb' method", function () {
9090
it('results in a db size of zero without a callback', function (done) {
9191
client.flushdb();
9292
setTimeout(function (err, res) {
93-
client.dbsize([], function (err, res) {
93+
client.dbsize(function (err, res) {
9494
helper.isNotError()(err, res);
9595
helper.isType.number()(err, res);
9696
assert.strictEqual(0, res, 'Flushing db should result in db size 0');

test/commands/get.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe("The 'get' method", function () {
7777
client.on('error', function (err) {
7878
throw err;
7979
});
80-
setTimeout(done, 50);
80+
setTimeout(done, 25);
8181
});
8282
});
8383

test/commands/info.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("The 'info' method", function () {
3434
setTimeout(function () {
3535
assert.strictEqual(typeof client.server_info.db2, 'object');
3636
done();
37-
}, 150);
37+
}, 30);
3838
});
3939

4040
it('works with optional section provided with and without callback', function (done) {

test/commands/select.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe("The 'select' method", function () {
6262
client.select(1, function (err) {
6363
assert.equal(err, null);
6464
assert.equal(client.selected_db, 1, 'we should have selected the new valid DB');
65-
return done();
65+
done();
6666
});
6767
});
6868
});
@@ -73,7 +73,7 @@ describe("The 'select' method", function () {
7373
client.select(9999, function (err) {
7474
assert.equal(err.code, 'ERR');
7575
assert.equal(err.message, 'ERR invalid DB index');
76-
return done();
76+
done();
7777
});
7878
});
7979
});
@@ -86,8 +86,8 @@ describe("The 'select' method", function () {
8686
client.select(1);
8787
setTimeout(function () {
8888
assert.equal(client.selected_db, 1, 'we should have selected the new valid DB');
89-
return done();
90-
}, 100);
89+
done();
90+
}, 25);
9191
});
9292
});
9393

test/commands/set.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("The 'set' method", function () {
4343
beforeEach(function (done) {
4444
client = redis.createClient.apply(null, args);
4545
client.once('ready', function () {
46-
done();
46+
client.flushdb(done);
4747
});
4848
});
4949

@@ -62,6 +62,35 @@ describe("The 'set' method", function () {
6262
});
6363
});
6464
});
65+
66+
it('set expire date in seconds', function (done) {
67+
client.set('foo', 'bar', 'ex', 10, helper.isString('OK'));
68+
client.pttl('foo', function (err, res) {
69+
assert(res >= 10000 - 50); // Max 50 ms should have passed
70+
assert(res <= 10000); // Max possible should be 10.000
71+
done(err);
72+
});
73+
});
74+
75+
it('set expire date in milliseconds', function (done) {
76+
client.set('foo', 'bar', 'px', 100, helper.isString('OK'));
77+
client.pttl('foo', function (err, res) {
78+
assert(res >= 50); // Max 50 ms should have passed
79+
assert(res <= 100); // Max possible should be 100
80+
done(err);
81+
});
82+
});
83+
84+
it('only set the key if (not) already set', function (done) {
85+
client.set('foo', 'bar', 'NX', helper.isString('OK'));
86+
client.set('foo', 'bar', 'nx', helper.isNull());
87+
client.set('foo', 'bar', 'EX', '10', 'XX', helper.isString('OK'));
88+
client.ttl('foo', function (err, res) {
89+
assert(res >= 9); // Min 9s should be left
90+
assert(res <= 10); // Max 10s should be left
91+
done(err);
92+
});
93+
});
6594
});
6695

6796
describe('reports an error with invalid parameters', function () {

0 commit comments

Comments
 (0)