Software /
code /
prosody
Annotate
spec/util_queue_spec.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
author | Martijn van Duren <martijn@openbsd.org> |
---|---|
date | Thu, 06 Feb 2025 15:04:38 +0000 |
parent | 13558:56e112b890ea |
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); |
9901
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
103 describe("consume()", function () |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
104 it("should work", function () |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
105 local q = queue.new(10); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
106 for i = 1, 5 do |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
107 q:push(i); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
108 end |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
109 local c = 0; |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
110 for i in q:consume() do |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
111 assert(i == c + 1); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
112 assert(q:count() == (5-i)); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
113 c = i; |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
114 end |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
115 end); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
116 |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
117 it("should work even if items are pushed in the loop", function () |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
118 local q = queue.new(10); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
119 for i = 1, 5 do |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
120 q:push(i); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
121 end |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
122 local c = 0; |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
123 for i in q:consume() do |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
124 assert(i == c + 1); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
125 if c < 3 then |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
126 assert(q:count() == (5-i)); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
127 else |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
128 assert(q:count() == (6-i)); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
129 end |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
130 |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
131 c = i; |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
132 |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
133 if c == 3 then |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
134 q:push(6); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
135 end |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
136 end |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
137 assert.equal(c, 6); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
138 end); |
c8b75239846c
util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents:
8240
diff
changeset
|
139 end); |
13558
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
140 describe("replace()", function () |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
141 it("should work", function () |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
142 local q = queue.new(10); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
143 for i = 1, 5 do |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
144 q:push(i); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
145 end |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
146 q:replace(6); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
147 local c = 0; |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
148 for i in q:consume() do |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
149 c = c + 1; |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
150 if c > 1 then |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
151 assert.is_equal(c, i); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
152 elseif c == 1 then |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
153 assert.is_equal(6, i); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
154 end |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
155 end |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
156 assert.is_equal(5, c); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
157 end); |
56e112b890ea
util.queue: tests: Add test for :replace() method
Matthew Wild <mwild1@gmail.com>
parents:
9901
diff
changeset
|
158 end); |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 end); |