mod_admin_telnet: Identify native bidi sessions
[prosody.git] / spec / util_cache_spec.lua
blob15e86ee9e03fbe066e356e78c86f8a6562322d77
2 local cache = require "util.cache";
4 describe("util.cache", function()
5 describe("#new()", function()
6 it("should work", function()
8 local c = cache.new(5);
10 local function expect_kv(key, value, actual_key, actual_value)
11 assert.are.equal(key, actual_key, "key incorrect");
12 assert.are.equal(value, actual_value, "value incorrect");
13 end
15 expect_kv(nil, nil, c:head());
16 expect_kv(nil, nil, c:tail());
18 assert.are.equal(c:count(), 0);
20 c:set("one", 1)
21 assert.are.equal(c:count(), 1);
22 expect_kv("one", 1, c:head());
23 expect_kv("one", 1, c:tail());
25 c:set("two", 2)
26 expect_kv("two", 2, c:head());
27 expect_kv("one", 1, c:tail());
29 c:set("three", 3)
30 expect_kv("three", 3, c:head());
31 expect_kv("one", 1, c:tail());
33 c:set("four", 4)
34 c:set("five", 5);
35 assert.are.equal(c:count(), 5);
36 expect_kv("five", 5, c:head());
37 expect_kv("one", 1, c:tail());
39 c:set("foo", nil);
40 assert.are.equal(c:count(), 5);
41 expect_kv("five", 5, c:head());
42 expect_kv("one", 1, c:tail());
44 assert.are.equal(c:get("one"), 1);
45 expect_kv("five", 5, c:head());
46 expect_kv("one", 1, c:tail());
48 assert.are.equal(c:get("two"), 2);
49 assert.are.equal(c:get("three"), 3);
50 assert.are.equal(c:get("four"), 4);
51 assert.are.equal(c:get("five"), 5);
53 assert.are.equal(c:get("foo"), nil);
54 assert.are.equal(c:get("bar"), nil);
56 c:set("six", 6);
57 assert.are.equal(c:count(), 5);
58 expect_kv("six", 6, c:head());
59 expect_kv("two", 2, c:tail());
61 assert.are.equal(c:get("one"), nil);
62 assert.are.equal(c:get("two"), 2);
63 assert.are.equal(c:get("three"), 3);
64 assert.are.equal(c:get("four"), 4);
65 assert.are.equal(c:get("five"), 5);
66 assert.are.equal(c:get("six"), 6);
68 c:set("three", nil);
69 assert.are.equal(c:count(), 4);
71 assert.are.equal(c:get("one"), nil);
72 assert.are.equal(c:get("two"), 2);
73 assert.are.equal(c:get("three"), nil);
74 assert.are.equal(c:get("four"), 4);
75 assert.are.equal(c:get("five"), 5);
76 assert.are.equal(c:get("six"), 6);
78 c:set("seven", 7);
79 assert.are.equal(c:count(), 5);
81 assert.are.equal(c:get("one"), nil);
82 assert.are.equal(c:get("two"), 2);
83 assert.are.equal(c:get("three"), nil);
84 assert.are.equal(c:get("four"), 4);
85 assert.are.equal(c:get("five"), 5);
86 assert.are.equal(c:get("six"), 6);
87 assert.are.equal(c:get("seven"), 7);
89 c:set("eight", 8);
90 assert.are.equal(c:count(), 5);
92 assert.are.equal(c:get("one"), nil);
93 assert.are.equal(c:get("two"), nil);
94 assert.are.equal(c:get("three"), nil);
95 assert.are.equal(c:get("four"), 4);
96 assert.are.equal(c:get("five"), 5);
97 assert.are.equal(c:get("six"), 6);
98 assert.are.equal(c:get("seven"), 7);
99 assert.are.equal(c:get("eight"), 8);
101 c:set("four", 4);
102 assert.are.equal(c:count(), 5);
104 assert.are.equal(c:get("one"), nil);
105 assert.are.equal(c:get("two"), nil);
106 assert.are.equal(c:get("three"), nil);
107 assert.are.equal(c:get("four"), 4);
108 assert.are.equal(c:get("five"), 5);
109 assert.are.equal(c:get("six"), 6);
110 assert.are.equal(c:get("seven"), 7);
111 assert.are.equal(c:get("eight"), 8);
113 c:set("nine", 9);
114 assert.are.equal(c:count(), 5);
116 assert.are.equal(c:get("one"), nil);
117 assert.are.equal(c:get("two"), nil);
118 assert.are.equal(c:get("three"), nil);
119 assert.are.equal(c:get("four"), 4);
120 assert.are.equal(c:get("five"), nil);
121 assert.are.equal(c:get("six"), 6);
122 assert.are.equal(c:get("seven"), 7);
123 assert.are.equal(c:get("eight"), 8);
124 assert.are.equal(c:get("nine"), 9);
127 local keys = { "nine", "four", "eight", "seven", "six" };
128 local values = { 9, 4, 8, 7, 6 };
129 local i = 0;
130 for k, v in c:items() do
131 i = i + 1;
132 assert.are.equal(k, keys[i]);
133 assert.are.equal(v, values[i]);
135 assert.are.equal(i, 5);
137 c:set("four", "2+2");
138 assert.are.equal(c:count(), 5);
140 assert.are.equal(c:get("one"), nil);
141 assert.are.equal(c:get("two"), nil);
142 assert.are.equal(c:get("three"), nil);
143 assert.are.equal(c:get("four"), "2+2");
144 assert.are.equal(c:get("five"), nil);
145 assert.are.equal(c:get("six"), 6);
146 assert.are.equal(c:get("seven"), 7);
147 assert.are.equal(c:get("eight"), 8);
148 assert.are.equal(c:get("nine"), 9);
152 local keys = { "four", "nine", "eight", "seven", "six" };
153 local values = { "2+2", 9, 8, 7, 6 };
154 local i = 0;
155 for k, v in c:items() do
156 i = i + 1;
157 assert.are.equal(k, keys[i]);
158 assert.are.equal(v, values[i]);
160 assert.are.equal(i, 5);
162 c:set("foo", nil);
163 assert.are.equal(c:count(), 5);
165 assert.are.equal(c:get("one"), nil);
166 assert.are.equal(c:get("two"), nil);
167 assert.are.equal(c:get("three"), nil);
168 assert.are.equal(c:get("four"), "2+2");
169 assert.are.equal(c:get("five"), nil);
170 assert.are.equal(c:get("six"), 6);
171 assert.are.equal(c:get("seven"), 7);
172 assert.are.equal(c:get("eight"), 8);
173 assert.are.equal(c:get("nine"), 9);
177 local keys = { "four", "nine", "eight", "seven", "six" };
178 local values = { "2+2", 9, 8, 7, 6 };
179 local i = 0;
180 for k, v in c:items() do
181 i = i + 1;
182 assert.are.equal(k, keys[i]);
183 assert.are.equal(v, values[i]);
185 assert.are.equal(i, 5);
187 c:set("four", nil);
189 assert.are.equal(c:get("one"), nil);
190 assert.are.equal(c:get("two"), nil);
191 assert.are.equal(c:get("three"), nil);
192 assert.are.equal(c:get("four"), nil);
193 assert.are.equal(c:get("five"), nil);
194 assert.are.equal(c:get("six"), 6);
195 assert.are.equal(c:get("seven"), 7);
196 assert.are.equal(c:get("eight"), 8);
197 assert.are.equal(c:get("nine"), 9);
201 local keys = { "nine", "eight", "seven", "six" };
202 local values = { 9, 8, 7, 6 };
203 local i = 0;
204 for k, v in c:items() do
205 i = i + 1;
206 assert.are.equal(k, keys[i]);
207 assert.are.equal(v, values[i]);
209 assert.are.equal(i, 4);
213 local evicted_key, evicted_value;
214 local c2 = cache.new(3, function (_key, _value)
215 evicted_key, evicted_value = _key, _value;
216 end);
217 local function set(k, v, should_evict_key, should_evict_value)
218 evicted_key, evicted_value = nil, nil;
219 c2:set(k, v);
220 assert.are.equal(evicted_key, should_evict_key);
221 assert.are.equal(evicted_value, should_evict_value);
223 set("a", 1)
224 set("a", 1)
225 set("a", 1)
226 set("a", 1)
227 set("a", 1)
229 set("b", 2)
230 set("c", 3)
231 set("b", 2)
232 set("d", 4, "a", 1)
233 set("e", 5, "c", 3)
237 local evicted_key, evicted_value;
238 local c3 = cache.new(1, function (_key, _value)
239 evicted_key, evicted_value = _key, _value;
240 if _key == "a" then
241 -- Sanity check for what we're evicting
242 assert.are.equal(_key, "a");
243 assert.are.equal(_value, 1);
244 -- We're going to block eviction of this key/value, so set to nil...
245 evicted_key, evicted_value = nil, nil;
246 -- Returning false to block eviction
247 return false
249 end);
250 local function set(k, v, should_evict_key, should_evict_value)
251 evicted_key, evicted_value = nil, nil;
252 local ret = c3:set(k, v);
253 assert.are.equal(evicted_key, should_evict_key);
254 assert.are.equal(evicted_value, should_evict_value);
255 return ret;
257 set("a", 1)
258 set("a", 1)
259 set("a", 1)
260 set("a", 1)
261 set("a", 1)
263 -- Our on_evict prevents "a" from being evicted, causing this to fail...
264 assert.are.equal(set("b", 2), false, "Failed to prevent eviction, or signal result");
266 expect_kv("a", 1, c3:head());
267 expect_kv("a", 1, c3:tail());
269 -- Check the final state is what we expect
270 assert.are.equal(c3:get("a"), 1);
271 assert.are.equal(c3:get("b"), nil);
272 assert.are.equal(c3:count(), 1);
276 local c4 = cache.new(3, false);
278 assert.are.equal(c4:set("a", 1), true);
279 assert.are.equal(c4:set("a", 1), true);
280 assert.are.equal(c4:set("a", 1), true);
281 assert.are.equal(c4:set("a", 1), true);
282 assert.are.equal(c4:set("b", 2), true);
283 assert.are.equal(c4:set("c", 3), true);
284 assert.are.equal(c4:set("d", 4), false);
285 assert.are.equal(c4:set("d", 4), false);
286 assert.are.equal(c4:set("d", 4), false);
288 expect_kv("c", 3, c4:head());
289 expect_kv("a", 1, c4:tail());
291 local c5 = cache.new(3, function (k, v) --luacheck: ignore 212/v
292 if k == "a" then
293 return nil;
294 elseif k == "b" then
295 return true;
297 return false;
298 end);
300 assert.are.equal(c5:set("a", 1), true);
301 assert.are.equal(c5:set("a", 1), true);
302 assert.are.equal(c5:set("a", 1), true);
303 assert.are.equal(c5:set("a", 1), true);
304 assert.are.equal(c5:set("b", 2), true);
305 assert.are.equal(c5:set("c", 3), true);
306 assert.are.equal(c5:set("d", 4), true); -- "a" evicted (cb returned nil)
307 assert.are.equal(c5:set("d", 4), true); -- nop
308 assert.are.equal(c5:set("d", 4), true); -- nop
309 assert.are.equal(c5:set("e", 5), true); -- "b" evicted (cb returned true)
310 assert.are.equal(c5:set("f", 6), false); -- "c" won't evict (cb returned false)
312 expect_kv("e", 5, c5:head());
313 expect_kv("c", 3, c5:tail());
314 end);
315 end);
316 end);