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

allow persistence.factory in options to be string #171

Merged
merged 3 commits into from
Jul 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ module.exports.Memory = require("./memory");
module.exports.LevelUp = require("./levelup");
module.exports.Redis = require("./redis");
module.exports.Mongo = require("./mongo");

var factories = {};
Object.keys(module.exports).forEach(function(type) {
factories[type.toLowerCase()] = module.exports[type];
});

module.exports.getFactory = function(name) {
return factories[name.toLowerCase()];
};
18 changes: 14 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var Stats = require("./stats");
var uuid = require("node-uuid");
var st = require("st");
var url = require("url");
var persistence = require('./persistence');
var defaults = {
port: 1883,
host: null,
Expand Down Expand Up @@ -120,12 +121,21 @@ function Server(opts, callback) {

this.opts = extend(true, {}, defaults, opts);

if (this.opts.persistence && this.opts.persistence.factory) {
this.opts.persistence.factory(this.opts.persistence).wire(this);
}

callback = callback || function() {};

var persistenceFactory = this.opts.persistence && this.opts.persistence.factory;
if (persistenceFactory) {
if (typeof persistenceFactory === 'string') {
var factoryName = persistenceFactory;
persistenceFactory = persistence.getFactory(factoryName);
if (!persistenceFactory) {
return callback(new Error('No persistence factory found for ' + factoryName ));
}
}

persistenceFactory(this.opts.persistence).wire(this);
}

this._dedupId = 0;
this.clients = {};
this.closed = false;
Expand Down
49 changes: 49 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,53 @@ describe("mosca.Server - MQTT backend", function() {
}
], done);
});

it("should build the correct persistence with string", function(done) {
var newSettings = moscaSettings();

newSettings.persistence = {
factory: 'redis',
port: 6379,
host: 'localhost'
};

var server = new mosca.Server(newSettings);

async.series([

function(cb) {
server.on("ready", cb);
},

function(cb) {
expect(server.persistence.constructor).to.match(/RedisPersistence/);
cb();
},

function(cb) {
server.close(cb);
}
], done);
});

it("should fail if persistence string is not correct", function(done) {
var newSettings = moscaSettings();

newSettings.persistence = {
factory: 'no_such_persistence',
port: 6379,
host: 'localhost'
};

var server = new mosca.Server(newSettings, function(err) {
if(err instanceof Error) {
done();
} else {
expect().fail('new mosca.Server should fail');
}
});

});


});