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 #154 from mocheng/fix_151
Browse files Browse the repository at this point in the history
Fix #151
  • Loading branch information
mcollina committed Jun 23, 2014
2 parents ff6a6d3 + de0bfc5 commit 5eeb3cd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/persistence/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ RedisPersistence.prototype._storePacket = function(client, packet, cb) {
var packetKey = "packets:" + client + ":" + packet.messageId;
this._client.multi()
.set(packetKey, JSON.stringify(packet))
.lpush("packets:" + client, packetKey)
.rpush("packets:" + client, packetKey)
.exec(cb);
};

Expand All @@ -321,7 +321,7 @@ RedisPersistence.prototype.streamOfflinePackets = function(client, cb) {
return;
}

that._client.lrange("packets:" + client.id, -1, 10000, function(err, results) {
that._client.lrange("packets:" + client.id, 0, 10000, function(err, results) {

function emit(key, result) {
if (result) {
Expand Down Expand Up @@ -366,7 +366,7 @@ RedisPersistence.prototype.updateOfflinePacket = function(client, messageId, new
that._client.multi()
.rename(oldPacketKey, newPacketKey)
.lrem(listKey, 1, oldPacketKey)
.lpush(listKey, newPacketKey)
.rpush(listKey, newPacketKey)
.exec(done);
};

Expand Down
50 changes: 50 additions & 0 deletions test/persistence/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,56 @@ module.exports = function(create, buildOpts) {
});
});

describe("multiple offline packets", function() {
var client = {
id: "my client id",
clean: false,
logger: globalLogger,
subscriptions: {
hello: {
qos: 1
}
}
};

var first_packet = {
topic: "hello",
qos: 1,
payload: new Buffer("world"),
messageId: 42
};

var second_packet = {
topic: "hello",
qos: 1,
payload: new Buffer("mosca"),
messageId: 43
};

beforeEach(function(done) {
this.instance.storeSubscriptions(client, done);
});

it("should store and stream multiple offline packet", function(done) {
var packets = [];
function onStreamPacket(err, packet) {
packets.push(packet);
if (packets.length === 2) {
expect(packets[0]).to.eql(first_packet);
expect(packets[1]).to.eql(second_packet);
done();
}
}

var instance = this.instance;
instance.storeOfflinePacket(first_packet, function() {
instance.storeOfflinePacket(second_packet, function() {
instance.streamOfflinePackets(client, onStreamPacket);
});
});
});
});

describe("offline packets pattern", function() {
var client = {
id: "my client id - 42",
Expand Down

0 comments on commit 5eeb3cd

Please sign in to comment.