Skip to content

Commit afe1af1

Browse files
100% test coverage.
1 parent f73fa2c commit afe1af1

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

fixtures/async/a_queue.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ module Async
130130
expect(queue.wait).to be == :item
131131
end
132132
end
133+
134+
with "#close" do
135+
it "signals waiting tasks when closed" do
136+
waiting_task = reactor.async do
137+
queue.dequeue
138+
end
139+
140+
queue.close
141+
142+
waiting_task.wait
143+
expect(waiting_task).to be(:finished?)
144+
end
145+
end
133146

134147
with "an empty queue" do
135148
it "is expected to be empty" do
@@ -175,6 +188,42 @@ module Async
175188
expect(count).to be == repeats
176189
end
177190
end
191+
192+
with "a closed queue" do
193+
before do
194+
queue.close
195+
end
196+
197+
it "prevents push after close" do
198+
expect{queue.push(:item)}.to raise_exception(Async::Queue::ClosedError)
199+
end
200+
201+
it "prevents enqueue after close" do
202+
expect{queue.enqueue(:item)}.to raise_exception(Async::Queue::ClosedError)
203+
end
204+
205+
it "prevents << after close" do
206+
expect{queue << :item}.to raise_exception(Async::Queue::ClosedError)
207+
end
208+
209+
it "returns nil from dequeue when closed and empty" do
210+
expect(queue.dequeue).to be_nil
211+
end
212+
213+
it "returns nil from pop when closed and empty" do
214+
expect(queue.pop).to be_nil
215+
end
216+
217+
it "signals waiting tasks when closed" do
218+
waiting_task = reactor.async do
219+
queue.dequeue
220+
end
221+
222+
# Already closed, so just check if the task finishes:
223+
waiting_task.wait
224+
expect(waiting_task).to be(:finished?)
225+
end
226+
end
178227

179228
it_behaves_like Async::ChainableAsync do
180229
def before

lib/async/queue.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def empty?
5555

5656
# Add an item to the queue.
5757
def push(item)
58+
if @closed
59+
raise ClosedError, "Cannot push items to a closed queue."
60+
end
61+
5862
@items << item
5963

6064
@available.signal unless self.empty?
@@ -153,7 +157,7 @@ def close
153157

154158
# @returns [Boolean] Whether trying to enqueue an item would block.
155159
def limited?
156-
@items.size >= @limit
160+
!@closed && @items.size >= @limit
157161
end
158162

159163
# Add an item to the queue.

test/async/limited_queue.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
end
4646

4747
10.times do
48-
item = queue.dequeue
48+
queue.dequeue
4949
total_dequeued += 1
5050

5151
expect(total_resumed).to be == total_dequeued
@@ -83,5 +83,21 @@ def before
8383
end
8484
end
8585
end
86+
87+
with "#close" do
88+
it "signals tasks waiting to enqueue items when closed" do
89+
queue.enqueue(:item1)
90+
91+
# This task will block as the queue is full:
92+
waiting_task = reactor.async do
93+
queue.enqueue(:item2)
94+
end
95+
96+
queue.close
97+
98+
waiting_task.wait
99+
expect(waiting_task).to be(:finished?)
100+
end
101+
end
86102
end
87103

0 commit comments

Comments
 (0)