2009-12-30 07:07:48 -05:00
|
|
|
require 'active_record_unit'
|
2009-12-31 01:14:57 -05:00
|
|
|
require 'active_record/railties/controller_runtime'
|
2009-12-30 07:07:48 -05:00
|
|
|
require 'fixtures/project'
|
2010-06-24 07:23:43 -04:00
|
|
|
require 'active_support/log_subscriber/test_helper'
|
|
|
|
require 'action_controller/log_subscriber'
|
2009-12-30 07:07:48 -05:00
|
|
|
|
2015-01-31 23:12:37 -05:00
|
|
|
ActionController::Base.include(ActiveRecord::Railties::ControllerRuntime)
|
2009-12-30 07:07:48 -05:00
|
|
|
|
2010-02-15 09:44:30 -05:00
|
|
|
class ControllerRuntimeLogSubscriberTest < ActionController::TestCase
|
|
|
|
class LogSubscriberController < ActionController::Base
|
2010-01-12 19:18:23 -05:00
|
|
|
def show
|
|
|
|
render :inline => "<%= Project.all %>"
|
|
|
|
end
|
2011-05-14 05:31:00 -04:00
|
|
|
|
|
|
|
def zero
|
|
|
|
render :inline => "Zero DB runtime"
|
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2013-02-07 17:54:46 -05:00
|
|
|
def create
|
|
|
|
ActiveRecord::LogSubscriber.runtime += 100
|
2014-08-18 19:41:03 -04:00
|
|
|
Project.last
|
2014-08-16 16:45:11 -04:00
|
|
|
redirect_to "/"
|
2013-02-07 17:54:46 -05:00
|
|
|
end
|
|
|
|
|
2011-05-22 14:49:31 -04:00
|
|
|
def redirect
|
|
|
|
Project.all
|
|
|
|
redirect_to :action => 'show'
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_after_render
|
|
|
|
render :inline => "Hello world"
|
|
|
|
Project.all
|
|
|
|
ActiveRecord::LogSubscriber.runtime += 100
|
|
|
|
end
|
2009-12-30 07:07:48 -05:00
|
|
|
end
|
2010-02-23 20:31:17 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
include ActiveSupport::LogSubscriber::TestHelper
|
2010-02-15 09:44:30 -05:00
|
|
|
tests LogSubscriberController
|
2009-12-30 07:07:48 -05:00
|
|
|
|
|
|
|
def setup
|
2010-03-02 17:40:59 -05:00
|
|
|
super
|
2010-01-12 19:18:23 -05:00
|
|
|
@old_logger = ActionController::Base.logger
|
2010-06-24 07:23:43 -04:00
|
|
|
ActionController::LogSubscriber.attach_to :action_controller
|
2009-12-30 07:07:48 -05:00
|
|
|
end
|
|
|
|
|
2010-01-12 19:18:23 -05:00
|
|
|
def teardown
|
|
|
|
super
|
2010-06-24 07:23:43 -04:00
|
|
|
ActiveSupport::LogSubscriber.log_subscribers.clear
|
2010-01-12 19:18:23 -05:00
|
|
|
ActionController::Base.logger = @old_logger
|
2009-12-30 07:07:48 -05:00
|
|
|
end
|
|
|
|
|
2010-01-12 19:18:23 -05:00
|
|
|
def set_logger(logger)
|
|
|
|
ActionController::Base.logger = logger
|
|
|
|
end
|
2010-02-15 11:20:11 -05:00
|
|
|
|
2009-12-30 07:07:48 -05:00
|
|
|
def test_log_with_active_record
|
|
|
|
get :show
|
|
|
|
wait
|
2010-01-12 19:18:23 -05:00
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 2, @logger.logged(:info).size
|
2011-05-14 05:25:08 -04:00
|
|
|
assert_match(/\(Views: [\d.]+ms \| ActiveRecord: [\d.]+ms\)/, @logger.logged(:info)[1])
|
2009-12-30 07:07:48 -05:00
|
|
|
end
|
2011-05-14 05:31:00 -04:00
|
|
|
|
|
|
|
def test_runtime_reset_before_requests
|
|
|
|
ActiveRecord::LogSubscriber.runtime += 12345
|
|
|
|
get :zero
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal 2, @logger.logged(:info).size
|
|
|
|
assert_match(/\(Views: [\d.]+ms \| ActiveRecord: 0.0ms\)/, @logger.logged(:info)[1])
|
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2013-02-07 17:54:46 -05:00
|
|
|
def test_log_with_active_record_when_post
|
|
|
|
post :create
|
|
|
|
wait
|
|
|
|
assert_match(/ActiveRecord: ([1-9][\d.]+)ms\)/, @logger.logged(:info)[2])
|
|
|
|
end
|
|
|
|
|
2011-05-22 14:49:31 -04:00
|
|
|
def test_log_with_active_record_when_redirecting
|
|
|
|
get :redirect
|
|
|
|
wait
|
|
|
|
assert_equal 3, @logger.logged(:info).size
|
|
|
|
assert_match(/\(ActiveRecord: [\d.]+ms\)/, @logger.logged(:info)[2])
|
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2011-05-22 14:49:31 -04:00
|
|
|
def test_include_time_query_time_after_rendering
|
|
|
|
get :db_after_render
|
|
|
|
wait
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2011-05-22 14:49:31 -04:00
|
|
|
assert_equal 2, @logger.logged(:info).size
|
|
|
|
assert_match(/\(Views: [\d.]+ms \| ActiveRecord: ([1-9][\d.]+)ms\)/, @logger.logged(:info)[1])
|
|
|
|
end
|
2010-09-22 15:03:39 -04:00
|
|
|
end
|