diff options
Diffstat (limited to 'activesupport/test/notifications_test.rb')
-rw-r--r-- | activesupport/test/notifications_test.rb | 127 |
1 files changed, 92 insertions, 35 deletions
diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 1cb17e6197..bb20d26a25 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -1,5 +1,7 @@ -require 'abstract_unit' -require 'active_support/core_ext/module/delegation' +# frozen_string_literal: true + +require "abstract_unit" +require "active_support/core_ext/module/delegation" module Notifications class TestCase < ActiveSupport::TestCase @@ -24,6 +26,42 @@ module Notifications end end + class SubscribeEventObjectsTest < TestCase + def test_subscribe_events + events = [] + @notifier.subscribe do |event| + events << event + end + + ActiveSupport::Notifications.instrument("foo") + event = events.first + assert event, "should have an event" + assert_operator event.allocations, :>, 0 + assert_operator event.cpu_time, :>, 0 + assert_operator event.idle_time, :>, 0 + assert_operator event.duration, :>, 0 + end + + def test_subscribe_via_top_level_api + old_notifier = ActiveSupport::Notifications.notifier + ActiveSupport::Notifications.notifier = ActiveSupport::Notifications::Fanout.new + + event = nil + ActiveSupport::Notifications.subscribe("foo") do |e| + event = e + end + + ActiveSupport::Notifications.instrument("foo") do + 100.times { Object.new } # allocate at least 100 objects + end + + assert event + assert_operator event.allocations, :>=, 100 + ensure + ActiveSupport::Notifications.notifier = old_notifier + end + end + class SubscribedTest < TestCase def test_subscribed name = "foo" @@ -31,7 +69,7 @@ module Notifications expected = [name, name] events = [] - callback = lambda {|*_| events << _.first} + callback = lambda { |*_| events << _.first } ActiveSupport::Notifications.subscribed(callback, name) do ActiveSupport::Notifications.instrument(name) ActiveSupport::Notifications.instrument(name2) @@ -43,16 +81,16 @@ module Notifications assert_equal expected, events end - def test_subsribing_to_instrumentation_while_inside_it + 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 old_notifier = ActiveSupport::Notifications.notifier ActiveSupport::Notifications.notifier = ActiveSupport::Notifications::Fanout.new - ActiveSupport::Notifications.subscribe('foo', TestSubscriber.new) + ActiveSupport::Notifications.subscribe("foo", TestSubscriber.new) - ActiveSupport::Notifications.instrument('foo') do - ActiveSupport::Notifications.subscribe('foo') {} + ActiveSupport::Notifications.instrument("foo") do + ActiveSupport::Notifications.subscribe("foo") { } end ensure ActiveSupport::Notifications.notifier = old_notifier @@ -90,6 +128,25 @@ module Notifications assert_equal [["named.subscription", :foo], ["named.subscription", :foo]], @events end + def test_unsubscribing_by_name_leaves_regexp_matched_subscriptions + @matched_events = [] + @notifier.subscribe(/subscription/) { |*args| @matched_events << event(*args) } + @notifier.publish("named.subscription", :before) + @notifier.wait + [@events, @named_events, @matched_events].each do |collector| + assert_includes(collector, ["named.subscription", :before]) + end + @notifier.unsubscribe("named.subscription") + @notifier.publish("named.subscription", :after) + @notifier.publish("other.subscription", :after) + @notifier.wait + assert_includes(@events, ["named.subscription", :after]) + assert_includes(@events, ["other.subscription", :after]) + assert_includes(@matched_events, ["other.subscription", :after]) + assert_not_includes(@matched_events, ["named.subscription", :after]) + assert_not_includes(@named_events, ["named.subscription", :after]) + end + private def event(*args) args @@ -141,26 +198,26 @@ module Notifications def test_log_subscriber_with_string events = [] - @notifier.subscribe('1') { |*args| events << args } + @notifier.subscribe("1") { |*args| events << args } - @notifier.publish '1' - @notifier.publish '1.a' - @notifier.publish 'a.1' + @notifier.publish "1" + @notifier.publish "1.a" + @notifier.publish "a.1" @notifier.wait - assert_equal [['1']], events + assert_equal [["1"]], events end def test_log_subscriber_with_pattern events = [] @notifier.subscribe(/\d/) { |*args| events << args } - @notifier.publish '1' - @notifier.publish 'a.1' - @notifier.publish '1.a' + @notifier.publish "1" + @notifier.publish "a.1" + @notifier.publish "1.a" @notifier.wait - assert_equal [['1'], ['a.1'], ['1.a']], events + assert_equal [["1"], ["a.1"], ["1.a"]], events end def test_multiple_log_subscribers @@ -188,7 +245,7 @@ module Notifications end class InstrumentationTest < TestCase - delegate :instrument, :to => ActiveSupport::Notifications + delegate :instrument, to: ActiveSupport::Notifications def test_instrument_returns_block_result assert_equal 2, instrument(:awesome) { 1 + 1 } @@ -198,7 +255,7 @@ module Notifications assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } assert_equal 1, @events.size assert_equal :awesome, @events.first.name - assert_equal Hash[:result => 2], @events.first.payload + assert_equal Hash[result: 2], @events.first.payload end def test_instrumenter_exposes_its_id @@ -206,24 +263,24 @@ module Notifications end def test_nested_events_can_be_instrumented - instrument(:awesome, :payload => "notifications") do - instrument(:wot, :payload => "child") do + instrument(:awesome, payload: "notifications") do + instrument(:wot, payload: "child") do 1 + 1 end assert_equal 1, @events.size assert_equal :wot, @events.first.name - assert_equal Hash[:payload => "child"], @events.first.payload + assert_equal Hash[payload: "child"], @events.first.payload end assert_equal 2, @events.size assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "notifications"], @events.last.payload + assert_equal Hash[payload: "notifications"], @events.last.payload end def test_instrument_publishes_when_exception_is_raised begin - instrument(:awesome, :payload => "notifications") do + instrument(:awesome, payload: "notifications") do raise "FAIL" end rescue RuntimeError => e @@ -231,15 +288,15 @@ module Notifications end assert_equal 1, @events.size - assert_equal Hash[:payload => "notifications", - :exception => ["RuntimeError", "FAIL"], :exception_object => e], @events.last.payload + assert_equal Hash[payload: "notifications", + exception: ["RuntimeError", "FAIL"], exception_object: e], @events.last.payload end def test_event_is_pushed_even_without_block - instrument(:awesome, :payload => "notifications") + instrument(:awesome, payload: "notifications") assert_equal 1, @events.size assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "notifications"], @events.last.payload + assert_equal Hash[payload: "notifications"], @events.last.payload end end @@ -248,14 +305,14 @@ module Notifications time = Time.now event = event(:foo, time, time + 0.01, random_id, {}) - assert_equal :foo, event.name - assert_equal time, event.time + assert_equal :foo, event.name + assert_equal time, event.time assert_in_delta 10.0, event.duration, 0.00001 end def test_events_consumes_information_given_as_payload - event = event(:foo, Time.now, Time.now + 1, random_id, :payload => :bar) - assert_equal Hash[:payload => :bar], event.payload + event = event(:foo, Time.now, Time.now + 1, random_id, payload: :bar) + assert_equal Hash[payload: :bar], event.payload end def test_event_is_parent_based_on_children @@ -268,12 +325,12 @@ module Notifications parent.children << child assert parent.parent_of?(child) - assert !child.parent_of?(parent) - assert !parent.parent_of?(not_child) - assert !not_child.parent_of?(parent) + assert_not child.parent_of?(parent) + assert_not parent.parent_of?(not_child) + assert_not not_child.parent_of?(parent) end - protected + private def random_id @random_id ||= SecureRandom.hex(10) end |