2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "active_support/subscriber"
|
2013-09-19 05:03:58 -04:00
|
|
|
|
|
|
|
class TestSubscriber < ActiveSupport::Subscriber
|
2014-05-09 05:16:18 -04:00
|
|
|
cattr_reader :events
|
2013-09-19 05:03:58 -04:00
|
|
|
|
|
|
|
def self.clear
|
2014-05-09 05:16:18 -04:00
|
|
|
@@events = []
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def open_party(event)
|
2014-05-09 05:16:18 -04:00
|
|
|
events << event
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
2020-09-16 13:37:24 -04:00
|
|
|
def another_open_party(event)
|
|
|
|
events << event
|
|
|
|
end
|
|
|
|
|
2013-09-19 05:03:58 -04:00
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def private_party(event)
|
|
|
|
events << event
|
|
|
|
end
|
2014-05-09 05:16:18 -04:00
|
|
|
end
|
|
|
|
|
2020-09-16 13:37:24 -04:00
|
|
|
class PartySubscriber < TestSubscriber
|
|
|
|
def another_open_party(event)
|
|
|
|
event.payload["processing_class"] = self.class
|
2019-04-04 01:27:57 -04:00
|
|
|
events << event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-09 05:16:18 -04:00
|
|
|
# Monkey patch subscriber to test that only one subscriber per method is added.
|
|
|
|
class TestSubscriber
|
2014-05-23 10:45:28 -04:00
|
|
|
remove_method :open_party
|
2021-11-15 13:51:28 -05:00
|
|
|
def open_party(event) # rubocop:disable Lint/DuplicateMethods
|
2014-05-09 05:16:18 -04:00
|
|
|
events << event
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SubscriberTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
TestSubscriber.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attaches_subscribers
|
2020-09-16 13:37:24 -04:00
|
|
|
TestSubscriber.attach_to :doodle
|
|
|
|
|
2013-09-19 05:03:58 -04:00
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
2014-05-09 05:16:18 -04:00
|
|
|
assert_equal "open_party.doodle", TestSubscriber.events.first.name
|
2020-09-16 13:37:24 -04:00
|
|
|
ensure
|
|
|
|
TestSubscriber.detach_from :doodle
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attaches_subscribers_with_inherit_all_option
|
|
|
|
PartySubscriber.attach_to :doodle, inherit_all: true
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
|
|
|
assert_equal "open_party.doodle", PartySubscriber.events.first.name
|
|
|
|
ensure
|
|
|
|
PartySubscriber.detach_from :doodle
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attaches_subscribers_with_inherit_all_option_replaces_original_behaviour
|
|
|
|
PartySubscriber.attach_to :doodle, inherit_all: true
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.instrument("another_open_party.doodle")
|
|
|
|
|
|
|
|
assert_equal 1, PartySubscriber.events.size
|
|
|
|
|
|
|
|
event = PartySubscriber.events.first
|
|
|
|
assert_equal "another_open_party.doodle", event.name
|
|
|
|
assert_equal PartySubscriber, event.payload.fetch("processing_class")
|
|
|
|
ensure
|
|
|
|
PartySubscriber.detach_from :doodle
|
2014-05-09 05:16:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_attaches_only_one_subscriber
|
2020-09-16 13:37:24 -04:00
|
|
|
TestSubscriber.attach_to :doodle
|
|
|
|
|
2014-05-09 05:16:18 -04:00
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
|
|
|
assert_equal 1, TestSubscriber.events.size
|
2020-09-16 13:37:24 -04:00
|
|
|
ensure
|
|
|
|
TestSubscriber.detach_from :doodle
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_does_not_attach_private_methods
|
2020-09-16 13:37:24 -04:00
|
|
|
TestSubscriber.attach_to :doodle
|
|
|
|
|
2013-09-19 05:03:58 -04:00
|
|
|
ActiveSupport::Notifications.instrument("private_party.doodle")
|
|
|
|
|
2014-07-31 07:56:22 -04:00
|
|
|
assert_equal [], TestSubscriber.events
|
2020-09-16 13:37:24 -04:00
|
|
|
ensure
|
|
|
|
TestSubscriber.detach_from :doodle
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|
2019-04-04 01:27:57 -04:00
|
|
|
|
|
|
|
def test_detaches_subscribers
|
2020-09-16 13:37:24 -04:00
|
|
|
TestSubscriber.attach_to :doodle
|
|
|
|
TestSubscriber.detach_from :doodle
|
|
|
|
|
2019-04-04 01:27:57 -04:00
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
2020-09-16 13:37:24 -04:00
|
|
|
assert_equal [], TestSubscriber.events
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_detaches_subscribers_from_inherited_methods
|
|
|
|
PartySubscriber.attach_to :doodle
|
|
|
|
PartySubscriber.detach_from :doodle
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.instrument("open_party.doodle")
|
|
|
|
|
|
|
|
assert_equal [], TestSubscriber.events
|
2019-04-04 01:27:57 -04:00
|
|
|
end
|
2021-03-01 11:55:47 -05:00
|
|
|
|
|
|
|
def test_supports_publish_event
|
|
|
|
TestSubscriber.attach_to :doodle
|
|
|
|
|
|
|
|
original_event = ActiveSupport::Notifications::Event.new("open_party.doodle", Time.at(0), Time.at(10), "id", { foo: "bar" })
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.publish_event(original_event)
|
|
|
|
|
|
|
|
assert_equal original_event, TestSubscriber.events.first
|
|
|
|
ensure
|
|
|
|
TestSubscriber.detach_from :doodle
|
|
|
|
end
|
2013-09-19 05:03:58 -04:00
|
|
|
end
|