Skip to content
Browse files

Add some queue backfill inspection to EM::Queue and EM::Deferrable::P…

…ool for resource management processes
  • Loading branch information...
1 parent d9a23e4 commit 3d47cd2ebd519d1135babc5b53a48aa5724ec7dc @raggi raggi committed Jul 15, 2011
Showing with 32 additions and 1 deletion.
  1. +5 −0 lib/em/deferrable/pool.rb
  2. +8 −1 lib/em/queue.rb
  3. +11 −0 tests/test_deferrable_pool.rb
  4. +8 −0 tests/test_queue.rb
View
5 lib/em/deferrable/pool.rb
@@ -99,6 +99,11 @@ def perform(*a, &b)
end
alias reschedule perform
+ # A peek at the number of enqueued jobs waiting for resources
+ def num_waiting
+ @resources.num_waiting
+ end
+
# Removed will show resources in a partial pruned state. Resources in the
# removed list may not appear in the contents list if they are currently in
# use.
View
9 lib/em/queue.rb
@@ -59,6 +59,13 @@ def empty?
# @note This is a peek, it's not thread safe, and may only tend toward accuracy.
def size
@items.size
- end # size
+ end
+
+ # @return [Integer] Waiting size
+ # @note This is a peek at the number of jobs that are currently waiting on the Queue
+ def num_waiting
+ @popq.size
+ end
+
end # Queue
end # EventMachine
View
11 tests/test_deferrable_pool.rb
@@ -114,4 +114,15 @@ def test_contents
pool.contents.delete(:res)
assert_equal [:res], pool.contents
end
+
+ def test_num_waiting
+ pool.add :res
+ assert_equal 0, pool.num_waiting
+ pool.perform { |r| EM::DefaultDeferrable.new }
+ assert_equal 0, pool.num_waiting
+ 10.times { pool.perform { |r| EM::DefaultDeferrable.new } }
+ EM.run { EM.next_tick { EM.stop } }
+ assert_equal 10, pool.num_waiting
+ end
+
end
View
8 tests/test_queue.rb
@@ -39,4 +39,12 @@ def test_queue_reactor_thread
EM.run { EM.next_tick { EM.stop } }
assert_equal 1, x
end
+
+ def test_num_waiting
+ q = EM::Queue.new
+ many = 3
+ many.times { q.pop {} }
+ EM.run { EM.next_tick { EM.stop } }
+ assert_equal many, q.num_waiting
+ end
end

0 comments on commit 3d47cd2

Please sign in to comment.
Something went wrong with that request. Please try again.