2009-09-16 22:53:49 +00:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
# Allow LittleFanout to be cleaned.
|
2009-10-15 21:51:51 +00:00
|
|
|
class ActiveSupport::Notifications::LittleFanout
|
2009-09-30 11:59:15 +00:00
|
|
|
def clear
|
|
|
|
@listeners.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-15 21:51:51 +00:00
|
|
|
class NotificationsEventTest < Test::Unit::TestCase
|
2009-10-27 07:07:21 +00:00
|
|
|
def test_events_are_initialized_with_details
|
|
|
|
event = event(:foo, Time.now, Time.now + 1, 1, random_id, :payload => :bar)
|
2009-10-02 00:14:38 +00:00
|
|
|
assert_equal :foo, event.name
|
|
|
|
assert_equal Hash[:payload => :bar], event.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-10-02 00:14:38 +00:00
|
|
|
def test_events_consumes_information_given_as_payload
|
2009-10-27 07:07:21 +00:00
|
|
|
time = Time.now
|
|
|
|
event = event(:foo, time, time + 0.01, 1, random_id, {})
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2009-10-02 00:14:38 +00:00
|
|
|
assert_equal Hash.new, event.payload
|
|
|
|
assert_equal time, event.time
|
|
|
|
assert_equal 1, event.result
|
2009-10-27 07:07:21 +00:00
|
|
|
assert_equal 10.0, event.duration
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
2009-10-15 17:49:29 +00:00
|
|
|
|
|
|
|
def test_event_is_parent_based_on_time_frame
|
2009-10-27 07:07:21 +00:00
|
|
|
time = Time.utc(2009, 01, 01, 0, 0, 1)
|
|
|
|
|
|
|
|
parent = event(:foo, Time.utc(2009), Time.utc(2009) + 100, nil, random_id, {})
|
|
|
|
child = event(:foo, time, time + 10, nil, random_id, {})
|
|
|
|
not_child = event(:foo, time, time + 100, nil, random_id, {})
|
2009-10-15 17:49:29 +00: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-10-27 07:07:21 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def random_id
|
|
|
|
@random_id ||= ActiveSupport::SecureRandom.hex(10)
|
|
|
|
end
|
2009-10-15 17:49:29 +00:00
|
|
|
|
2009-10-27 07:07:21 +00:00
|
|
|
def event(*args)
|
|
|
|
ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
end
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-10-15 21:51:51 +00:00
|
|
|
class NotificationsMainTest < Test::Unit::TestCase
|
2009-09-16 22:53:49 +00:00
|
|
|
def setup
|
2009-09-30 11:59:15 +00:00
|
|
|
@events = []
|
2009-10-02 00:14:38 +00:00
|
|
|
Thread.abort_on_exception = true
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.subscribe { |event| @events << event }
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-10-02 00:14:38 +00:00
|
|
|
Thread.abort_on_exception = false
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.queue.clear
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-10-15 21:51:51 +00:00
|
|
|
def test_notifications_returns_action_result
|
|
|
|
result = ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
|
2009-09-16 22:53:49 +00:00
|
|
|
1 + 1
|
|
|
|
end
|
|
|
|
|
2009-10-02 00:14:38 +00:00
|
|
|
assert_equal 2, result
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_events_are_published_to_a_listener
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
|
2009-09-16 22:53:49 +00:00
|
|
|
1 + 1
|
|
|
|
end
|
|
|
|
|
2009-10-06 12:42:42 +00:00
|
|
|
sleep(0.1)
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
2009-10-15 21:51:51 +00:00
|
|
|
assert_equal Hash[:payload => "notifications"], @events.last.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_events_can_be_instrumented
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
|
|
|
|
ActiveSupport::Notifications.instrument(:wot, :payload => "child") do
|
2009-10-06 12:42:42 +00:00
|
|
|
1 + 1
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-10-06 12:42:42 +00:00
|
|
|
sleep(0.1)
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :wot, @events.first.name
|
2009-10-02 00:14:38 +00:00
|
|
|
assert_equal Hash[:payload => "child"], @events.first.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-10-06 12:42:42 +00:00
|
|
|
sleep(0.1)
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
assert_equal 2, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
2009-10-15 21:51:51 +00:00
|
|
|
assert_equal Hash[:payload => "notifications"], @events.last.payload
|
2009-10-06 12:42:42 +00:00
|
|
|
assert_in_delta 100, @events.last.duration, 70
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_event_is_pushed_even_if_block_fails
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
|
2009-09-16 22:53:49 +00:00
|
|
|
raise "OMG"
|
|
|
|
end rescue RuntimeError
|
|
|
|
|
2009-10-06 12:42:42 +00:00
|
|
|
sleep(0.1)
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
2009-10-15 21:51:51 +00:00
|
|
|
assert_equal Hash[:payload => "notifications"], @events.last.payload
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-10-07 14:17:50 +00:00
|
|
|
def test_event_is_pushed_even_without_block
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications")
|
2009-10-07 14:17:50 +00:00
|
|
|
sleep(0.1)
|
|
|
|
|
|
|
|
assert_equal 1, @events.size
|
|
|
|
assert_equal :awesome, @events.last.name
|
2009-10-15 21:51:51 +00:00
|
|
|
assert_equal Hash[:payload => "notifications"], @events.last.payload
|
2009-10-07 14:17:50 +00:00
|
|
|
end
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
def test_subscriber_with_pattern
|
|
|
|
@another = []
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.subscribe("cache"){ |event| @another << event }
|
|
|
|
ActiveSupport::Notifications.instrument(:cache){ 1 }
|
2009-10-07 14:17:50 +00:00
|
|
|
|
|
|
|
sleep(0.1)
|
|
|
|
|
|
|
|
assert_equal 1, @another.size
|
|
|
|
assert_equal :cache, @another.first.name
|
|
|
|
assert_equal 1, @another.first.result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_subscriber_with_pattern_as_regexp
|
|
|
|
@another = []
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.subscribe(/cache/){ |event| @another << event }
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument(:something){ 0 }
|
|
|
|
ActiveSupport::Notifications.instrument(:cache){ 1 }
|
2009-09-16 22:53:49 +00:00
|
|
|
|
2009-10-06 12:42:42 +00:00
|
|
|
sleep(0.1)
|
2009-09-30 11:59:15 +00:00
|
|
|
|
|
|
|
assert_equal 1, @another.size
|
|
|
|
assert_equal :cache, @another.first.name
|
2009-10-07 14:17:50 +00:00
|
|
|
assert_equal 1, @another.first.result
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
def test_with_several_consumers_and_several_events
|
|
|
|
@another = []
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.subscribe { |event| @another << event }
|
2009-09-16 22:53:49 +00:00
|
|
|
|
|
|
|
1.upto(100) do |i|
|
2009-10-15 21:51:51 +00:00
|
|
|
ActiveSupport::Notifications.instrument(:value){ i }
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
sleep 0.1
|
|
|
|
|
2009-09-30 11:59:15 +00:00
|
|
|
assert_equal 100, @events.size
|
|
|
|
assert_equal :value, @events.first.name
|
|
|
|
assert_equal 1, @events.first.result
|
|
|
|
assert_equal 100, @events.last.result
|
|
|
|
|
|
|
|
assert_equal 100, @another.size
|
|
|
|
assert_equal :value, @another.first.name
|
|
|
|
assert_equal 1, @another.first.result
|
|
|
|
assert_equal 100, @another.last.result
|
2009-09-16 22:53:49 +00:00
|
|
|
end
|
|
|
|
end
|