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 #731 from martin-doyle/master
Browse files Browse the repository at this point in the history
Catch exception on server start
  • Loading branch information
mcollina committed Mar 12, 2018
2 parents ae59321 + 793f076 commit e253ca9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ function Server(opts, callback) {
var server = interfaces.serverFactory(iface, fallback, that);
that.servers.push(server);
server.maxConnections = iface.maxConnections || 10000000;

// Catch listen errors
server.on('error', function (e) {
that.logger.error('Error starting Mosca Server');
that.emit('error', e);
});
server.listen(port, host, dn);
}, done);
},
Expand Down
47 changes: 47 additions & 0 deletions test/server_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var steed = require("steed");

var moscaSettings = function() {
return {
port: 1883
};
};

describe("mosca.Server.error", function() {
var instance;
var secondInstance;
var settings;

beforeEach(function(done) {
settings = moscaSettings();
settings.publishNewClient = false;
settings.publishClientDisconnect = false;
instance = new mosca.Server(settings, done);
this.instance = instance;
this.settings = settings;
secondInstance = null;

});

afterEach(function(done) {
var instances = [this.instance];

if (secondInstance) {
instances.push(secondInstance);
}

steed.each(instances, function(instance, cb) {
instance.close(cb);
}, function() {
setImmediate(done);
});
});
it("should get Error: listen EADDRINUSE :::1883", function(done) {
secondInstance = new mosca.Server(moscaSettings(), function(err, server) {
expect(server === secondInstance).to.be.true;
});
secondInstance.on('error', function(err) {
expect(err.toString()).to.be.equal("Error: listen EADDRINUSE :::1883");
done();
});
});
});

0 comments on commit e253ca9

Please sign in to comment.