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 #733 from martin-doyle/master
Browse files Browse the repository at this point in the history
Catch http exception on server start
  • Loading branch information
mcollina authored Apr 6, 2018
2 parents 822b2d4 + 9005cac commit f3deb5d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
5 changes: 3 additions & 2 deletions lib/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ function mqttsFactory(iface, fallback, mosca) {

return tls.createServer(credentials, buildWrap(mosca));
}

function httpFactory(iface, fallback, mosca) {
var serve = buildServe(iface, mosca);
var server = http.createServer(serve);

mosca.attachHttpServer(server);
server.on('listening', function () {
mosca.attachHttpServer(server);
});
return server;
}

Expand Down
102 changes: 66 additions & 36 deletions test/server_error.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,77 @@
var steed = require("steed");
var steed = require('steed');
var assert = chai.assert;

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

describe("mosca.Server.error", function() {
var instance;
var secondInstance;
var settings;
var moscaSettings2 = function () {
return {
port: 1884,
http: {
port: 8000
}
};
};

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;

});
describe('mosca.Server.error', function () {
var instance;
var secondInstance;

afterEach(function(done) {
var instances = [this.instance];
beforeEach(function (done) {
instance = null;
secondInstance = null;
done();
});

if (secondInstance) {
instances.push(secondInstance);
}
afterEach(function (done) {
this.instance = instance;
var instances = [this.instance];

steed.each(instances, function(instance, cb) {
instance.close(cb);
}, function() {
setImmediate(done);
});
if (secondInstance) {
instances.push(secondInstance);
}

steed.each(instances, function (instance, cb) {
instance.close(cb);
}, function () {
setImmediate(done);
});
});
it('should get MQTT port Error: listen EADDRINUSE', function (done) {
this.timeout(10000); // have to wait for the inject with delay of two seconds
instance = new mosca.Server(moscaSettings(), function (err, server) {
assert.ifError(err);
expect(server === instance).to.be.true;
});
secondInstance = new mosca.Server(moscaSettings(), function (err, server) {
assert.ifError(err);
expect(server === secondInstance).to.be.true;
});
secondInstance.on('error', function (err) {
expect(err.toString().substr(0, 24)).to.be.equal('Error: listen EADDRINUSE');
done();
});
});
it('should get HTTP port Error: listen EADDRINUSE', function (done) {
this.timeout(10000); // have to wait for the inject with delay of two seconds
instance = new mosca.Server(moscaSettings(), function (err, server) {
assert.ifError(err);
expect(server === instance).to.be.true;
});
secondInstance = new mosca.Server(moscaSettings2(), function (err, server) {
assert.ifError(err);
expect(server === secondInstance).to.be.true;
});
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();
});
secondInstance.on('error', function (err) {
expect(err.toString().substr(0, 24)).to.be.equal('Error: listen EADDRINUSE');
done();
});
});
});

0 comments on commit f3deb5d

Please sign in to comment.