1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add a test for elapsed and require missing benchmark file.

This commit is contained in:
José Valim 2010-07-20 17:07:18 +02:00
parent 51d2db0a63
commit 9df9c4bac0
3 changed files with 16 additions and 19 deletions

View file

@ -1,3 +1,4 @@
require 'benchmark'
require 'abstract_controller/logger' require 'abstract_controller/logger'
module ActionController module ActionController

View file

@ -17,8 +17,8 @@ module ActiveSupport
# and publish it. Notice that events get sent even if an error occurs # and publish it. Notice that events get sent even if an error occurs
# in the passed-in block # in the passed-in block
def instrument(name, payload={}) def instrument(name, payload={})
@started = Time.now
begin begin
@started = Time.now
yield(payload) if block_given? yield(payload) if block_given?
rescue Exception => e rescue Exception => e
payload[:exception] = [e.class.name, e.message] payload[:exception] = [e.class.name, e.message]
@ -30,7 +30,7 @@ module ActiveSupport
end end
def elapsed def elapsed
1000.0 * @finished.to_f - @started.to_f 1000.0 * (@finished.to_f - @started.to_f)
end end
private private

View file

@ -11,14 +11,11 @@ module Notifications
@named_subscription = @notifier.subscribe("named.subscription") { |*args| @named_events << event(*args) } @named_subscription = @notifier.subscribe("named.subscription") { |*args| @named_events << event(*args) }
end end
private private
def event(*args)
ActiveSupport::Notifications::Event.new(*args)
end
def drain def event(*args)
@notifier.wait ActiveSupport::Notifications::Event.new(*args)
end end
end end
class UnsubscribeTest < TestCase class UnsubscribeTest < TestCase
@ -132,13 +129,10 @@ module Notifications
def test_instrument_returns_block_result def test_instrument_returns_block_result
assert_equal 2, instrument(:awesome) { 1 + 1 } assert_equal 2, instrument(:awesome) { 1 + 1 }
drain
end end
def test_instrument_yields_the_paylod_for_further_modification def test_instrument_yields_the_paylod_for_further_modification
assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 }
drain
assert_equal 1, @events.size assert_equal 1, @events.size
assert_equal :awesome, @events.first.name assert_equal :awesome, @events.first.name
assert_equal Hash[:result => 2], @events.first.payload assert_equal Hash[:result => 2], @events.first.payload
@ -154,15 +148,11 @@ module Notifications
1 + 1 1 + 1
end end
drain
assert_equal 1, @events.size assert_equal 1, @events.size
assert_equal :wot, @events.first.name assert_equal :wot, @events.first.name
assert_equal Hash[:payload => "child"], @events.first.payload assert_equal Hash[:payload => "child"], @events.first.payload
end end
drain
assert_equal 2, @events.size assert_equal 2, @events.size
assert_equal :awesome, @events.last.name assert_equal :awesome, @events.last.name
assert_equal Hash[:payload => "notifications"], @events.last.payload assert_equal Hash[:payload => "notifications"], @events.last.payload
@ -177,16 +167,22 @@ module Notifications
assert_equal "FAIL", e.message assert_equal "FAIL", e.message
end end
drain
assert_equal 1, @events.size assert_equal 1, @events.size
assert_equal Hash[:payload => "notifications", assert_equal Hash[:payload => "notifications",
:exception => ["RuntimeError", "FAIL"]], @events.last.payload :exception => ["RuntimeError", "FAIL"]], @events.last.payload
end end
def test_elapsed
instrument(:something) do
sleep(0.001)
end
# Elapsed returns duration in ms
assert_in_delta 1, ActiveSupport::Notifications.instrumenter.elapsed, 100
end
def test_event_is_pushed_even_without_block def test_event_is_pushed_even_without_block
instrument(:awesome, :payload => "notifications") instrument(:awesome, :payload => "notifications")
drain
assert_equal 1, @events.size assert_equal 1, @events.size
assert_equal :awesome, @events.last.name assert_equal :awesome, @events.last.name
assert_equal Hash[:payload => "notifications"], @events.last.payload assert_equal Hash[:payload => "notifications"], @events.last.payload