Changeset

9206:33ee40dc3e25

Pubsub: Add tests for :get_last_item() and fix for non-persistent nodes
author Matthew Wild <mwild1@gmail.com>
date Sat, 18 Aug 2018 15:10:41 +0100
parents 9205:2e710b618440
children 9207:76d593b35958
files plugins/mod_pubsub/pubsub.lib.lua spec/util_pubsub_spec.lua util/pubsub.lua
diffstat 3 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_pubsub/pubsub.lib.lua	Sat Aug 18 14:38:18 2018 +0100
+++ b/plugins/mod_pubsub/pubsub.lib.lua	Sat Aug 18 15:10:41 2018 +0100
@@ -872,8 +872,8 @@
 			truncate = size;
 		});
 	end
-	function get_set:tail()
-		-- This should conveniently return the last item
+	function get_set:head()
+		-- This should conveniently return the most recent item
 		local item = self:get(nil);
 		if item then
 			return item.attr.id, item;
--- a/spec/util_pubsub_spec.lua	Sat Aug 18 14:38:18 2018 +0100
+++ b/spec/util_pubsub_spec.lua	Sat Aug 18 15:10:41 2018 +0100
@@ -285,4 +285,30 @@
 			end);
 		end);
 	end);
+
+	describe("item API", function ()
+		local service;
+		before_each(function ()
+			service = pubsub.new();
+			service:create("test", true, { publish_model = "subscribers" });
+		end);
+		describe("get_last_item()", function ()
+			it("succeeds with nil on empty nodes", function ()
+				local ok, id, item = service:get_last_item("test", true);
+				assert.is_true(ok);
+				assert.is_nil(id);
+				assert.is_nil(item);
+			end);
+			it("succeeds and returns the last item", function ()
+				service:publish("test", true, "one", "hello world");
+				service:publish("test", true, "two", "hello again");
+				service:publish("test", true, "three", "hey");
+				service:publish("test", true, "one", "bye");
+				local ok, id, item = service:get_last_item("test", true);
+				assert.is_true(ok);
+				assert.equal("one", id);
+				assert.equal("bye", item);
+			end);
+		end);
+	end);
 end);
--- a/util/pubsub.lua	Sat Aug 18 14:38:18 2018 +0100
+++ b/util/pubsub.lua	Sat Aug 18 15:10:41 2018 +0100
@@ -604,7 +604,7 @@
 	end
 
 	-- Returns success, id, item
-	return true, self.data[node]:tail();
+	return true, self.data[node]:head();
 end
 
 function service:get_nodes(actor)