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

Commit

Permalink
Improved the constructor of the mosca.Server.
Browse files Browse the repository at this point in the history
The constructor can now be called without the 'new' and it yields
the server as the second object of the callback.
  • Loading branch information
mcollina committed Jul 25, 2013
1 parent 7f085a2 commit fd6ce60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 46 deletions.
9 changes: 8 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ var defaults = {
* @api public
*/
function Server(opts, callback) {

if (!(this instanceof Server)) {
return new Server(opts, callback);
}

EventEmitter.call(this);

this.opts = extend(true, {}, defaults, opts);
Expand All @@ -83,7 +88,9 @@ function Server(opts, callback) {
this.ascoltatore = opts.ascoltatore || ascoltatori.build(this.opts.backend);
this.ascoltatore.on("error", this.emit.bind(this));

that.once("ready", callback);
that.once("ready", function() {
callback(null, that);
});

async.series([
function(cb) {
Expand Down
74 changes: 29 additions & 45 deletions test/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ describe("mosca.Server", function() {
});
}

it("should pass itself in the callback", function(done) {
secondInstance = new mosca.Server(moscaSettings(), function(err, server) {
expect(server === secondInstance).to.be.true;
done();
});
});

it("should allow to be called like a function", function(done) {
var func = mosca.Server;
secondInstance = func(moscaSettings(), function(err, server) {
expect(server === secondInstance).to.be.true;
done();
});
});

it("should support connecting and disconnecting", function(done) {
buildClient(done, function(client) {

Expand Down Expand Up @@ -626,7 +641,9 @@ describe("mosca.Server", function() {
type: "mqtt"
};
settings.port = nextPort();
secondInstance = new mosca.Server(settings, cb);
secondInstance = new mosca.Server(settings, function() {
cb();
});
},

function(cb) {
Expand Down Expand Up @@ -737,7 +754,9 @@ describe("mosca.Server", function() {
type: "mqtt"
};
settings.port = settings.port + 1000;
secondInstance = new mosca.Server(settings, cb);
secondInstance = new mosca.Server(settings, function() {
cb();
});
},

function(cb) {
Expand All @@ -756,61 +775,26 @@ describe("mosca.Server", function() {
]);
});

it("should support specifying an Ascoltatore instead of backend options in a tree-based topology", function(done) {
var d = donner(2, done);
it("should support specifying an Ascoltatore instead of backend options", function(done) {

async.waterfall([

function(cb) {
buildAndConnect(d, function(client1) {
cb(null, client1);
});
},

function(client1, cb) {
client1.on("publish", function(packet) {
expect(packet.payload).to.be.eql("some data");
client1.disconnect();
});

var subscriptions = [{
topic: "hello/#",
qos: 0
}
];

client1.subscribe({
subscriptions: subscriptions,
messageId: 42
});
client1.on("suback", function() {
cb(null);
});
},

function(cb) {
settings.ascoltatore = ascoltatori.build({
port: settings.port,
type: "mqtt",
json: false
});
settings.port = settings.port + 1000;
secondInstance = new mosca.Server(settings, cb);
mosca.Server(settings, cb);
},

function(cb) {
buildAndConnect(d, function(client2) {
cb(null, client2);
});
},

function(client2, cb) {
client2.publish({
topic: "hello/world",
payload: "some data"
function(server, cb) {
secondInstance = server;
buildAndConnect(done, function(client) {
client.disconnect();
cb(null);
});
client2.disconnect();
}

]);
});

Expand Down

0 comments on commit fd6ce60

Please sign in to comment.