Mercurial > prosody-hg
annotate spec/util_smqueue_spec.lua @ 14221:565debd9c37a
Merge 13.0->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 12 Jun 2026 13:05:10 +0100 |
| parents | 73903352de86 |
| children |
| rev | line source |
|---|---|
|
12055
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 describe("util.smqueue", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local smqueue |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 setup(function() smqueue = require "util.smqueue"; end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 describe("#new()", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 it("should work", function() |
| 12778 | 8 assert.has_error(function () smqueue.new(-1) end); |
| 9 assert.has_error(function () smqueue.new(0) end); | |
| 10 assert.not_has_error(function () smqueue.new(1) end); | |
|
12055
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local q = smqueue.new(10); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 assert.truthy(q); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 describe("#push()", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 it("should allow pushing many items", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local q = smqueue.new(10); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 for i = 1, 20 do q:push(i); end |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 assert.equal(20, q:count_unacked()); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 describe("#resumable()", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 it("returns true while the queue is small", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local q = smqueue.new(10); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 for i = 1, 10 do q:push(i); end |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 assert.truthy(q:resumable()); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 q:push(11); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 assert.falsy(q:resumable()); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 describe("#ack", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 it("allows removing items", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local q = smqueue.new(10); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 for i = 1, 10 do q:push(i); end |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 assert.same({ 1; 2; 3 }, q:ack(3)); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 assert.same({ 4; 5; 6 }, q:ack(6)); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 assert.falsy(q:ack(3), "can't go backwards") |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 assert.falsy(q:ack(100), "can't ack too many") |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 for i = 11, 20 do q:push(i); end |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 assert.same({ 11; 12 }, q:ack(12), "items are dropped"); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 describe("#resume", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 it("iterates over current items", function() |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 local q = smqueue.new(10); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 for i = 1, 12 do q:push(i); end |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 assert.same({ 3; 4; 5; 6 }, q:ack(6)); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 assert.truthy(q:resumable()); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 local resume = {} |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 for _, i in q:resume() do resume[i] = true end |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 assert.same({ [7] = true; [8] = true; [9] = true; [10] = true; [11] = true; [12] = true }, resume); |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end) |
|
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 end) |
|
12058
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
58 |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
59 describe("#table", function () |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
60 it("produces a compat layer", function () |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
61 local q = smqueue.new(10); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
62 for i = 1,10 do q:push(i); end |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
63 do |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
64 local t = q:table(); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
65 assert.same({ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 }, t); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
66 end |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
67 do |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
68 for i = 11,20 do q:push(i); end |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
69 local t = q:table(); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
70 assert.same({ 11; 12; 13; 14; 15; 16; 17; 18; 19; 20 }, t); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
71 end |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
72 do |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
73 q:ack(15); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
74 local t = q:table(); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
75 assert.same({ 16; 17; 18; 19; 20 }, t); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
76 end |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
77 do |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
78 q:ack(20); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
79 local t = q:table(); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
80 assert.same({}, t); |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
81 end |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
82 end) |
|
4860da718e87
util.smqueue: Simplify compat table, fix dependent modules (thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
12055
diff
changeset
|
83 end) |
|
14198
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
84 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
85 describe("#add_checkpoint", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
86 it("fires the callback when ack reaches the checkpoint", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
87 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
88 for i = 1, 4 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
89 local called = false; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
90 q:add_checkpoint(function() called = true; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
91 assert.falsy(called, "should not fire before ack"); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
92 q:ack(4); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
93 assert.truthy(called, "should fire when ack reaches checkpoint"); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
94 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
95 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
96 it("fires when ack exceeds the checkpoint", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
97 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
98 for i = 1, 4 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
99 local called = false; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
100 q:add_checkpoint(function() called = true; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
101 for i = 5, 8 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
102 q:ack(7); -- acks past the checkpoint at 4 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
103 assert.truthy(called); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
104 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
105 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
106 it("does not fire for acks below the checkpoint threshold", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
107 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
108 for i = 1, 5 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
109 local called = false; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
110 q:add_checkpoint(function() called = true; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
111 q:ack(4); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
112 assert.falsy(called, "should not fire when ack is below checkpoint"); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
113 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
114 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
115 it("fires the callback with optional data argument", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
116 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
117 for i = 1, 4 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
118 local received_data; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
119 q:add_checkpoint(function(d) received_data = d; end, "hello"); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
120 q:ack(4); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
121 assert.equal("hello", received_data); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
122 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
123 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
124 it("fires multiple checkpoints in a single ack call", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
125 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
126 for i = 1, 3 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
127 local fired = {}; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
128 q:add_checkpoint(function() fired[#fired+1] = "first"; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
129 q:push(4); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
130 q:add_checkpoint(function() fired[#fired+1] = "second-1"; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
131 q:add_checkpoint(function() fired[#fired+1] = "second-2"; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
132 q:push(5); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
133 q:add_checkpoint(function() fired[#fired+1] = "third"; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
134 q:ack(5); -- should call all checkpoints, in order |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
135 assert.same({ "first", "second-1", "second-2", "third" }, fired); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
136 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
137 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
138 it("fires checkpoints in order across multiple acks", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
139 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
140 for i = 1, 3 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
141 local order = {}; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
142 q:add_checkpoint(function() order[#order+1] = 1; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
143 q:push(4); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
144 q:add_checkpoint(function() order[#order+1] = 2; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
145 q:push(5); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
146 q:add_checkpoint(function() order[#order+1] = 3; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
147 q:ack(3); -- fires only first |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
148 assert.same({ 1 }, order); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
149 q:ack(5); -- fires second and third |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
150 assert.same({ 1, 2, 3 }, order); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
151 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
152 |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
153 it("fires each checkpoint exactly once", function() |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
154 local q = smqueue.new(10); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
155 for i = 1, 4 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
156 local count = 0; |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
157 q:add_checkpoint(function() count = count + 1; end); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
158 q:ack(4); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
159 q:ack(4); -- duplicate ack |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
160 for i = 5, 8 do q:push(i); end |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
161 q:ack(8); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
162 assert.equal(1, count); |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
163 end) |
|
73903352de86
util.smqueue: Refactor checkpoint logic and add tests
Matthew Wild <mwild1@gmail.com>
parents:
12778
diff
changeset
|
164 end) |
|
12055
daced16154fa
util.smqueue: Abstract queue with acknowledgements and overflow
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 end); |
