2009-09-16 18:53:49 -04:00
|
|
|
require 'abstract_unit'
|
2011-08-15 16:06:21 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
module Notifications
|
|
|
|
class TestCase < ActiveSupport::TestCase
|
|
|
|
def setup
|
2011-02-09 17:02:38 -05:00
|
|
|
@old_notifier = ActiveSupport::Notifications.notifier
|
|
|
|
@notifier = ActiveSupport::Notifications::Fanout.new
|
|
|
|
ActiveSupport::Notifications.notifier = @notifier
|
2009-11-28 15:49:07 -05:00
|
|
|
@events = []
|
2010-04-24 22:12:02 -04:00
|
|
|
@named_events = []
|
2010-03-01 19:01:08 -05:00
|
|
|
@subscription = @notifier.subscribe { |*args| @events << event(*args) }
|
2010-04-24 22:12:02 -04:00
|
|
|
@named_subscription = @notifier.subscribe("named.subscription") { |*args| @named_events << event(*args) }
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
2009-09-30 07:59:15 -04:00
|
|
|
|
2011-02-09 17:02:38 -05:00
|
|
|
def teardown
|
|
|
|
ActiveSupport::Notifications.notifier = @old_notifier
|
|
|
|
end
|
|
|
|
|
2010-07-20 11:07:18 -04:00
|
|
|
private
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2010-07-20 11:07:18 -04:00
|
|
|
def event(*args)
|
|
|
|
ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
2009-10-15 13:49:29 -04:00
|
|
|
|
2011-11-05 15:02:54 -04:00
|
|
|
class SubscribedTest < TestCase
|
|
|
|
def test_subscribed
|
|
|
|
name = "foo"
|
|
|
|
name2 = name * 2
|
|
|
|
expected = [name, name]
|
|
|
|
|
|
|
|
events = []
|
|
|
|
callback = lambda {|*_| events << _.first}
|
|
|
|
ActiveSupport::Notifications.subscribed(callback, name) 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
|
|
|
|
end
|
|
|
|
|
2010-03-01 19:01:08 -05:00
|
|
|
class UnsubscribeTest < TestCase
|
2010-03-01 20:33:59 -05:00
|
|
|
def test_unsubscribing_removes_a_subscription
|
2010-03-01 19:01:08 -05:00
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
@notifier.unsubscribe(@subscription)
|
2010-03-01 20:33:59 -05:00
|
|
|
@notifier.publish :foo
|
2010-03-01 19:01:08 -05:00
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
end
|
2010-03-01 20:33:59 -05:00
|
|
|
|
2010-04-24 22:12:02 -04: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
|
|
|
|
|
2010-03-01 20:33:59 -05:00
|
|
|
private
|
|
|
|
def event(*args)
|
|
|
|
args
|
|
|
|
end
|
2010-03-01 19:01:08 -05:00
|
|
|
end
|
|
|
|
|
2010-01-21 07:05:30 -05:00
|
|
|
class SyncPubSubTest < TestCase
|
2009-11-28 15:49:07 -05:00
|
|
|
def test_events_are_published_to_a_listener
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
end
|
2009-10-27 03:07:21 -04:00
|
|
|
|
2010-03-01 20:33:59 -05: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
|
|
|
|
|
|
|
|
@notifier.subscribe("not_existant") do |*args|
|
|
|
|
@events << ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
|
|
|
|
|
|
|
assert_equal [[:foo]] * 4, @events
|
|
|
|
end
|
|
|
|
|
2010-03-17 18:44:03 -04:00
|
|
|
def test_log_subscriber_with_string
|
2009-11-28 15:49:07 -05:00
|
|
|
events = []
|
|
|
|
@notifier.subscribe('1') { |*args| events << args }
|
2009-10-15 13:49:29 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
@notifier.publish '1'
|
|
|
|
@notifier.publish '1.a'
|
|
|
|
@notifier.publish 'a.1'
|
|
|
|
@notifier.wait
|
2009-10-15 13:49:29 -04:00
|
|
|
|
2010-03-17 18:44:03 -04:00
|
|
|
assert_equal [['1']], events
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
2009-10-27 03:07:21 -04:00
|
|
|
|
2010-03-17 18:44:03 -04:00
|
|
|
def test_log_subscriber_with_pattern
|
2009-11-28 15:49:07 -05:00
|
|
|
events = []
|
|
|
|
@notifier.subscribe(/\d/) { |*args| events << args }
|
2009-10-15 13:49:29 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
@notifier.publish '1'
|
|
|
|
@notifier.publish 'a.1'
|
|
|
|
@notifier.publish '1.a'
|
|
|
|
@notifier.wait
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
assert_equal [['1'], ['a.1'], ['1.a']], events
|
2009-10-28 00:01:31 -04:00
|
|
|
end
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2010-02-15 09:44:30 -05:00
|
|
|
def test_multiple_log_subscribers
|
2009-11-28 15:49:07 -05:00
|
|
|
@another = []
|
|
|
|
@notifier.subscribe { |*args| @another << args }
|
|
|
|
@notifier.publish :foo
|
|
|
|
@notifier.wait
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
assert_equal [[:foo]], @events
|
|
|
|
assert_equal [[:foo]], @another
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
private
|
|
|
|
def event(*args)
|
|
|
|
args
|
|
|
|
end
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
class InstrumentationTest < TestCase
|
2010-02-04 04:39:55 -05:00
|
|
|
delegate :instrument, :to => ActiveSupport::Notifications
|
2010-01-06 16:23:29 -05:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def test_instrument_returns_block_result
|
2010-01-06 16:23:29 -05:00
|
|
|
assert_equal 2, instrument(:awesome) { 1 + 1 }
|
2010-01-13 16:28:18 -05:00
|
|
|
end
|
|
|
|
|
2010-01-15 05:01:37 -05:00
|
|
|
def test_instrument_yields_the_paylod_for_further_modification
|
|
|
|
assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 }
|
2010-01-13 16:28:18 -05:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.first.name
|
|
|
|
assert_equal Hash[:result => 2], @events.first.payload
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2010-01-04 16:22:21 -05:00
|
|
|
def test_instrumenter_exposes_its_id
|
2010-01-06 16:23:29 -05:00
|
|
|
assert_equal 20, ActiveSupport::Notifications.instrumenter.id.size
|
2010-01-04 16:22:21 -05:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def test_nested_events_can_be_instrumented
|
2010-01-06 16:23:29 -05:00
|
|
|
instrument(:awesome, :payload => "notifications") do
|
|
|
|
instrument(:wot, :payload => "child") do
|
2009-11-28 15:49:07 -05:00
|
|
|
1 + 1
|
|
|
|
end
|
2009-10-06 08:42:42 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :wot, @events.first.name
|
|
|
|
assert_equal Hash[:payload => "child"], @events.first.payload
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
assert_equal 2, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
|
|
|
assert_equal Hash[:payload => "notifications"], @events.last.payload
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
|
2010-04-27 17:13:47 -04:00
|
|
|
def test_instrument_publishes_when_exception_is_raised
|
2009-11-28 15:49:07 -05:00
|
|
|
begin
|
2010-01-06 16:23:29 -05:00
|
|
|
instrument(:awesome, :payload => "notifications") do
|
2010-01-31 00:27:24 -05:00
|
|
|
raise "FAIL"
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
2010-01-12 07:07:04 -05:00
|
|
|
rescue RuntimeError => e
|
2010-01-31 00:27:24 -05:00
|
|
|
assert_equal "FAIL", e.message
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
2009-10-28 04:58:33 -04:00
|
|
|
|
2010-04-27 17:13:47 -04:00
|
|
|
assert_equal 1, @events.size
|
2010-05-02 16:40:20 -04:00
|
|
|
assert_equal Hash[:payload => "notifications",
|
|
|
|
:exception => ["RuntimeError", "FAIL"]], @events.last.payload
|
2009-10-28 04:58:33 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def test_event_is_pushed_even_without_block
|
2010-01-06 16:23:29 -05:00
|
|
|
instrument(:awesome, :payload => "notifications")
|
2009-11-28 15:49:07 -05:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
|
|
|
assert_equal Hash[:payload => "notifications"], @events.last.payload
|
2009-10-28 00:01:31 -04:00
|
|
|
end
|
2009-10-07 10:17:50 -04:00
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
class EventTest < TestCase
|
|
|
|
def test_events_are_initialized_with_details
|
|
|
|
time = Time.now
|
2009-12-26 14:28:53 -05:00
|
|
|
event = event(:foo, time, time + 0.01, random_id, {})
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2010-07-11 21:51:37 -04:00
|
|
|
assert_equal :foo, event.name
|
|
|
|
assert_equal time, event.time
|
2010-07-18 13:19:34 -04:00
|
|
|
assert_in_delta 10.0, event.duration, 0.00001
|
2009-10-28 00:01:31 -04:00
|
|
|
end
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2009-12-26 14:28:53 -05:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
def test_event_is_parent_based_on_time_frame
|
|
|
|
time = Time.utc(2009, 01, 01, 0, 0, 1)
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2009-12-26 14:28:53 -05:00
|
|
|
parent = event(:foo, Time.utc(2009), Time.utc(2009) + 100, random_id, {})
|
|
|
|
child = event(:foo, time, time + 10, random_id, {})
|
|
|
|
not_child = event(:foo, time, time + 100, random_id, {})
|
2009-09-16 18:53:49 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
assert parent.parent_of?(child)
|
|
|
|
assert !child.parent_of?(parent)
|
|
|
|
assert !parent.parent_of?(not_child)
|
|
|
|
assert !not_child.parent_of?(parent)
|
|
|
|
end
|
2009-09-30 07:59:15 -04:00
|
|
|
|
2009-11-28 15:49:07 -05:00
|
|
|
protected
|
|
|
|
def random_id
|
2011-05-23 07:02:06 -04:00
|
|
|
@random_id ||= SecureRandom.hex(10)
|
2009-11-28 15:49:07 -05:00
|
|
|
end
|
2009-09-16 18:53:49 -04:00
|
|
|
end
|
|
|
|
end
|