From fb80b1547c10508f77595179f042c01a86fe8804 Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sat, 4 Mar 2017 11:26:52 -0300 Subject: [PATCH 1/7] Created clientError event (fixes #609) --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 0427a88..d350a28 100644 --- a/lib/client.js +++ b/lib/client.js @@ -130,7 +130,7 @@ Client.prototype._setup = function() { }); function handleError(err) { - that.logger.warn(err); + that.server.emit("clientError", err, that); that.onNonDisconnectClose(err.message); } From bd6cd25200f9c5554f080240d5ef05060b03d841 Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sun, 5 Mar 2017 17:13:08 -0300 Subject: [PATCH 2/7] Added unit test for clientError event --- test/server.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/server.js b/test/server.js index 6e772a1..8ccf74a 100644 --- a/test/server.js +++ b/test/server.js @@ -279,6 +279,32 @@ describe("mosca.Server", function() { }); }); + it("should emit \"clientError\" when client error occurs due to unexpected disconnection", function(done) { + // listen to a client error event + this.instance.once("clientError", function(error, client) { + expect(error).to.be.instanceof(Error); + done(); + }); + // cause a connection error between client and server, leading to a socket hang up + require('child_process').spawn('sh', [ '-c', + 'node -e ' + + '"var createConnection = require(\'' + __dirname.replace(/\\/g, "/") + '/helpers/createConnection\');' + + 'var client = createConnection(' + instance.opts.port + ');' + + 'client.on(\'connected\', function() {' + + 'client.connect({' + + 'keepalive: 1000,' + + 'clientId: \'mosca_' + require("crypto").randomBytes(8).toString('hex') + '\',' + + 'protocolId: \'MQIsdp\',' + + 'protocolVersion: 3' + + '});' + + 'client.on(\'connack\', function(packet) {' + + 'throw new Error();' + + '});' + + '});"' + ] + ); + }); + describe("timers", function() { var clock; From 41a7f092179250687f52eb2ca8158b5df4bea9c6 Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sun, 5 Mar 2017 17:23:50 -0300 Subject: [PATCH 3/7] Replaced chai chain to evaluate error --- test/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/server.js b/test/server.js index 8ccf74a..4957381 100644 --- a/test/server.js +++ b/test/server.js @@ -282,7 +282,7 @@ describe("mosca.Server", function() { it("should emit \"clientError\" when client error occurs due to unexpected disconnection", function(done) { // listen to a client error event this.instance.once("clientError", function(error, client) { - expect(error).to.be.instanceof(Error); + expect(error).to.be.an('error'); done(); }); // cause a connection error between client and server, leading to a socket hang up From 33be6e86cf5e72b094d025ef77017e01fed9767a Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sun, 5 Mar 2017 17:31:34 -0300 Subject: [PATCH 4/7] Fixed instance reference error --- test/server.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/server.js b/test/server.js index 4957381..3460e04 100644 --- a/test/server.js +++ b/test/server.js @@ -280,8 +280,9 @@ describe("mosca.Server", function() { }); it("should emit \"clientError\" when client error occurs due to unexpected disconnection", function(done) { + var instance = this.instance; // listen to a client error event - this.instance.once("clientError", function(error, client) { + instance.once("clientError", function(error, client) { expect(error).to.be.an('error'); done(); }); From 4c231cf7a5fe14556293fa1acb934660edf31808 Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sun, 5 Mar 2017 17:51:42 -0300 Subject: [PATCH 5/7] Incorporated createConnection function in spawn code --- test/server.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/server.js b/test/server.js index 3460e04..fab6ff3 100644 --- a/test/server.js +++ b/test/server.js @@ -289,7 +289,16 @@ describe("mosca.Server", function() { // cause a connection error between client and server, leading to a socket hang up require('child_process').spawn('sh', [ '-c', 'node -e ' + - '"var createConnection = require(\'' + __dirname.replace(/\\/g, "/") + '/helpers/createConnection\');' + + '"var Connection = require(\'mqtt-connection\');' + + 'var net = require(\'net\');' + + 'function createConnection(port) {' + + 'var stream = net.createConnection(port);' + + 'var conn = new Connection(stream);' + + 'stream.on(\'connect\', function() {' + + 'conn.emit(\'connected\');' + + '});' + + 'return conn;' + + '}' + 'var client = createConnection(' + instance.opts.port + ');' + 'client.on(\'connected\', function() {' + 'client.connect({' + From d8b53ad2d06c1a494896cb8f40694bbc08f1c3cc Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sat, 11 Mar 2017 14:42:03 -0300 Subject: [PATCH 6/7] listed new event to events list --- lib/server.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/server.js b/lib/server.js index b8d9f1d..38b38ee 100755 --- a/lib/server.js +++ b/lib/server.js @@ -86,6 +86,8 @@ var nop = function() {}; * the client is passed as a parameter. * - `clientDisconnected`, when a client is disconnected; * the client is passed as a parameter. + * - `clientError`, when the server identifies a client connection error; + * the error and the client are passed as parameters. * - `published`, when a new message is published; * the packet and the client are passed as parameters. * - `subscribed`, when a client is subscribed to a topic; From ee591649acb1a1b57e5a3e4f0d850ac0a8917dbd Mon Sep 17 00:00:00 2001 From: Hicaro Adriano Date: Sat, 11 Mar 2017 14:44:17 -0300 Subject: [PATCH 7/7] emits an error using the client connection --- test/server.js | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/test/server.js b/test/server.js index fab6ff3..a3af024 100644 --- a/test/server.js +++ b/test/server.js @@ -286,33 +286,10 @@ describe("mosca.Server", function() { expect(error).to.be.an('error'); done(); }); - // cause a connection error between client and server, leading to a socket hang up - require('child_process').spawn('sh', [ '-c', - 'node -e ' + - '"var Connection = require(\'mqtt-connection\');' + - 'var net = require(\'net\');' + - 'function createConnection(port) {' + - 'var stream = net.createConnection(port);' + - 'var conn = new Connection(stream);' + - 'stream.on(\'connect\', function() {' + - 'conn.emit(\'connected\');' + - '});' + - 'return conn;' + - '}' + - 'var client = createConnection(' + instance.opts.port + ');' + - 'client.on(\'connected\', function() {' + - 'client.connect({' + - 'keepalive: 1000,' + - 'clientId: \'mosca_' + require("crypto").randomBytes(8).toString('hex') + '\',' + - 'protocolId: \'MQIsdp\',' + - 'protocolVersion: 3' + - '});' + - 'client.on(\'connack\', function(packet) {' + - 'throw new Error();' + - '});' + - '});"' - ] - ); + // cause a connection error between client and server + buildAndConnect(function () {}, instance, function(client) { + instance.clients[client.opts.clientId]['connection'].emit("error", new Error()); + }); }); describe("timers", function() {