aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-12-14 19:58:53 +0100
committerKim Alvefur <zash@zash.se>2021-12-14 19:58:53 +0100
commit93a9ed262065d9966643ad5ae50b58142a1c6823 (patch)
tree0fbcc8578d3aefac8a5021d36a86c5ae2aa78950 /spec
parentbdce99b7dd4411a8717798874a320bd2fc191b57 (diff)
downloadprosody-93a9ed262065d9966643ad5ae50b58142a1c6823.tar.gz
prosody-93a9ed262065d9966643ad5ae50b58142a1c6823.zip
util.smqueue: Abstract queue with acknowledgements and overflow
Meant to be used in mod_smacks for XEP-0198 Meant to have a larger virtual size than actual number of items stored, on the theory that in most cases, the excess will be acked before needed for a resumption event.
Diffstat (limited to 'spec')
-rw-r--r--spec/util_smqueue_spec.lua55
1 files changed, 55 insertions, 0 deletions
diff --git a/spec/util_smqueue_spec.lua b/spec/util_smqueue_spec.lua
new file mode 100644
index 00000000..991a656b
--- /dev/null
+++ b/spec/util_smqueue_spec.lua
@@ -0,0 +1,55 @@
+describe("util.smqueue", function()
+
+ local smqueue
+ setup(function() smqueue = require "util.smqueue"; end)
+
+ describe("#new()", function()
+ it("should work", function()
+ local q = smqueue.new(10);
+ assert.truthy(q);
+ end)
+ end)
+
+ describe("#push()", function()
+ it("should allow pushing many items", function()
+ local q = smqueue.new(10);
+ for i = 1, 20 do q:push(i); end
+ assert.equal(20, q:count_unacked());
+ end)
+ end)
+
+ describe("#resumable()", function()
+ it("returns true while the queue is small", function()
+ local q = smqueue.new(10);
+ for i = 1, 10 do q:push(i); end
+ assert.truthy(q:resumable());
+ q:push(11);
+ assert.falsy(q:resumable());
+ end)
+ end)
+
+ describe("#ack", function()
+ it("allows removing items", function()
+ local q = smqueue.new(10);
+ for i = 1, 10 do q:push(i); end
+ assert.same({ 1; 2; 3 }, q:ack(3));
+ assert.same({ 4; 5; 6 }, q:ack(6));
+ assert.falsy(q:ack(3), "can't go backwards")
+ assert.falsy(q:ack(100), "can't ack too many")
+ for i = 11, 20 do q:push(i); end
+ assert.same({ 11; 12 }, q:ack(12), "items are dropped");
+ end)
+ end)
+
+ describe("#resume", function()
+ it("iterates over current items", function()
+ local q = smqueue.new(10);
+ for i = 1, 12 do q:push(i); end
+ assert.same({ 3; 4; 5; 6 }, q:ack(6));
+ assert.truthy(q:resumable());
+ local resume = {}
+ for _, i in q:resume() do resume[i] = true end
+ assert.same({ [7] = true; [8] = true; [9] = true; [10] = true; [11] = true; [12] = true }, resume);
+ end)
+ end)
+end);