mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Bug fix: Evented notification subscribers can handle published events
This commit is contained in:
parent
18fe96cc16
commit
e539228d08
2 changed files with 29 additions and 0 deletions
|
@ -79,6 +79,13 @@ module ActiveSupport
|
|||
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)
|
||||
|
|
|
@ -81,6 +81,20 @@ module Notifications
|
|||
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 @@ module Notifications
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue