Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #10672 from carllerche/master

Bug fix: Evented notification subscribers can handle published events
  • Loading branch information...
commit fe7e73086240a5bae4dc7cd01f8ec75ad8cddb96 2 parents 18fe96c + e539228
@spastorino spastorino authored
View
7 activesupport/lib/active_support/notifications/fanout.rb
@@ -79,6 +79,13 @@ class Evented #:nodoc:
def initialize(pattern, delegate)
@pattern = pattern
@delegate = delegate
+ @can_publish = delegate.respond_to?(:publish)
+ end
+
+ def publish(name, *args)
+ if @can_publish
+ @delegate.publish name, *args
+ end
end
def start(name, id, payload)
View
22 activesupport/test/notifications_test.rb
@@ -81,6 +81,20 @@ def event(*args)
end
end
+ class TestSubscriber
+ attr_reader :starts, :finishes, :publishes
+
+ def initialize
+ @starts = []
+ @finishes = []
+ @publishes = []
+ end
+
+ def start(*args); @starts << args; end
+ def finish(*args); @finishes << args; end
+ def publish(*args); @publishes << args; end
+ end
+
class SyncPubSubTest < TestCase
def test_events_are_published_to_a_listener
@notifier.publish :foo
@@ -144,6 +158,14 @@ def test_multiple_log_subscribers
assert_equal [[:foo]], @another
end
+ def test_publish_with_subscriber
+ subscriber = TestSubscriber.new
+ @notifier.subscribe nil, subscriber
+ @notifier.publish :foo
+
+ assert_equal [[:foo]], subscriber.publishes
+ end
+
private
def event(*args)
args
Please sign in to comment.
Something went wrong with that request. Please try again.