Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #152 from mocheng/fix_150
Browse files Browse the repository at this point in the history
Fix #150: add db to redis backend persistence options
  • Loading branch information
mcollina committed Jun 23, 2014
2 parents 0d50ac6 + 9237004 commit ff6a6d3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/persistence/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
55 changes: 55 additions & 0 deletions test/persistence/redis_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit ff6a6d3

Please sign in to comment.