From 8a97d634a4c5c5d046424c227a4b26bb9f486481 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 6 Jan 2022 01:18:35 +0100 Subject: util.pubsub: Fix item store resize to "max" Previously this would end up passing the "max" directly to the underlying storage. --- plugins/mod_pep.lua | 1 + plugins/mod_pubsub/mod_pubsub.lua | 1 + spec/util_pubsub_spec.lua | 20 ++++++++++++++++++++ util/pubsub.lua | 7 ++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/plugins/mod_pep.lua b/plugins/mod_pep.lua index bcd529ca..f2a25e00 100644 --- a/plugins/mod_pep.lua +++ b/plugins/mod_pep.lua @@ -221,6 +221,7 @@ function get_pep_service(username) ["access_model"] = "presence"; ["send_last_published_item"] = "on_sub_and_presence"; }; + max_items = max_max_items; autocreate_on_publish = true; autocreate_on_subscribe = false; diff --git a/plugins/mod_pubsub/mod_pubsub.lua b/plugins/mod_pubsub/mod_pubsub.lua index be460f72..4af16a6f 100644 --- a/plugins/mod_pubsub/mod_pubsub.lua +++ b/plugins/mod_pubsub/mod_pubsub.lua @@ -224,6 +224,7 @@ function module.load() node_defaults = { ["persist_items"] = true; }; + max_items = max_max_items; nodestore = node_store; itemstore = create_simple_itemstore; broadcaster = simple_broadcast; diff --git a/spec/util_pubsub_spec.lua b/spec/util_pubsub_spec.lua index 2dc5ca60..45a612a0 100644 --- a/spec/util_pubsub_spec.lua +++ b/spec/util_pubsub_spec.lua @@ -169,6 +169,26 @@ describe("util.pubsub", function () }, ret); end); + it("has a default max_items", function () + assert.truthy(service.config.max_items); + end) + + it("changes max_items to max", function () + assert.truthy(service:set_node_config("node", true, { max_items = "max" })); + end); + + it("publishes some more items", function() + for i = 4, service.config.max_items + 5 do + assert.truthy(service:publish("node", true, tostring(i), "item " .. tostring(i))); + end + end); + + it("should still return only two items", function () + local ok, ret = service:get_items("node", true); + assert.truthy(ok); + assert.same(service.config.max_items, #ret); + end); + end); describe("the thing", function () diff --git a/util/pubsub.lua b/util/pubsub.lua index 75620a42..acb34db9 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -5,6 +5,7 @@ local errors = require "util.error"; local service_mt = {}; local default_config = { + max_items = 256; itemstore = function (config, _) return cache.new(config["max_items"]) end; broadcaster = function () end; subscriber_filter = function (subs) return subs end; @@ -841,7 +842,11 @@ function service:set_node_config(node, actor, new_config) --> ok, err end elseif old_config["max_items"] ~= node_obj.config["max_items"] then if self.data[node] then - self.data[node]:resize(self.nodes[node].config["max_items"]); + local max_items = self.nodes[node].config["max_items"]; + if max_items == "max" then + max_items = self.config.max_items; + end + self.data[node]:resize(max_items); end end -- cgit v1.2.3