diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-17 11:17:42 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-17 11:29:51 +0100 |
commit | 0334f9f6cfa4c4c746de7e19250a13366b616c55 (patch) | |
tree | 9e2ff5d7efac90c26f3256d26d0789c14424ab7e /activesupport | |
parent | afd0c06dfa8d3e04e85f9f0ea65c9beb376cb79f (diff) | |
download | rails-0334f9f6cfa4c4c746de7e19250a13366b616c55.tar.gz rails-0334f9f6cfa4c4c746de7e19250a13366b616c55.tar.bz2 rails-0334f9f6cfa4c4c746de7e19250a13366b616c55.zip |
Add ActionDispatch::Notifications middleware.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/notifications.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/notifications/instrumenter.rb | 9 | ||||
-rw-r--r-- | activesupport/test/notifications_test.rb | 16 |
3 files changed, 26 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index 3e96decb8c..a1383bb478 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -45,7 +45,7 @@ module ActiveSupport class << self attr_writer :notifier delegate :publish, :subscribe, :to => :notifier - delegate :instrument, :to => :instrumenter + delegate :instrument, :instrument!, :to => :instrumenter def notifier @notifier ||= Notifier.new diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index f3d877efe7..7c5b118ee3 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -20,6 +20,15 @@ module ActiveSupport result end + # The same as instrument, but sends the notification even if the yielded + # block raises an error. + def instrument!(name, payload={}) + time = Time.now + yield(payload) if block_given? + ensure + @notifier.publish(name, time, Time.now, @id, payload) + end + private def unique_id SecureRandom.hex(10) diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index c3eb1a4eb5..c41d81fe7e 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -90,6 +90,22 @@ module Notifications drain end + def test_instrument_with_bang_returns_result_even_on_failure + begin + instrument!(:awesome, :payload => "notifications") do + raise "OMG" + end + flunk + rescue + end + + drain + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + def test_instrument_yields_the_paylod_for_further_modification assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } drain |