Skip to content

Commit

Permalink
AS::Notifications.subscribe blocks are now yielded the arguments to p…
Browse files Browse the repository at this point in the history
…ass to AS::Notifications::Event.new
  • Loading branch information
wycats committed Oct 28, 2009
1 parent 9b67b7b commit cbcb947
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/notifications.rb
@@ -1,6 +1,8 @@
require 'active_support/notifications'

ActiveSupport::Notifications.subscribe(/(read|write|cache|expire|exist)_(fragment|page)\??/) do |event|
ActiveSupport::Notifications.subscribe(/(read|write|cache|expire|exist)_(fragment|page)\??/) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)

if logger = ActionController::Base.logger
human_name = event.name.to_s.humanize
logger.info("#{human_name} (%.1fms)" % event.duration)
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/notifications.rb
Expand Up @@ -96,7 +96,7 @@ def bind(pattern)

def subscribe
@queue.subscribe(@pattern) do |*args|
yield Event.new(*args)
yield *args
end
end
end
Expand Down
18 changes: 14 additions & 4 deletions activesupport/test/notifications_test.rb
Expand Up @@ -52,7 +52,9 @@ class NotificationsMainTest < Test::Unit::TestCase
def setup
@events = []
Thread.abort_on_exception = true
ActiveSupport::Notifications.subscribe { |event| @events << event }
ActiveSupport::Notifications.subscribe do |*args|
@events << ActiveSupport::Notifications::Event.new(*args)
end
end

def teardown
Expand Down Expand Up @@ -124,7 +126,11 @@ def test_event_is_pushed_even_without_block

def test_subscriber_with_pattern
@another = []
ActiveSupport::Notifications.subscribe("cache"){ |event| @another << event }

ActiveSupport::Notifications.subscribe("cache") do |*args|
@another << ActiveSupport::Notifications::Event.new(*args)
end

ActiveSupport::Notifications.instrument(:cache){ 1 }

sleep(0.1)
Expand All @@ -136,7 +142,9 @@ def test_subscriber_with_pattern

def test_subscriber_with_pattern_as_regexp
@another = []
ActiveSupport::Notifications.subscribe(/cache/){ |event| @another << event }
ActiveSupport::Notifications.subscribe(/cache/) do |*args|
@another << ActiveSupport::Notifications::Event.new(*args)
end

ActiveSupport::Notifications.instrument(:something){ 0 }
ActiveSupport::Notifications.instrument(:cache){ 1 }
Expand All @@ -150,7 +158,9 @@ def test_subscriber_with_pattern_as_regexp

def test_with_several_consumers_and_several_events
@another = []
ActiveSupport::Notifications.subscribe { |event| @another << event }
ActiveSupport::Notifications.subscribe do |*args|
@another << ActiveSupport::Notifications::Event.new(*args)
end

1.upto(100) do |i|
ActiveSupport::Notifications.instrument(:value){ i }
Expand Down

0 comments on commit cbcb947

Please sign in to comment.