From 9237004b47c9cb3ca3b3f8f6c1f78d01bd06b3d5 Mon Sep 17 00:00:00 2001 From: Morgan Cheng Date: Mon, 23 Jun 2014 13:52:12 +0800 Subject: [PATCH] add db to redis backend persistence options --- lib/persistence/redis.js | 5 ++++ test/persistence/redis_spec.js | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/persistence/redis.js b/lib/persistence/redis.js index 55736ea..2df8d4c 100644 --- a/lib/persistence/redis.js +++ b/lib/persistence/redis.js @@ -50,6 +50,7 @@ var defaults = { * The current options include: * - `port`, the Redis' port. * - `host`, the Redis' host. + * - `db`, the Redis' database. * - `password`, the Redis' password. * - `redisOpts`, the options for the Redis client. * - `channel`, the pub/sub channel that will be used to synchronize @@ -142,6 +143,10 @@ RedisPersistence.prototype._buildClient = function() { options.host, options.redisOptions); + if (options.db) { + client.select(options.db); + } + if (options.password) { client.auth(options.password); } diff --git a/test/persistence/redis_spec.js b/test/persistence/redis_spec.js index 879ced6..55a9d2d 100644 --- a/test/persistence/redis_spec.js +++ b/test/persistence/redis_spec.js @@ -104,3 +104,58 @@ describe("mosca.persistence.Redis", function() { }); }); }); + +describe("mosca.persistence.Redis select database", function() { + this.timeout(2000); + + var opts = { + ttl: { + checkFrequency: 1000, + subscriptions: 1000, + packets: 1000 + }, + db: 1 // different from default redis database + }; + + abstract(Redis, opts); + + function flush(cb) { + var client = redis.createClient(); + client.select(opts.db); + client.flushdb(function() { + client.quit(cb); + }); + } + + beforeEach(function afterEachRedis(cb) { + flush(cb); + }); + + afterEach(function afterEachRedis(cb) { + flush(cb); + }); + + it("should have persistent data in selected database", function(done) { + var client = { + id: "my client id", + clean: false, + subscriptions: { + "hello/#": { + qos: 1 + } + } + }; + + var redisClientSubscriptionKey = 'client:sub:' + client.id; + + this.instance.storeSubscriptions(client, function() { + + var redisClient = redis.createClient(); + redisClient.select(opts.db); + redisClient.exists(redisClientSubscriptionKey, function(err, existence) { + expect(!!existence).to.eql(true); + redisClient.quit(done); + }); + }); + }); +});