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

Commit e253ca9

Browse files
authored
Merge pull request #731 from martin-doyle/master
Catch exception on server start
2 parents ae59321 + 793f076 commit e253ca9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/server.js

+6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ function Server(opts, callback) {
216216
var server = interfaces.serverFactory(iface, fallback, that);
217217
that.servers.push(server);
218218
server.maxConnections = iface.maxConnections || 10000000;
219+
220+
// Catch listen errors
221+
server.on('error', function (e) {
222+
that.logger.error('Error starting Mosca Server');
223+
that.emit('error', e);
224+
});
219225
server.listen(port, host, dn);
220226
}, done);
221227
},

test/server_error.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var steed = require("steed");
2+
3+
var moscaSettings = function() {
4+
return {
5+
port: 1883
6+
};
7+
};
8+
9+
describe("mosca.Server.error", function() {
10+
var instance;
11+
var secondInstance;
12+
var settings;
13+
14+
beforeEach(function(done) {
15+
settings = moscaSettings();
16+
settings.publishNewClient = false;
17+
settings.publishClientDisconnect = false;
18+
instance = new mosca.Server(settings, done);
19+
this.instance = instance;
20+
this.settings = settings;
21+
secondInstance = null;
22+
23+
});
24+
25+
afterEach(function(done) {
26+
var instances = [this.instance];
27+
28+
if (secondInstance) {
29+
instances.push(secondInstance);
30+
}
31+
32+
steed.each(instances, function(instance, cb) {
33+
instance.close(cb);
34+
}, function() {
35+
setImmediate(done);
36+
});
37+
});
38+
it("should get Error: listen EADDRINUSE :::1883", function(done) {
39+
secondInstance = new mosca.Server(moscaSettings(), function(err, server) {
40+
expect(server === secondInstance).to.be.true;
41+
});
42+
secondInstance.on('error', function(err) {
43+
expect(err.toString()).to.be.equal("Error: listen EADDRINUSE :::1883");
44+
done();
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)