提交 74f6ccea 编写于 作者: J José Valim

instrumenter should be accessible from ActiveSupport::Notifications.

上级 0c2d933f
......@@ -44,11 +44,16 @@ module Notifications
class << self
attr_writer :notifier
delegate :publish, :subscribe, :instrument, :to => :notifier
delegate :publish, :subscribe, :to => :notifier
delegate :instrument, :to => :instrumenter
def notifier
@notifier ||= Notifier.new
end
def instrumenter
Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier)
end
end
class Notifier
......@@ -67,13 +72,6 @@ def subscribe(pattern = nil, &block)
def wait
@queue.wait
end
delegate :instrument, :to => :current_instrumenter
private
def current_instrumenter
Thread.current[:"instrumentation_#{object_id}"] ||= Notifications::Instrumenter.new(self)
end
end
end
end
......@@ -5,7 +5,8 @@ class TestCase < ActiveSupport::TestCase
def setup
Thread.abort_on_exception = true
@notifier = ActiveSupport::Notifications::Notifier.new
ActiveSupport::Notifications.notifier = nil
@notifier = ActiveSupport::Notifications.notifier
@events = []
@notifier.subscribe { |*args| @events << event(*args) }
end
......@@ -82,17 +83,19 @@ def setup
end
class InstrumentationTest < TestCase
delegate :instrument, :to => ActiveSupport::Notifications
def test_instrument_returns_block_result
assert_equal 2, @notifier.instrument(:awesome) { 1 + 1 }
assert_equal 2, instrument(:awesome) { 1 + 1 }
end
def test_instrumenter_exposes_its_id
assert_equal 20, ActiveSupport::Notifications::Instrumenter.new(@notifier).id.size
assert_equal 20, ActiveSupport::Notifications.instrumenter.id.size
end
def test_nested_events_can_be_instrumented
@notifier.instrument(:awesome, :payload => "notifications") do
@notifier.instrument(:wot, :payload => "child") do
instrument(:awesome, :payload => "notifications") do
instrument(:wot, :payload => "child") do
1 + 1
end
......@@ -112,7 +115,7 @@ def test_nested_events_can_be_instrumented
def test_instrument_publishes_when_exception_is_raised
begin
@notifier.instrument(:awesome, :payload => "notifications") do
instrument(:awesome, :payload => "notifications") do
raise "OMG"
end
flunk
......@@ -127,7 +130,7 @@ def test_instrument_publishes_when_exception_is_raised
end
def test_event_is_pushed_even_without_block
@notifier.instrument(:awesome, :payload => "notifications")
instrument(:awesome, :payload => "notifications")
drain
assert_equal 1, @events.size
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册