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

Add a better promise documentation and add some tests

This commit is contained in:
Ruben Bridgewater
2015-09-30 01:57:58 +02:00
parent db8c6e3bc2
commit 29b31f749a
6 changed files with 80 additions and 13 deletions

View File

@@ -52,6 +52,16 @@ describe("The 'mget' method", function () {
});
});
it('handles fetching multiple keys, when some keys do not exist promisified', function () {
return client.MGETAsync("mget keys 1", ["some random shit", "mget keys 2", "mget keys 3"]).then(function (results) {
assert.strictEqual(4, results.length);
assert.strictEqual("mget val 1", results[0].toString());
assert.strictEqual(null, results[1]);
assert.strictEqual("mget val 2", results[2].toString());
assert.strictEqual("mget val 3", results[3].toString());
});
});
afterEach(function () {
client.end();
});

View File

@@ -38,6 +38,12 @@ describe("The 'multi' method", function () {
done();
});
});
it("reports an error if promisified", function () {
return client.multi().execAsync().catch(function(err) {
assert(err.message.match(/The connection has already been closed/));
});
});
});
describe("when connected", function () {
@@ -238,6 +244,22 @@ describe("The 'multi' method", function () {
});
});
it('allows multiple commands to work the same as normal to be performed using a chaining API promisified', function () {
return client.multi()
.mset(['some', '10', 'keys', '20'])
.incr(['some', helper.isNumber(11)])
.incr(['keys'], helper.isNumber(21))
.mget('some', 'keys')
.execAsync()
.then(function (replies) {
assert.equal('OK', replies[0]);
assert.equal(11, replies[1]);
assert.equal(21, replies[2]);
assert.equal(11, replies[3][0].toString());
assert.equal(21, replies[3][1].toString());
});
});
it('allows an array to be provided indicating multiple operations to perform', function (done) {
// test nested multi-bulk replies with nulls.
client.multi([
@@ -299,6 +321,19 @@ describe("The 'multi' method", function () {
});
});
it('reports multiple exceptions when they occur (while EXEC is running) promisified', function () {
return client.multi().config("bar").debug("foo").eval("return {err='this is an error'}", 0).execAsync().then(function (reply) {
assert.strictEqual(reply.length, 3);
assert.equal(reply[0].code, 'ERR');
assert.equal(reply[0].command, 'CONFIG');
assert.equal(reply[2].code, undefined);
assert.equal(reply[2].command, 'EVAL');
assert(/^this is an error/.test(reply[2].message));
assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR");
assert(/^ERR/.test(reply[1].message), "Error message should begin with ERR");
});
});
it('reports multiple exceptions when they occur (while EXEC is running) and calls cb', function (done) {
var multi = client.multi();
multi.config("bar", helper.isError());

View File

@@ -3,6 +3,11 @@
// helpers for configuring a redis client in
// its various modes, ipV6, ipV4, socket.
var redis = require('../../index');
var bluebird = require('bluebird');
// Promisify everything
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
var config = {
redis: redis,

View File

@@ -15,18 +15,15 @@ function waitForRedis (available, cb) {
var ipV4 = false;
var id = setInterval(function () {
tcpPortUsed.check(config.PORT, '127.0.0.1')
.then(function (_ipV4) {
ipV4 = _ipV4;
return tcpPortUsed.check(config.PORT, '::1');
})
.then(function (ipV6) {
if (ipV6 === available && ipV4 === available &&
fs.existsSync('/tmp/redis.sock') === available) {
clearInterval(id);
return cb();
}
});
tcpPortUsed.check(config.PORT, '127.0.0.1').then(function (_ipV4) {
ipV4 = _ipV4;
return tcpPortUsed.check(config.PORT, '::1');
}).then(function (ipV6) {
if (ipV6 === available && ipV4 === available && fs.existsSync('/tmp/redis.sock') === available) {
clearInterval(id);
return cb();
}
});
}, 100);
}