2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04: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
|
|
|
|
2011-12-09 19:03:18 -05:00
|
|
|
attr_reader :buffer, :logger
|
|
|
|
|
|
|
|
class Buffer
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
def initialize; @lines = []; end
|
|
|
|
def each(&block); @lines.each(&block); end
|
|
|
|
def write(x); @lines << x; end
|
|
|
|
def close; end
|
|
|
|
def last; @lines.last; end
|
|
|
|
def size; @lines.size; end
|
|
|
|
def empty?; @lines.empty?; end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@buffer = Buffer.new
|
2011-12-19 21:41:37 -05:00
|
|
|
@logger = ActiveSupport::Logger.new(@buffer)
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_without_block
|
|
|
|
assert_raise(LocalJumpError) { benchmark }
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty buffer
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
benchmark("test_run") { i_was_run = true }
|
2005-07-05 15:33:25 -04:00
|
|
|
assert i_was_run
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_last_logged "test_run"
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
2016-02-20 12:41:33 -05:00
|
|
|
def test_with_silence
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_difference "buffer.count", +2 do
|
|
|
|
benchmark("test_run") do
|
2016-02-20 12:41:33 -05:00
|
|
|
logger.info "SOMETHING"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_difference "buffer.count", +1 do
|
|
|
|
benchmark("test_run", silence: true) do
|
2016-02-20 12:41:33 -05:00
|
|
|
logger.info "NOTHING"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-01 12:53:16 -05:00
|
|
|
def test_within_level
|
2011-12-19 21:41:37 -05:00
|
|
|
logger.level = ActiveSupport::Logger::DEBUG
|
2016-08-16 03:30:11 -04:00
|
|
|
benchmark("included_debug_run", level: :debug) {}
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_last_logged "included_debug_run"
|
2009-01-01 12:53:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_outside_level
|
2011-12-19 21:41:37 -05:00
|
|
|
logger.level = ActiveSupport::Logger::ERROR
|
2016-08-16 03:30:11 -04:00
|
|
|
benchmark("skipped_debug_run", level: :debug) {}
|
2009-01-01 12:53:16 -05:00
|
|
|
assert_no_match(/skipped_debug_run/, buffer.last)
|
|
|
|
ensure
|
2011-12-19 21:41:37 -05:00
|
|
|
logger.level = ActiveSupport::Logger::DEBUG
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 12:03:25 -04:00
|
|
|
def assert_last_logged(message = "Benchmarking")
|
2009-01-01 12:53:16 -05:00
|
|
|
assert_match(/^#{message} \(.*\)$/, buffer.last)
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
end
|