From f3f3bfb24d0d8889a668e8770f0471ddbcbb7d3d Mon Sep 17 00:00:00 2001 From: Samir Naik Date: Tue, 16 Jul 2013 09:18:04 -0700 Subject: [PATCH] Reset ping timer on publish, subscribe, unsubsribe. Adding tests and fixing test on pingreq resetting timer --- lib/client.js | 3 ++ test/server_spec.js | 102 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index d2a422f..6f8246d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -47,16 +47,19 @@ Client.prototype._setup = function() { }); client.on("subscribe", function(packet) { + that.setUpTimer(); that.handleSubscribe(packet); }); client.on("publish", function(packet) { + that.setUpTimer(); that.server.authorizePublish(client, packet.topic, packet.payload, function(err, success) { that.handleAuthorizePublish(err, success, packet); }); }); client.on("unsubscribe", function(packet) { + that.setUpTimer(); logger.info({ packet: packet }, "unsubscribe received"); async.parallel(packet.unsubscriptions.map(that.unsubscribeMapTo.bind(that)), function(err) { if (err) { diff --git a/test/server_spec.js b/test/server_spec.js index 3a2094a..b636a90 100644 --- a/test/server_spec.js +++ b/test/server_spec.js @@ -143,12 +143,110 @@ describe("mosca.Server", function() { client.stream.on("close", function() { var interval = (microtime.now() - timer) / 1000000; - expect(interval).to.be.least(keepalive + keepalive / 4); + expect(interval).to.be.least(keepalive + keepalive / 2); }); setTimeout(function() { client.pingreq(); - }, keepalive * 1000 / 4); + }, keepalive * 1000 / 2); + }); + }); + + it("should correctly renew the keepalive window after a subscribe", function(done) { + buildClient(done, function(client) { + var keepalive = 1; + var timer = microtime.now(); + + var opts = buildOpts(); + opts.keepalive = keepalive; + + var messageId = Math.floor(65535 * Math.random()); + var subscriptions = [{ + topic: "hello", + qos: 0 + } + ]; + + client.connect(opts); + + client.stream.on("close", function() { + var interval = (microtime.now() - timer) / 1000000; + expect(interval).to.be.least(keepalive + keepalive / 2); + }); + + setTimeout(function() { + client.subscribe({ + subscriptions: subscriptions, + messageId: messageId + }); + }, keepalive * 1000 / 2); + }); + }); + + it("should correctly renew the keepalive window after a publish", function(done) { + buildClient(done, function(client) { + var keepalive = 1; + var timer = microtime.now(); + + var opts = buildOpts(); + opts.keepalive = keepalive; + + var messageId = Math.floor(65535 * Math.random()); + var subscriptions = [{ + topic: "hello", + qos: 0 + } + ]; + + client.connect(opts); + + client.stream.on("close", function() { + var interval = (microtime.now() - timer) / 1000000; + expect(interval).to.be.least(keepalive + keepalive / 2); + }); + + setTimeout(function() { + client.publish({ + topic: "hello", + payload: "some data", + messageId: messageId + }); + }, keepalive * 1000 / 2); + }); + }); + + it("should correctly renew the keepalive window after an unsubscribe", function(done) { + buildClient(done, function(client) { + var keepalive = 1; + var timer = microtime.now(); + + var opts = buildOpts(); + opts.keepalive = keepalive; + + var messageId = Math.floor(65535 * Math.random()); + var subscriptions = [{ + topic: "hello", + qos: 0 + } + ]; + + client.connect(opts); + client.subscribe({ + subscriptions: subscriptions, + messageId: messageId + }); + + client.stream.on("close", function() { + var interval = (microtime.now() - timer) / 1000000; + expect(interval).to.be.least(keepalive + keepalive / 2); + }); + + setTimeout(function() { + client.unsubscribe({ + unsubscriptions: ['hello'], + messageId: messageId + }); + }, keepalive * 1000 / 2); }); });