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

Commit

Permalink
Reset keepalive timer on PUBACK.
Browse files Browse the repository at this point in the history
References #123.
  • Loading branch information
mcollina committed Apr 22, 2014
1 parent 8e9cb41 commit 4d1f8fc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/client.js
Expand Up @@ -70,6 +70,7 @@ Client.prototype._setup = function() {
});

client.on("puback", function(packet) {
that.setUpTimer();
that.handlePuback(packet);
});

Expand Down
55 changes: 50 additions & 5 deletions test/abstract_server.js
Expand Up @@ -286,11 +286,6 @@ module.exports = function(moscaSettings, createConnection) {
opts.keepalive = keepalive;

var messageId = Math.floor(65535 * Math.random());
var subscriptions = [{
topic: "hello",
qos: 0
}
];

client.connect(opts);

Expand All @@ -311,6 +306,56 @@ module.exports = function(moscaSettings, createConnection) {
});
});

it("should correctly renew the keepalive window after a puback", function(done) {
buildClient(done, function(client) {
var keepalive = 1;

var opts = buildOpts();
opts.keepalive = keepalive;
var closed = false;
var timer;

var messageId = Math.floor(65535 * Math.random());
var subscriptions = [{
topic: "hello",
qos: 1
}
];

client.connect(opts);

client.on("connack", function() {
client.subscribe({
subscriptions: subscriptions,
messageId: messageId
});
});

client.on("suback", function() {
timer = Date.now();
instance.publish({ topic: "hello", payload: "world" });
});

client.stream.on("close", function() {
closed = true;
var interval = (Date.now() - timer) / 1000;
expect(interval).to.be.least(keepalive + keepalive / 2);
});

client.on("publish", function(packet) {
if (closed) {
return;
}

setTimeout(function() {
client.puback({ messageId: packet.messageId });
}, keepalive * 1000 / 2);
});

fastForward(100, 4000);
});
});

it("should correctly renew the keepalive window after an unsubscribe", function(done) {
buildClient(done, function(client) {
var keepalive = 1;
Expand Down

0 comments on commit 4d1f8fc

Please sign in to comment.