2017-07-09 13:49:52 -04:00
# frozen_string_literal: true
2017-07-10 09:40:03 -04:00
2016-08-06 12:41:18 -04:00
require " helper "
2014-05-22 07:19:48 -04:00
require " active_support/log_subscriber/test_helper "
2016-08-06 12:41:18 -04:00
require " active_support/core_ext/numeric/time "
require " jobs/hello_job "
require " jobs/logging_job "
require " jobs/overridden_logging_job "
require " jobs/nested_job "
2017-04-20 19:02:26 -04:00
require " jobs/rescue_job "
2018-08-30 13:04:29 -04:00
require " jobs/retry_job "
2016-08-06 12:41:18 -04:00
require " models/person "
2014-05-22 07:19:48 -04:00
2015-03-11 18:40:24 -04:00
class LoggingTest < ActiveSupport :: TestCase
2018-08-30 13:04:29 -04:00
include ActiveJob :: TestHelper
2014-05-22 07:19:48 -04:00
include ActiveSupport :: LogSubscriber :: TestHelper
include ActiveSupport :: Logger :: Severity
2014-05-22 16:38:01 -04:00
class TestLogger < ActiveSupport :: Logger
def initialize
@file = StringIO . new
super ( @file )
end
def messages
@file . rewind
@file . read
end
end
2014-05-22 07:19:48 -04:00
def setup
super
2014-08-17 09:23:24 -04:00
JobBuffer . clear
2014-05-22 07:19:48 -04:00
@old_logger = ActiveJob :: Base . logger
2014-05-22 16:38:01 -04:00
@logger = ActiveSupport :: TaggedLogging . new ( TestLogger . new )
set_logger @logger
2014-05-22 07:19:48 -04:00
ActiveJob :: Logging :: LogSubscriber . attach_to :active_job
end
def teardown
super
ActiveJob :: Logging :: LogSubscriber . log_subscribers . pop
2014-10-20 03:08:11 -04:00
set_logger @old_logger
2014-05-22 07:19:48 -04:00
end
def set_logger ( logger )
ActiveJob :: Base . logger = logger
end
2018-06-20 15:04:07 -04:00
def subscribed
[ ] . tap do | events |
ActiveSupport :: Notifications . subscribed ( - > ( * args ) { events << args } , / enqueue.* \ .active_job / ) do
yield
end
end
end
2014-05-22 16:38:01 -04:00
def test_uses_active_job_as_tag
2014-12-01 11:13:49 -05:00
HelloJob . perform_later " Cristian "
2014-05-22 16:38:01 -04:00
assert_match ( / \ [ActiveJob \ ] / , @logger . messages )
end
2014-08-25 10:34:50 -04:00
def test_uses_job_name_as_tag
2018-08-30 13:04:29 -04:00
perform_enqueued_jobs do
LoggingJob . perform_later " Dummy "
assert_match ( / \ [LoggingJob \ ] / , @logger . messages )
end
2014-08-25 10:34:50 -04:00
end
def test_uses_job_id_as_tag
2018-08-30 13:04:29 -04:00
perform_enqueued_jobs do
LoggingJob . perform_later " Dummy "
assert_match ( / \ [LOGGING-JOB-ID \ ] / , @logger . messages )
end
2014-08-25 10:34:50 -04:00
end
def test_logs_correct_queue_name
original_queue_name = LoggingJob . queue_name
LoggingJob . queue_as :php_jobs
2014-12-01 11:13:49 -05:00
LoggingJob . perform_later ( " Dummy " )
2014-08-25 10:34:50 -04:00
assert_match ( / to .*? \ (php_jobs \ ).* / , @logger . messages )
ensure
LoggingJob . queue_name = original_queue_name
end
2014-11-28 14:19:16 -05:00
def test_globalid_parameter_logging
2018-08-30 13:04:29 -04:00
perform_enqueued_jobs do
person = Person . new ( 123 )
LoggingJob . perform_later person
assert_match ( %r{ Enqueued.*gid://aj/Person/123 } , @logger . messages )
assert_match ( %r{ Dummy, here is it: # <Person:.*> } , @logger . messages )
assert_match ( %r{ Performing.*gid://aj/Person/123 } , @logger . messages )
end
2014-11-28 14:19:16 -05:00
end
2015-09-06 17:39:42 -04:00
def test_globalid_nested_parameter_logging
2018-08-30 13:04:29 -04:00
perform_enqueued_jobs do
person = Person . new ( 123 )
LoggingJob . perform_later ( person : person )
assert_match ( %r{ Enqueued.*gid://aj/Person/123 } , @logger . messages )
assert_match ( %r{ Dummy, here is it: .* # <Person:.*> } , @logger . messages )
assert_match ( %r{ Performing.*gid://aj/Person/123 } , @logger . messages )
end
2015-09-06 17:39:42 -04:00
end
2014-05-22 07:19:48 -04:00
def test_enqueue_job_logging
2018-06-20 15:04:07 -04:00
events = subscribed { HelloJob . perform_later " Cristian " }
2014-12-01 11:13:49 -05:00
assert_match ( / Enqueued HelloJob \ (Job ID: .*? \ ) to .*?:.*Cristian / , @logger . messages )
2018-06-20 15:04:07 -04:00
assert_equal ( events . count , 1 )
key , * = events . first
assert_equal ( key , " enqueue.active_job " )
2014-05-22 07:19:48 -04:00
end
def test_perform_job_logging
2018-08-30 13:04:29 -04:00
perform_enqueued_jobs do
LoggingJob . perform_later " Dummy "
assert_match ( / Performing LoggingJob \ (Job ID: .*? \ ) from .*? with arguments:.*Dummy / , @logger . messages )
assert_match ( / Dummy, here is it: Dummy / , @logger . messages )
assert_match ( / Performed LoggingJob \ (Job ID: .*? \ ) from .*? in .*ms / , @logger . messages )
end
2014-05-22 16:38:01 -04:00
end
def test_perform_nested_jobs_logging
2018-08-30 13:04:29 -04:00
perform_enqueued_jobs do
NestedJob . perform_later
assert_match ( / \ [LoggingJob \ ] \ [.*? \ ] / , @logger . messages )
assert_match ( / \ [ActiveJob \ ] Enqueued NestedJob \ (Job ID: .* \ ) to / , @logger . messages )
assert_match ( / \ [ActiveJob \ ] \ [NestedJob \ ] \ [NESTED-JOB-ID \ ] Performing NestedJob \ (Job ID: .*? \ ) from / , @logger . messages )
assert_match ( / \ [ActiveJob \ ] \ [NestedJob \ ] \ [NESTED-JOB-ID \ ] Enqueued LoggingJob \ (Job ID: .*? \ ) to .* with arguments: "NestedJob" / , @logger . messages )
assert_match ( / \ [ActiveJob \ ].* \ [LoggingJob \ ] \ [LOGGING-JOB-ID \ ] Performing LoggingJob \ (Job ID: .*? \ ) from .* with arguments: "NestedJob" / , @logger . messages )
assert_match ( / \ [ActiveJob \ ].* \ [LoggingJob \ ] \ [LOGGING-JOB-ID \ ] Dummy, here is it: NestedJob / , @logger . messages )
assert_match ( / \ [ActiveJob \ ].* \ [LoggingJob \ ] \ [LOGGING-JOB-ID \ ] Performed LoggingJob \ (Job ID: .*? \ ) from .* in / , @logger . messages )
assert_match ( / \ [ActiveJob \ ] \ [NestedJob \ ] \ [NESTED-JOB-ID \ ] Performed NestedJob \ (Job ID: .*? \ ) from .* in / , @logger . messages )
end
2014-05-22 07:19:48 -04:00
end
def test_enqueue_at_job_logging
2018-06-20 15:04:07 -04:00
events = subscribed { HelloJob . set ( wait_until : 24 . hours . from_now ) . perform_later " Cristian " }
2014-12-01 11:13:49 -05:00
assert_match ( / Enqueued HelloJob \ (Job ID: .* \ ) to .*? at.*Cristian / , @logger . messages )
2018-06-20 15:04:07 -04:00
assert_equal ( events . count , 1 )
key , * = events . first
assert_equal ( key , " enqueue_at.active_job " )
2014-05-22 07:19:48 -04:00
rescue NotImplementedError
skip
end
def test_enqueue_in_job_logging
2018-06-20 15:04:07 -04:00
events = subscribed { HelloJob . set ( wait : 2 . seconds ) . perform_later " Cristian " }
2014-12-01 11:13:49 -05:00
assert_match ( / Enqueued HelloJob \ (Job ID: .* \ ) to .*? at.*Cristian / , @logger . messages )
2018-06-20 15:04:07 -04:00
assert_equal ( events . count , 1 )
key , * = events . first
assert_equal ( key , " enqueue_at.active_job " )
2014-05-22 07:19:48 -04:00
rescue NotImplementedError
skip
end
2016-06-17 12:01:03 -04:00
def test_for_tagged_logger_support_is_consistent
set_logger :: Logger . new ( nil )
OverriddenLoggingJob . perform_later " Dummy "
end
2017-03-27 19:16:50 -04:00
def test_job_error_logging
2018-09-09 00:46:43 -04:00
perform_enqueued_jobs { RescueJob . perform_later " other " }
2017-03-27 19:16:50 -04:00
rescue RescueJob :: OtherError
assert_match ( / Performing RescueJob \ (Job ID: .*? \ ) from .*? with arguments:.*other / , @logger . messages )
assert_match ( / Error performing RescueJob \ (Job ID: .*? \ ) from .*? in .*ms: RescueJob::OtherError \ (Bad hair \ ): \ n.* \ brescue_job \ .rb: \ d+:in `perform' / , @logger . messages )
end
2018-08-30 13:04:29 -04:00
def test_enqueue_retry_logging
perform_enqueued_jobs do
RetryJob . perform_later " DefaultsError " , 2
assert_match ( / Retrying RetryJob in \ d+ seconds, due to a DefaultsError \ . The original exception was nil \ . / , @logger . messages )
end
end
2018-09-16 10:39:55 -04:00
def test_enqueue_retry_logging_on_retry_job
perform_enqueued_jobs { RescueJob . perform_later " david " }
assert_match ( / Retrying RescueJob in nil seconds, due to a nil \ . The original exception was nil \ . / , @logger . messages )
end
2018-08-30 13:04:29 -04:00
def test_retry_stopped_logging
perform_enqueued_jobs do
RetryJob . perform_later " CustomCatchError " , 6
assert_match ( / Stopped retrying RetryJob due to a CustomCatchError, which reoccurred on \ d+ attempts \ . The original exception was # <CustomCatchError: CustomCatchError> \ . / , @logger . messages )
end
end
def test_retry_stopped_logging_without_block
perform_enqueued_jobs do
begin
RetryJob . perform_later " DefaultsError " , 6
rescue DefaultsError
assert_match ( / Stopped retrying RetryJob due to a DefaultsError, which reoccurred on \ d+ attempts \ . The original exception was # <DefaultsError: DefaultsError> \ . / , @logger . messages )
end
end
end
def test_discard_logging
perform_enqueued_jobs do
RetryJob . perform_later " DiscardableError " , 2
assert_match ( / Discarded RetryJob due to a DiscardableError \ . The original exception was nil \ . / , @logger . messages )
end
end
2014-05-22 07:19:48 -04:00
end