2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2005-07-05 15:33:25 -04:00
|
|
|
|
2009-10-09 08:52:25 -04:00
|
|
|
class BenchmarkableTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Benchmarkable
|
2005-07-05 15:33:25 -04:00
|
|
|
|
2009-09-28 14:31:30 -04:00
|
|
|
def teardown
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.send(:clear_buffer)
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_without_block
|
|
|
|
assert_raise(LocalJumpError) { benchmark }
|
2009-01-01 12:53:16 -05:00
|
|
|
assert buffer.empty?
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_defaults
|
|
|
|
i_was_run = false
|
|
|
|
benchmark { i_was_run = true }
|
|
|
|
assert i_was_run
|
|
|
|
assert_last_logged
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_message
|
|
|
|
i_was_run = false
|
|
|
|
benchmark('test_run') { i_was_run = true }
|
|
|
|
assert i_was_run
|
|
|
|
assert_last_logged 'test_run'
|
|
|
|
end
|
|
|
|
|
2009-01-01 12:53:16 -05:00
|
|
|
def test_with_message_and_deprecated_level
|
2005-07-05 15:33:25 -04:00
|
|
|
i_was_run = false
|
2009-01-01 12:53:16 -05:00
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
benchmark('debug_run', :debug) { i_was_run = true }
|
|
|
|
end
|
|
|
|
|
2005-07-05 15:33:25 -04:00
|
|
|
assert i_was_run
|
2009-01-01 12:53:16 -05:00
|
|
|
assert_last_logged 'debug_run'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_within_level
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.level = ActiveSupport::BufferedLogger::DEBUG
|
2009-01-01 12:53:16 -05:00
|
|
|
benchmark('included_debug_run', :level => :debug) { }
|
|
|
|
assert_last_logged 'included_debug_run'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_outside_level
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.level = ActiveSupport::BufferedLogger::ERROR
|
2009-01-01 12:53:16 -05:00
|
|
|
benchmark('skipped_debug_run', :level => :debug) { }
|
|
|
|
assert_no_match(/skipped_debug_run/, buffer.last)
|
|
|
|
ensure
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.level = ActiveSupport::BufferedLogger::DEBUG
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
2009-01-01 12:53:16 -05:00
|
|
|
def test_without_silencing
|
|
|
|
benchmark('debug_run', :silence => false) do
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.info "not silenced!"
|
2009-01-01 12:53:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 2, buffer.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_silencing
|
|
|
|
benchmark('debug_run', :silence => true) do
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.info "silenced!"
|
2009-01-01 12:53:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1, buffer.size
|
|
|
|
end
|
|
|
|
|
2005-07-05 15:33:25 -04:00
|
|
|
private
|
2009-10-09 08:52:25 -04:00
|
|
|
def logger
|
|
|
|
@logger ||= begin
|
|
|
|
logger = ActiveSupport::BufferedLogger.new(StringIO.new)
|
|
|
|
logger.auto_flushing = false
|
|
|
|
logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-01 12:53:16 -05:00
|
|
|
def buffer
|
2009-10-09 08:52:25 -04:00
|
|
|
logger.send(:buffer)
|
2009-01-01 12:53:16 -05:00
|
|
|
end
|
2009-10-09 08:52:25 -04:00
|
|
|
|
2009-01-01 12:53:16 -05:00
|
|
|
def assert_last_logged(message = 'Benchmarking')
|
|
|
|
assert_match(/^#{message} \(.*\)$/, buffer.last)
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
end
|