2017-07-09 12:06:36 +00:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 13:39:13 +00:00
|
|
|
|
2018-09-30 00:50:43 +00:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 16:03:25 +00:00
|
|
|
require "active_support/core_ext/module/delegation"
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
module Notifications
|
|
|
|
class TestCase < ActiveSupport::TestCase
|
|
|
|
def setup
|
2011-02-09 22:02:38 +00:00
|
|
|
@old_notifier = ActiveSupport::Notifications.notifier
|
|
|
|
@notifier = ActiveSupport::Notifications::Fanout.new
|
|
|
|
ActiveSupport::Notifications.notifier = @notifier
|
2009-11-28 20:49:07 +00:00
|
|
|
@events = []
|
2010-04-25 02:12:02 +00:00
|
|
|
@named_events = []
|
2010-03-02 00:01:08 +00:00
|
|
|
@subscription = @notifier.subscribe { |*args| @events << event(*args) }
|
2010-04-25 02:12:02 +00:00
|
|
|
@named_subscription = @notifier.subscribe("named.subscription") { |*args| @named_events << event(*args) }
|
2009-11-28 20:49:07 +00:00
|
|
|
end
|
2009-09-30 11:59:15 +00:00
|
|
|
|
2011-02-09 22:02:38 +00:00
|
|
|
def teardown
|
|
|
|
ActiveSupport::Notifications.notifier = @old_notifier
|
|
|
|
end
|
|
|
|
|
2010-07-20 15:07:18 +00:00
|
|
|
private
|
|
|
|
def event(*args)
|
|
|
|
ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
2009-10-15 17:49:29 +00:00
|
|
|
|
2018-12-17 08:25:55 +00:00
|
|
|
class SubscribeEventObjectsTest < TestCase
|
2018-07-26 19:03:31 +00:00
|
|
|
def test_subscribe_events
|
|
|
|
events = []
|
2018-07-26 19:15:41 +00:00
|
|
|
@notifier.subscribe do |event|
|
2018-07-26 19:03:31 +00:00
|
|
|
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
|
2023-01-28 18:10:46 +00:00
|
|
|
assert_operator event.idle_time, :>=, 0
|
2018-07-26 19:03:31 +00:00
|
|
|
assert_operator event.duration, :>, 0
|
|
|
|
end
|
2018-07-26 19:05:23 +00:00
|
|
|
|
2019-05-21 21:07:27 +00:00
|
|
|
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
|
|
|
|
|
2018-07-26 19:15:41 +00:00
|
|
|
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
|
2018-07-26 19:05:23 +00:00
|
|
|
end
|
|
|
|
|
2018-07-26 19:15:41 +00:00
|
|
|
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
|
2018-07-26 19:05:23 +00:00
|
|
|
end
|
2020-11-10 03:26:21 +00:00
|
|
|
|
|
|
|
def test_subscribe_with_a_single_arity_lambda_listener
|
|
|
|
event_name = nil
|
|
|
|
listener = ->(event) do
|
|
|
|
event_name = event.name
|
|
|
|
end
|
|
|
|
|
|
|
|
@notifier.subscribe(&listener)
|
|
|
|
ActiveSupport::Notifications.instrument("event_name")
|
|
|
|
|
|
|
|
assert_equal "event_name", event_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_subscribe_with_a_single_arity_callable_listener
|
|
|
|
event_name = nil
|
|
|
|
listener = Class.new do
|
|
|
|
define_method :call do |event|
|
|
|
|
event_name = event.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@notifier.subscribe(nil, listener.new)
|
|
|
|
ActiveSupport::Notifications.instrument("event_name")
|
|
|
|
|
|
|
|
assert_equal "event_name", event_name
|
|
|
|
end
|
2018-07-26 19:03:31 +00:00
|
|
|
end
|
|
|
|
|
2019-05-05 05:55:26 +00:00
|
|
|
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
|
|
|
|
|
Make Notifier::Fanout faster and safer
This commit aims to improve ActiveSupport::Notifications::Fanout. There
are three main goals here: backwards compatibility, safety, and
performance.
* Backwards compatibility
This ActiveSupport::Notifications is an old and well used interface.
Over time it has collected a lot of features and flexibility, much of
which I suspect is not used anywhere by anyone, but it is hard to know
specifics and we would at minimum need a deprecation cycle.
For this reason this aims to fully maintain compatibility. This includes
both the ability to use an alternate notification implementation instead
of Fanout, the signatures received by all types of listeners, and the
interface used on the Instrumenter and Fanout itself (including the
sometimes problematic start/finish).
* Safety
There have been issues (both recent and past) with the "timestacks"
becoming invalid, particularly when subscribing and unsubscribing within
events. This is an issue when topics are subscribed/unsubscribed to
while they are in flight.
The previous implementation would record a separate timestamp or event
object for each listener in a thread local stack. This meant that it was
essential that the listeners to start and finish were identical.
This issue is avoided by passing the listeners used to `start` the event
to `finish` (`finish_with_state` in the Instrumenter), to ensure they
are the same set in `start`/`finish`.
This commit further avoids this issue. Instead of pushing individual
times onto a stack, we now push a single object, `Handle`, onto the
stack for an event. This object holds all the subscribers (recorded at
start time) and all their associated data. This means that as long as
start/stop calls are not interleaved.
This commit also exposes `build_handle` as a public interface. This
returns the Handle object which can have start/stop called at any time
and any order safely. The one reservation I have with making this public
is that existing "evented" listeners (those receiving start/stop) may
not be ready for that (ex. if they maintain an internal thread-local
stack).
* Performance
This aims to be faster and make fewer allocations then the existing
implementation.
For time-based and event-object-based listeners, the previous
implementation created a separate object for each listener, pushing
and popping it on a thread-local stack. This is slower both because we
need to access the thread local repeatedly (hash lookups) and because
we're allocating duplicate objects.
The new implementation works by grouping similar types of listeners
together and shares either the `Event` or start/stop times between all
of them. The grouping was done so that we didn't need to allocate Events
or Times for topics which did have a listener of that type.
This implementation is significantly faster for all cases, except for
evented, which is slower.
For topics with 10 subscriptions:
*main*:
timed 66.739k (± 2.5%) i/s - 338.800k in 5.079883s
timed_monotonic 138.265k (± 0.6%) i/s - 699.261k in 5.057575s
event_object 48.650k (± 0.2%) i/s - 244.250k in 5.020614s
evented 366.559k (± 1.0%) i/s - 1.851M in 5.049727s
unsubscribed 3.696M (± 0.5%) i/s - 18.497M in 5.005335s
*This branch*:
timed 259.031k (± 0.6%) i/s - 1.302M in 5.025612s
timed_monotonic 327.439k (± 1.7%) i/s - 1.665M in 5.086815s
event_object 228.991k (± 0.3%) i/s - 1.164M in 5.083539s
evented 296.057k (± 0.3%) i/s - 1.501M in 5.070315s
unsubscribed 3.670M (± 0.3%) i/s - 18.376M in 5.007095s
Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
Co-authored-by: Theo Julienne <theojulienne@github.com>
2022-02-17 16:47:29 +00:00
|
|
|
class BuildHandleTest < TestCase
|
|
|
|
def test_interleaved_event
|
|
|
|
event_name = "foo"
|
|
|
|
actual_times = []
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.subscribe(event_name) do |name, started, finished, unique_id, data|
|
|
|
|
actual_times << [started, finished]
|
|
|
|
end
|
|
|
|
|
|
|
|
times = (1..4).map { |s| Time.new(2020, 1, 1) + s }
|
|
|
|
|
|
|
|
instrumenter = ActiveSupport::Notifications.instrumenter
|
|
|
|
travel_to times[0]
|
|
|
|
handle1 = instrumenter.build_handle(event_name, {})
|
|
|
|
handle2 = instrumenter.build_handle(event_name, {})
|
|
|
|
|
|
|
|
handle1.start
|
|
|
|
travel_to times[1]
|
|
|
|
handle2.start
|
|
|
|
travel_to times[2]
|
|
|
|
handle1.finish
|
|
|
|
travel_to times[3]
|
|
|
|
handle2.finish
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
# from when start1 was returned, to when its state passed to finish
|
|
|
|
[times[0], times[2]],
|
|
|
|
# from when start2 was returned, to when its state passed to finish
|
|
|
|
[times[1], times[3]],
|
|
|
|
], actual_times
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_subscribed_interleaved_with_event
|
|
|
|
instrumenter = ActiveSupport::Notifications.instrumenter
|
|
|
|
|
|
|
|
name = "foo"
|
|
|
|
events1 = []
|
|
|
|
events2 = []
|
|
|
|
|
|
|
|
callback1 = lambda { |event| events1 << event }
|
|
|
|
callback2 = lambda { |event| events2 << event }
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.subscribed(callback1, name) do
|
|
|
|
handle = instrumenter.build_handle(name, {})
|
|
|
|
handle.start
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.subscribed(callback2, name) do
|
|
|
|
handle.finish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1, events1.size
|
|
|
|
assert_empty events2
|
|
|
|
|
|
|
|
assert_equal name, events1[0].name
|
|
|
|
assert events1[0].time
|
|
|
|
assert events1[0].end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-05 19:02:54 +00:00
|
|
|
class SubscribedTest < TestCase
|
|
|
|
def test_subscribed
|
|
|
|
name = "foo"
|
|
|
|
name2 = name * 2
|
|
|
|
expected = [name, name]
|
|
|
|
|
|
|
|
events = []
|
2016-08-16 07:30:11 +00:00
|
|
|
callback = lambda { |*_| events << _.first }
|
2011-11-05 19:02:54 +00:00
|
|
|
ActiveSupport::Notifications.subscribed(callback, name) do
|
|
|
|
ActiveSupport::Notifications.instrument(name)
|
|
|
|
ActiveSupport::Notifications.instrument(name2)
|
|
|
|
ActiveSupport::Notifications.instrument(name)
|
|
|
|
end
|
|
|
|
assert_equal expected, events
|
|
|
|
|
2019-06-02 20:47:10 +00:00
|
|
|
ActiveSupport::Notifications.instrument(name)
|
|
|
|
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
|
|
|
|
|
2011-11-05 19:02:54 +00:00
|
|
|
ActiveSupport::Notifications.instrument(name)
|
|
|
|
assert_equal expected, events
|
|
|
|
end
|
2015-11-28 00:40:21 +00:00
|
|
|
|
2019-02-01 16:47:10 +00:00
|
|
|
def test_subscribing_to_instrumentation_while_inside_it
|
2015-11-28 00:40:21 +00:00
|
|
|
# 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
|
|
|
|
|
2016-08-06 16:03:25 +00:00
|
|
|
ActiveSupport::Notifications.subscribe("foo", TestSubscriber.new)
|
2015-11-28 00:40:21 +00:00
|
|
|
|
2022-02-15 22:59:38 +00:00
|
|
|
assert_nothing_raised do
|
|
|
|
ActiveSupport::Notifications.instrument("foo") do
|
|
|
|
ActiveSupport::Notifications.subscribe("foo") { }
|
|
|
|
end
|
2015-11-28 00:40:21 +00:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.notifier = old_notifier
|
|
|
|
end
|
2019-05-05 05:55:26 +00:00
|
|
|
|
|
|
|
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
|
2011-11-05 19:02:54 +00:00
|
|
|
end
|
|
|
|
|
2022-02-16 23:50:05 +00:00
|
|
|
class InspectTest < TestCase
|
|
|
|
def test_inspect_output_is_small
|
|
|
|
expected = "#<ActiveSupport::Notifications::Fanout (2 patterns)>"
|
|
|
|
assert_equal expected, @notifier.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-02 00:01:08 +00:00
|
|
|
class UnsubscribeTest < TestCase
|
2010-03-02 01:33:59 +00:00
|
|
|
def test_unsubscribing_removes_a_subscription
|
2010-03-02 00:01:08 +00:00
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
@notifier.unsubscribe(@subscription)
|
2010-03-02 01:33:59 +00:00
|
|
|
@notifier.publish :foo
|
2010-03-02 00:01:08 +00:00
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
end
|
2010-03-02 01:33:59 +00:00
|
|
|
|
2010-04-25 02:12:02 +00:00
|
|
|
def test_unsubscribing_by_name_removes_a_subscription
|
|
|
|
@notifier.publish "named.subscription", :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [["named.subscription", :foo]], @named_events
|
|
|
|
@notifier.unsubscribe("named.subscription")
|
|
|
|
@notifier.publish "named.subscription", :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [["named.subscription", :foo]], @named_events
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unsubscribing_by_name_leaves_the_other_subscriptions
|
|
|
|
@notifier.publish "named.subscription", :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [["named.subscription", :foo]], @events
|
|
|
|
@notifier.unsubscribe("named.subscription")
|
|
|
|
@notifier.publish "named.subscription", :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [["named.subscription", :foo], ["named.subscription", :foo]], @events
|
|
|
|
end
|
|
|
|
|
2019-02-07 19:58:50 +00:00
|
|
|
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
|
|
|
|
|
2010-03-02 01:33:59 +00:00
|
|
|
private
|
|
|
|
def event(*args)
|
|
|
|
args
|
|
|
|
end
|
2010-03-02 00:01:08 +00:00
|
|
|
end
|
|
|
|
|
2013-05-17 23:27:23 +00:00
|
|
|
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
|
|
|
|
|
2010-01-21 12:05:30 +00:00
|
|
|
class SyncPubSubTest < TestCase
|
2009-11-28 20:49:07 +00:00
|
|
|
def test_events_are_published_to_a_listener
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
end
|
2009-10-27 07:07:21 +00:00
|
|
|
|
2010-03-02 01:33:59 +00:00
|
|
|
def test_publishing_multiple_times_works
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo], [:foo]], @events
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_publishing_after_a_new_subscribe_works
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.publish :foo
|
|
|
|
|
2023-09-29 15:34:44 +00:00
|
|
|
@notifier.subscribe("not_existent") { |event| @events << event }
|
2010-03-02 01:33:59 +00:00
|
|
|
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
|
|
|
|
assert_equal [[:foo]] * 4, @events
|
|
|
|
end
|
|
|
|
|
2010-03-17 22:44:03 +00:00
|
|
|
def test_log_subscriber_with_string
|
2009-11-28 20:49:07 +00:00
|
|
|
events = []
|
2016-08-06 16:03:25 +00:00
|
|
|
@notifier.subscribe("1") { |*args| events << args }
|
2009-10-15 17:49:29 +00:00
|
|
|
|
2016-08-06 16:03:25 +00:00
|
|
|
@notifier.publish "1"
|
|
|
|
@notifier.publish "1.a"
|
|
|
|
@notifier.publish "a.1"
|
2009-11-28 20:49:07 +00:00
|
|
|
@notifier.wait
|
2009-10-15 17:49:29 +00:00
|
|
|
|
2016-08-06 16:03:25 +00:00
|
|
|
assert_equal [["1"]], events
|
2009-11-28 20:49:07 +00:00
|
|
|
end
|
2009-10-27 07:07:21 +00:00
|
|
|
|
2010-03-17 22:44:03 +00:00
|
|
|
def test_log_subscriber_with_pattern
|
2009-11-28 20:49:07 +00:00
|
|
|
events = []
|
|
|
|
@notifier.subscribe(/\d/) { |*args| events << args }
|
2009-10-15 17:49:29 +00:00
|
|
|
|
2016-08-06 16:03:25 +00:00
|
|
|
@notifier.publish "1"
|
|
|
|
@notifier.publish "a.1"
|
|
|
|
@notifier.publish "1.a"
|
2009-11-28 20:49:07 +00:00
|
|
|
@notifier.wait
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2016-08-06 16:03:25 +00:00
|
|
|
assert_equal [["1"], ["a.1"], ["1.a"]], events
|
2009-10-28 04:01:31 +00:00
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2010-02-15 14:44:30 +00:00
|
|
|
def test_multiple_log_subscribers
|
2009-11-28 20:49:07 +00:00
|
|
|
@another = []
|
|
|
|
@notifier.subscribe { |*args| @another << args }
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
assert_equal [[:foo]], @another
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2013-05-17 23:27:23 +00:00
|
|
|
def test_publish_with_subscriber
|
|
|
|
subscriber = TestSubscriber.new
|
|
|
|
@notifier.subscribe nil, subscriber
|
|
|
|
@notifier.publish :foo
|
|
|
|
|
|
|
|
assert_equal [[:foo]], subscriber.publishes
|
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
private
|
|
|
|
def event(*args)
|
|
|
|
args
|
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
class InstrumentationTest < TestCase
|
2016-08-06 17:38:33 +00:00
|
|
|
delegate :instrument, to: ActiveSupport::Notifications
|
2010-01-06 21:23:29 +00:00
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
def test_instrument_returns_block_result
|
2010-01-06 21:23:29 +00:00
|
|
|
assert_equal 2, instrument(:awesome) { 1 + 1 }
|
2010-01-13 21:28:18 +00:00
|
|
|
end
|
|
|
|
|
2013-03-02 23:50:41 +00:00
|
|
|
def test_instrument_yields_the_payload_for_further_modification
|
2010-01-15 10:01:37 +00:00
|
|
|
assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 }
|
2010-01-13 21:28:18 +00:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.first.name
|
2016-08-06 17:38:33 +00:00
|
|
|
assert_equal Hash[result: 2], @events.first.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2010-01-04 21:22:21 +00:00
|
|
|
def test_instrumenter_exposes_its_id
|
2010-01-06 21:23:29 +00:00
|
|
|
assert_equal 20, ActiveSupport::Notifications.instrumenter.id.size
|
2010-01-04 21:22:21 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
def test_nested_events_can_be_instrumented
|
2016-08-06 17:38:33 +00:00
|
|
|
instrument(:awesome, payload: "notifications") do
|
|
|
|
instrument(:wot, payload: "child") do
|
2009-11-28 20:49:07 +00:00
|
|
|
1 + 1
|
|
|
|
end
|
2009-10-06 12:42:42 +00:00
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :wot, @events.first.name
|
2016-08-06 17:38:33 +00:00
|
|
|
assert_equal Hash[payload: "child"], @events.first.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
assert_equal 2, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
2016-08-06 17:38:33 +00:00
|
|
|
assert_equal Hash[payload: "notifications"], @events.last.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2010-04-27 21:13:47 +00:00
|
|
|
def test_instrument_publishes_when_exception_is_raised
|
2009-11-28 20:49:07 +00:00
|
|
|
begin
|
2016-08-06 17:38:33 +00:00
|
|
|
instrument(:awesome, payload: "notifications") do
|
2010-01-31 05:27:24 +00:00
|
|
|
raise "FAIL"
|
2009-11-28 20:49:07 +00:00
|
|
|
end
|
2010-01-12 12:07:04 +00:00
|
|
|
rescue RuntimeError => e
|
2010-01-31 05:27:24 +00:00
|
|
|
assert_equal "FAIL", e.message
|
2009-11-28 20:49:07 +00:00
|
|
|
end
|
2009-10-28 08:58:33 +00:00
|
|
|
|
2010-04-27 21:13:47 +00:00
|
|
|
assert_equal 1, @events.size
|
2016-08-06 17:38:33 +00:00
|
|
|
assert_equal Hash[payload: "notifications",
|
|
|
|
exception: ["RuntimeError", "FAIL"], exception_object: e], @events.last.payload
|
2009-10-28 08:58:33 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
def test_event_is_pushed_even_without_block
|
2016-08-06 17:38:33 +00:00
|
|
|
instrument(:awesome, payload: "notifications")
|
2009-11-28 20:49:07 +00:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
2016-08-06 17:38:33 +00:00
|
|
|
assert_equal Hash[payload: "notifications"], @events.last.payload
|
2009-10-28 04:01:31 +00:00
|
|
|
end
|
2009-10-07 14:17:50 +00:00
|
|
|
end
|
|
|
|
|
2009-11-28 20:49:07 +00:00
|
|
|
class EventTest < TestCase
|
|
|
|
def test_events_are_initialized_with_details
|
2021-10-21 07:39:42 +00:00
|
|
|
time = Time.now.to_f
|
2009-12-26 19:28:53 +00:00
|
|
|
event = event(:foo, time, time + 0.01, random_id, {})
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2018-01-25 23:57:25 +00:00
|
|
|
assert_equal :foo, event.name
|
2021-11-30 20:03:32 +00:00
|
|
|
assert_in_epsilon 10.0, event.duration, 0.01
|
2009-10-28 04:01:31 +00:00
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2019-04-30 21:32:03 +00:00
|
|
|
def test_event_cpu_time_does_not_raise_error_when_start_or_finished_not_called
|
|
|
|
time = Time.now
|
2019-04-15 23:50:35 +00:00
|
|
|
event = event(:foo, time, time + 0.01, random_id, {})
|
|
|
|
|
|
|
|
assert_equal 0, event.cpu_time
|
|
|
|
end
|
|
|
|
|
2009-12-26 19:28:53 +00:00
|
|
|
def test_events_consumes_information_given_as_payload
|
2021-10-21 07:31:25 +00:00
|
|
|
event = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 1, random_id, payload: :bar)
|
2016-08-06 17:38:33 +00:00
|
|
|
assert_equal Hash[payload: :bar], event.payload
|
2009-12-26 19:28:53 +00:00
|
|
|
end
|
|
|
|
|
2020-06-14 14:28:09 +00:00
|
|
|
def test_subscribe_raises_error_on_non_supported_arguments
|
|
|
|
notifier = ActiveSupport::Notifications::Fanout.new
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
notifier.subscribe(:symbol) { |*_| }
|
|
|
|
end
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
notifier.subscribe(Object.new) { |*_| }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 11:31:57 +00:00
|
|
|
private
|
2009-11-28 20:49:07 +00:00
|
|
|
def random_id
|
2011-05-23 11:02:06 +00:00
|
|
|
@random_id ||= SecureRandom.hex(10)
|
2009-11-28 20:49:07 +00:00
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
end
|