From 6db3d6d2c0cd5d94450d9ee6fc1fff82efebd251 Mon Sep 17 00:00:00 2001 From: martin-doyle Date: Sat, 17 Mar 2018 15:06:22 +0100 Subject: [PATCH 1/3] Catch http exception on server start --- lib/interfaces.js | 5 ++- test/server_error.js | 97 ++++++++++++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 38 deletions(-) diff --git a/lib/interfaces.js b/lib/interfaces.js index c205462..3911569 100644 --- a/lib/interfaces.js +++ b/lib/interfaces.js @@ -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; } diff --git a/test/server_error.js b/test/server_error.js index cdeb098..fa58314 100644 --- a/test/server_error.js +++ b/test/server_error.js @@ -1,47 +1,72 @@ -var steed = require("steed"); +var steed = require('steed'); -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 :::1883', function (done) { + this.timeout(10000); // have to wait for the inject with delay of two seconds + instance = new mosca.Server(moscaSettings(), function (err, server) { + expect(server === instance).to.be.true; + }); + 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(); + }); + }); + it('should get HTTP port Error: listen EADDRINUSE :::8000', function (done) { + this.timeout(10000); // have to wait for the inject with delay of two seconds + instance = new mosca.Server(moscaSettings(), function (err, server) { + expect(server === instance).to.be.true; + }); + secondInstance = new mosca.Server(moscaSettings2(), function (err, server) { + 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()).to.be.equal('Error: listen EADDRINUSE :::8000'); + done(); }); + }); }); \ No newline at end of file From 64391bbe7dafcdf59cc5dd7d4e4547797dd21aa9 Mon Sep 17 00:00:00 2001 From: martin-doyle Date: Sat, 24 Mar 2018 08:15:11 +0100 Subject: [PATCH 2/3] Change server error unit test --- test/server_error.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/server_error.js b/test/server_error.js index fa58314..f2726ff 100644 --- a/test/server_error.js +++ b/test/server_error.js @@ -43,7 +43,7 @@ describe('mosca.Server.error', function () { setImmediate(done); }); }); - it('should get MQTT port Error: listen EADDRINUSE :::1883', function (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) { expect(server === instance).to.be.true; @@ -52,11 +52,11 @@ describe('mosca.Server.error', function () { expect(server === secondInstance).to.be.true; }); secondInstance.on('error', function (err) { - expect(err.toString()).to.be.equal('Error: listen EADDRINUSE :::1883'); + expect(err.toString().substr(0, 24)).to.be.equal('Error: listen EADDRINUSE'); done(); }); }); - it('should get HTTP port Error: listen EADDRINUSE :::8000', function (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) { expect(server === instance).to.be.true; @@ -65,7 +65,7 @@ describe('mosca.Server.error', function () { expect(server === secondInstance).to.be.true; }); secondInstance.on('error', function (err) { - expect(err.toString()).to.be.equal('Error: listen EADDRINUSE :::8000'); + expect(err.toString().substr(0, 24)).to.be.equal('Error: listen EADDRINUSE'); done(); }); }); From 9005cac47039824f990b2d4815ef8ef0d5b1d243 Mon Sep 17 00:00:00 2001 From: martin-doyle Date: Thu, 5 Apr 2018 18:50:09 +0100 Subject: [PATCH 3/3] Change server error unit test, add check if err is null --- test/server_error.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/server_error.js b/test/server_error.js index f2726ff..4477d78 100644 --- a/test/server_error.js +++ b/test/server_error.js @@ -1,4 +1,5 @@ var steed = require('steed'); +var assert = chai.assert; var moscaSettings = function () { return { @@ -46,9 +47,11 @@ describe('mosca.Server.error', function () { 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) { @@ -59,9 +62,11 @@ describe('mosca.Server.error', function () { 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; }); secondInstance.on('error', function (err) {