Skip to content

Commit

Permalink
Add Queue#push and Queue#pop for compatibility with ::Queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 5, 2024
1 parent 6ecace9 commit 4f2304b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/async/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ def empty?
end

# Add an item to the queue.
def <<(item)
def push(item)
@items << item

@available.signal unless self.empty?
end

# Compatibility with {::Queue#push}.
alias << push

# Add multiple items to the queue.
def enqueue(*items)
@items.concat(items)
Expand All @@ -60,6 +63,9 @@ def dequeue
@items.shift
end

# Compatibility with {::Queue#pop}.
alias pop dequeue

# Process each item in the queue.
#
# @asynchronous Executes the given block concurrently for each item.
Expand Down
16 changes: 16 additions & 0 deletions test/async/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
AQueue = Sus::Shared("a queue") do
let(:queue) {subject.new}

with "#push" do
it "adds an item to the queue" do
queue.push(:item)
expect(queue.size).to be == 1
expect(queue.dequeue).to be == :item
end
end

with "#pop" do
it "removes an item from the queue" do
queue.push(:item)
expect(queue.pop).to be == :item
expect(queue.size).to be == 0
end
end

with '#each' do
it 'can enumerate queue items' do
reactor.async do |task|
Expand Down

0 comments on commit 4f2304b

Please sign in to comment.