Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Notifications: queue.drained? for testability in place of brittle sleeps

  • Loading branch information...
commit 58c0d31487d158286576b0745a55a0941ed076ad 1 parent 66fda6b
@jeremy jeremy authored
View
16 activesupport/lib/active_support/notifications.rb
@@ -144,27 +144,21 @@ def parent_of?(event)
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 @@ def consume
@subscriber.call(*args)
end
end
+
+ def drained?
+ @queue.size.zero?
+ end
end
end
end
View
24 activesupport/test/notifications_test.rb
@@ -75,7 +75,7 @@ def test_events_are_published_to_a_listener
1 + 1
end
- sleep 1
+ drain
assert_equal 1, @events.size
assert_equal :awesome, @events.last.name
@@ -88,19 +88,18 @@ def test_nested_events_can_be_instrumented
1 + 1
end
- sleep 1
+ drain
assert_equal 1, @events.size
assert_equal :wot, @events.first.name
assert_equal Hash[:payload => "child"], @events.first.payload
end
- sleep 1
+ drain
assert_equal 2, @events.size
assert_equal :awesome, @events.last.name
assert_equal Hash[:payload => "notifications"], @events.last.payload
- assert_in_delta 1000, @events.last.duration, 70
end
def test_event_is_pushed_even_if_block_fails
@@ -108,7 +107,7 @@ def test_event_is_pushed_even_if_block_fails
raise "OMG"
end rescue RuntimeError
- sleep 1
+ drain
assert_equal 1, @events.size
assert_equal :awesome, @events.last.name
@@ -117,7 +116,7 @@ def test_event_is_pushed_even_if_block_fails
def test_event_is_pushed_even_without_block
ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications")
- sleep 1
+ drain
assert_equal 1, @events.size
assert_equal :awesome, @events.last.name
@@ -137,7 +136,7 @@ def test_subscribed_in_a_transaction
end
ActiveSupport::Notifications.instrument(:cache){ 1 }
- sleep 1
+ drain
assert_equal 3, @another.size
before, during, after = @another.map {|e| e.transaction_id }
@@ -154,7 +153,7 @@ def test_subscriber_with_pattern
ActiveSupport::Notifications.instrument(:cache){ 1 }
- sleep 1
+ drain
assert_equal 1, @another.size
assert_equal :cache, @another.first.name
@@ -170,7 +169,7 @@ def test_subscriber_with_pattern_as_regexp
ActiveSupport::Notifications.instrument(:something){ 0 }
ActiveSupport::Notifications.instrument(:cache){ 1 }
- sleep 1
+ drain
assert_equal 1, @another.size
assert_equal :cache, @another.first.name
@@ -187,7 +186,7 @@ def test_with_several_consumers_and_several_events
ActiveSupport::Notifications.instrument(:value){ i }
end
- sleep 1
+ drain
assert_equal 100, @events.size
assert_equal :value, @events.first.name
@@ -199,4 +198,9 @@ def test_with_several_consumers_and_several_events
assert_equal 1, @another.first.result
assert_equal 100, @another.last.result
end
+
+ private
+ def drain
+ sleep(0.1) until ActiveSupport::Notifications.queue.drained?
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.