Skip to content

Commit

Permalink
Redefine aliases for LimitedQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
jahfer committed Jan 3, 2025
1 parent 367745d commit 5a7595b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/async/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ def limited?
# If the queue is full, this method will block until there is space available.
#
# @parameter item [Object] The item to add to the queue.
def <<(item)
def push(item)
while limited?
@full.wait
end

super
end

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

# Add multiple items to the queue.
#
Expand Down Expand Up @@ -163,5 +166,8 @@ def dequeue

return item
end

# Compatibility with {::Queue#pop}.
alias pop dequeue
end
end
18 changes: 13 additions & 5 deletions test/async/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,21 @@ def before
queue << :item2
end

reactor.async do |task|
task.sleep 0.01
expect(queue.items).to contain_exactly :item1
queue.dequeue
expect(queue.items).to contain_exactly :item2
expect(queue.dequeue).to be == :item1
expect(queue.dequeue).to be == :item2
end

with "#pop" do
it "waits until a queue is dequeued" do
reactor.async do
queue << :item2
end

expect(queue.pop).to be == :item1
expect(queue.pop).to be == :item2
end
end
end
end
end

0 comments on commit 5a7595b

Please sign in to comment.