Annotate

spec/util_queue_spec.lua @ 11157:413bd21ba449 0.11

net.websocket.frames: Read buffer length correctly in Lua 5.1 (fix #1598) COMPAT: The __len metamethod does not work with tables in Lua 5.1. Both strings and util.dbuffer now expose their length as a :len() method.
author Kim Alvefur <zash@zash.se>
date Mon, 12 Oct 2020 20:21:18 +0200
parent 8240:c803624cae3d
child 9901:c8b75239846c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local queue = require "util.queue";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 describe("util.queue", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 describe("#new()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local q = queue.new(10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 assert.are.equal(q.size, 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 assert.are.equal(q:count(), 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 assert.is_true(q:push("one"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 assert.is_true(q:push("two"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 assert.is_true(q:push("three"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 for i = 4, 10 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 assert.are.equal(q:count(), i, "count is not "..i.."("..q:count()..")");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 assert.are.equal(q:pop(), "one", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 assert.are.equal(q:pop(), "two", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 assert.are.equal(q:pop(), "three", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 assert.are.equal(q:count(), 10, "queue count incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 for _ = 1, 10 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 assert.are.equal(q:pop(), "hello", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 assert.are.equal(q:count(), 0, "queue count incorrect");
8240
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
40 assert.are.equal(q:pop(), nil, "empty queue pops non-nil result");
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
41 assert.are.equal(q:count(), 0, "popping empty queue affects count");
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
42
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
43 assert.are.equal(q:peek(), nil, "empty queue peeks non-nil result");
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
44 assert.are.equal(q:count(), 0, "peeking empty queue affects count");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 assert.is_true(q:push(1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 for i = 1, 1001 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 assert.are.equal(q:pop(), i);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 assert.are.equal(q:count(), 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 assert.is_true(q:push(i+1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 assert.are.equal(q:count(), 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 assert.are.equal(q:pop(), 1002);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 assert.is_true(q:push(1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 for i = 1, 1000 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 assert.are.equal(q:pop(), i);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 assert.is_true(q:push(i+1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 assert.are.equal(q:pop(), 1001);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 assert.are.equal(q:count(), 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 -- Test queues that purge old items when pushing to a full queue
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 local q = queue.new(10, true);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 for i = 1, 10 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 q:push(i);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 assert.are.equal(q:count(), 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 assert.is_true(q:push(11));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 assert.are.equal(q:count(), 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 assert.are.equal(q:pop(), 2); -- First item should have been purged
8240
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
76 assert.are.equal(q:peek(), 3);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 for i = 12, 32 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 assert.is_true(q:push(i));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 assert.are.equal(q:count(), 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 assert.are.equal(q:pop(), 23);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85
8240
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
86 do
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
87 -- Test iterator
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
88 local q = queue.new(10, true);
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
89
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
90 for i = 1, 10 do
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
91 q:push(i);
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
92 end
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
93
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
94 local i = 0;
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
95 for item in q:items() do
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
96 i = i + 1;
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
97 assert.are.equal(item, i, "unexpected item returned by iterator")
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
98 end
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
99 end
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
100
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 end);