diff options
Diffstat (limited to 'activesupport/test/notifications_test.rb')
-rw-r--r-- | activesupport/test/notifications_test.rb | 109 |
1 files changed, 107 insertions, 2 deletions
diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index d3c660a014..08277e5436 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -20,7 +20,6 @@ module Notifications end private - def event(*args) ActiveSupport::Notifications::Event.new(*args) end @@ -42,6 +41,27 @@ module Notifications assert_operator event.duration, :>, 0 end + def test_subscribe_to_events_where_payload_is_changed_during_instrumentation + @notifier.subscribe do |event| + assert_equal "success!", event.payload[:my_key] + end + + ActiveSupport::Notifications.instrument("foo") do |payload| + payload[:my_key] = "success!" + end + end + + def test_subscribe_to_events_can_handle_nested_hashes_in_the_paylaod + @notifier.subscribe do |event| + assert_equal "success!", event.payload[:some_key][:key_one] + assert_equal "great_success!", event.payload[:some_key][:key_two] + end + + ActiveSupport::Notifications.instrument("foo", some_key: { key_one: "success!" }) do |payload| + payload[:some_key][:key_two] = "great_success!" + end + end + def test_subscribe_via_top_level_api old_notifier = ActiveSupport::Notifications.notifier ActiveSupport::Notifications.notifier = ActiveSupport::Notifications::Fanout.new @@ -62,6 +82,38 @@ module Notifications end end + class TimedAndMonotonicTimedSubscriberTest < TestCase + def test_subscribe + event_name = "foo" + class_of_started = nil + class_of_finished = nil + + ActiveSupport::Notifications.subscribe(event_name) do |name, started, finished, unique_id, data| + class_of_started = started.class + class_of_finished = finished.class + end + + ActiveSupport::Notifications.instrument(event_name) + + assert_equal [Time, Time], [class_of_started, class_of_finished] + end + + def test_monotonic_subscribe + event_name = "foo" + class_of_started = nil + class_of_finished = nil + + ActiveSupport::Notifications.monotonic_subscribe(event_name) do |name, started, finished, unique_id, data| + class_of_started = started.class + class_of_finished = finished.class + end + + ActiveSupport::Notifications.instrument(event_name) + + assert_equal [Float, Float], [class_of_started, class_of_finished] + end + end + class SubscribedTest < TestCase def test_subscribed name = "foo" @@ -81,6 +133,24 @@ module Notifications assert_equal expected, events end + def test_subscribed_all_messages + name = "foo" + name2 = name * 2 + expected = [name, name2, name] + + events = [] + callback = lambda { |*_| events << _.first } + ActiveSupport::Notifications.subscribed(callback) do + ActiveSupport::Notifications.instrument(name) + ActiveSupport::Notifications.instrument(name2) + ActiveSupport::Notifications.instrument(name) + end + assert_equal expected, events + + ActiveSupport::Notifications.instrument(name) + assert_equal expected, events + end + def test_subscribing_to_instrumentation_while_inside_it # the repro requires that there are no evented subscribers for the "foo" event, # so we have to duplicate some of the setup code @@ -95,6 +165,42 @@ module Notifications ensure ActiveSupport::Notifications.notifier = old_notifier end + + def test_timed_subscribed + event_name = "foo" + class_of_started = nil + class_of_finished = nil + callback = lambda do |name, started, finished, unique_id, data| + class_of_started = started.class + class_of_finished = finished.class + end + + ActiveSupport::Notifications.subscribed(callback, event_name) do + ActiveSupport::Notifications.instrument(event_name) + end + + ActiveSupport::Notifications.instrument(event_name) + + assert_equal [Time, Time], [class_of_started, class_of_finished] + end + + def test_monotonic_timed_subscribed + event_name = "foo" + class_of_started = nil + class_of_finished = nil + callback = lambda do |name, started, finished, unique_id, data| + class_of_started = started.class + class_of_finished = finished.class + end + + ActiveSupport::Notifications.subscribed(callback, event_name, monotonic: true) do + ActiveSupport::Notifications.instrument(event_name) + end + + ActiveSupport::Notifications.instrument(event_name) + + assert_equal [Float, Float], [class_of_started, class_of_finished] + end end class UnsubscribeTest < TestCase @@ -317,7 +423,6 @@ module Notifications assert_equal 0, event.cpu_time end - def test_events_consumes_information_given_as_payload event = event(:foo, Concurrent.monotonic_time, Concurrent.monotonic_time + 1, random_id, payload: :bar) assert_equal Hash[payload: :bar], event.payload |