From 58c0d31487d158286576b0745a55a0941ed076ad Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 13 Nov 2009 20:58:38 -0800 Subject: Notifications: queue.drained? for testability in place of brittle sleeps --- activesupport/lib/active_support/notifications.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index 6304f496f5..7a9f76b26a 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -144,27 +144,21 @@ module ActiveSupport class LittleFanout def initialize @listeners = [] - @stream = Queue.new - Thread.new { consume } end def publish(*args) - @stream.push(args) + @listeners.each { |l| l.publish(*args) } end def subscribe(pattern=nil, &block) @listeners << Listener.new(pattern, &block) end - def consume - while args = @stream.shift - @listeners.each { |l| l.publish(*args) } - end + def drained? + @listeners.all? &:drained? end class Listener - # attr_reader :thread - def initialize(pattern, &block) @pattern = pattern @subscriber = block @@ -183,6 +177,10 @@ module ActiveSupport @subscriber.call(*args) end end + + def drained? + @queue.size.zero? + end end end end -- cgit v1.2.3