Mercurial > prosody-hg
comparison spec/util_queue_spec.lua @ 9901:c8b75239846c
util.queue: Add 'consume()' convenience iterator
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 23 Mar 2019 08:47:55 +0000 |
| parents | c803624cae3d |
| children | 56e112b890ea |
comparison
equal
deleted
inserted
replaced
| 9900:f2104b36f673 | 9901:c8b75239846c |
|---|---|
| 98 end | 98 end |
| 99 end | 99 end |
| 100 | 100 |
| 101 end); | 101 end); |
| 102 end); | 102 end); |
| 103 describe("consume()", function () | |
| 104 it("should work", function () | |
| 105 local q = queue.new(10); | |
| 106 for i = 1, 5 do | |
| 107 q:push(i); | |
| 108 end | |
| 109 local c = 0; | |
| 110 for i in q:consume() do | |
| 111 assert(i == c + 1); | |
| 112 assert(q:count() == (5-i)); | |
| 113 c = i; | |
| 114 end | |
| 115 end); | |
| 116 | |
| 117 it("should work even if items are pushed in the loop", function () | |
| 118 local q = queue.new(10); | |
| 119 for i = 1, 5 do | |
| 120 q:push(i); | |
| 121 end | |
| 122 local c = 0; | |
| 123 for i in q:consume() do | |
| 124 assert(i == c + 1); | |
| 125 if c < 3 then | |
| 126 assert(q:count() == (5-i)); | |
| 127 else | |
| 128 assert(q:count() == (6-i)); | |
| 129 end | |
| 130 | |
| 131 c = i; | |
| 132 | |
| 133 if c == 3 then | |
| 134 q:push(6); | |
| 135 end | |
| 136 end | |
| 137 assert.equal(c, 6); | |
| 138 end); | |
| 139 end); | |
| 103 end); | 140 end); |
