From de0bfc5c207775d4894c27a874e89dc345ed8fc4 Mon Sep 17 00:00:00 2001 From: Morgan Cheng Date: Mon, 23 Jun 2014 18:06:05 +0800 Subject: [PATCH] correct usage of redis LRANGE command --- lib/persistence/redis.js | 6 ++--- test/persistence/abstract.js | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/persistence/redis.js b/lib/persistence/redis.js index 2df8d4c..81b1fd1 100644 --- a/lib/persistence/redis.js +++ b/lib/persistence/redis.js @@ -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); }; @@ -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) { @@ -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); }; diff --git a/test/persistence/abstract.js b/test/persistence/abstract.js index 9af4f28..69b0b86 100644 --- a/test/persistence/abstract.js +++ b/test/persistence/abstract.js @@ -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",