Skip to content

Commit 92410a1

Browse files
authored
Merge pull request rails#53169 from zzak/re-53160
Add test coverage for Active Job log levels
2 parents aeb0828 + 7c73420 commit 92410a1

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

activejob/test/cases/logging_test.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,109 @@ def test_enqueue_all_job_logging
368368
ActiveJob.perform_all_later(LoggingJob.new("Dummy"), HelloJob.new("Jamie"), HelloJob.new("John"))
369369
assert_match(/Enqueued 3 jobs to .+ \(2 HelloJob, 1 LoggingJob\)/, @logger.messages)
370370
end
371+
372+
def test_enqueue_log_level
373+
@logger.level = WARN
374+
HelloJob.perform_later "Dummy"
375+
assert_no_match(/HelloJob/, @logger.messages)
376+
assert_empty @logger.messages
377+
378+
@logger.level = INFO
379+
LoggingJob.perform_later "Dummy"
380+
assert_match(/Enqueued LoggingJob \(Job ID: .*?\) to .*? with arguments:.*Dummy/, @logger.messages)
381+
end
382+
383+
unless adapter_is?(:inline, :sneakers)
384+
def test_enqueue_at_log_level
385+
@logger.level = WARN
386+
HelloJob.set(wait_until: 24.hours.from_now).perform_later "Cristian"
387+
assert_no_match(/HelloJob/, @logger.messages)
388+
assert_empty @logger.messages
389+
390+
@logger.level = INFO
391+
LoggingJob.set(wait_until: 24.hours.from_now).perform_later "Dummy"
392+
assert_match(/Enqueued LoggingJob \(Job ID: .*\) to .*? at.*Dummy/, @logger.messages)
393+
end
394+
end
395+
396+
def test_enqueue_all_log_level
397+
@logger.level = WARN
398+
ActiveJob.perform_all_later(LoggingJob.new("Dummy"), HelloJob.new("Jamie"), HelloJob.new("John"))
399+
assert_no_match(/\(2 HelloJob, 1 LoggingJob\)/, @logger.messages)
400+
assert_empty @logger.messages
401+
402+
@logger.level = INFO
403+
ActiveJob.perform_all_later(LoggingJob.new("Dummy"), HelloJob.new("Jamie"), HelloJob.new("John"))
404+
assert_match(/Enqueued 3 jobs to .+ \(2 HelloJob, 1 LoggingJob\)/, @logger.messages)
405+
end
406+
407+
def test_perform_start_log_level
408+
@logger.level = WARN
409+
perform_enqueued_jobs { LoggingJob.perform_later "Dummy" }
410+
assert_no_match(/LoggingJob/, @logger.messages)
411+
assert_empty @logger.messages
412+
413+
@logger.level = INFO
414+
perform_enqueued_jobs { LoggingJob.perform_later "Dummy" }
415+
assert_match(/Performing LoggingJob \(Job ID: .*?\) from .*? with arguments:.*Dummy/, @logger.messages)
416+
end
417+
418+
def test_perform_log_level
419+
@logger.level = WARN
420+
perform_enqueued_jobs { LoggingJob.perform_later "Dummy" }
421+
assert_no_match(/Dummy, here is it: Dummy/, @logger.messages)
422+
assert_empty @logger.messages
423+
424+
@logger.level = INFO
425+
perform_enqueued_jobs { LoggingJob.perform_later "Dummy" }
426+
assert_match(/Dummy, here is it: Dummy/, @logger.messages)
427+
end
428+
429+
unless adapter_is?(:inline, :sneakers)
430+
def test_enqueue_retry_log_level
431+
@logger.level = WARN
432+
perform_enqueued_jobs { RetryJob.perform_later "DefaultsError", 2 }
433+
assert_no_match(/RetryJob/, @logger.messages)
434+
assert_empty @logger.messages
435+
436+
@logger.level = INFO
437+
perform_enqueued_jobs { RetryJob.perform_later "DefaultsError", 2 }
438+
assert_match(/Retrying RetryJob \(Job ID: .*?\) after \d+ attempts in 3 seconds, due to a DefaultsError.*\./, @logger.messages)
439+
end
440+
end
441+
442+
def test_enqueue_retry_log_level_on_retry_job
443+
@logger.level = WARN
444+
perform_enqueued_jobs { RescueJob.perform_later "david" }
445+
assert_no_match(/RescueJob/, @logger.messages)
446+
assert_empty @logger.messages
447+
448+
@logger.level = INFO
449+
perform_enqueued_jobs { RescueJob.perform_later "david" }
450+
assert_match(/Retrying RescueJob \(Job ID: .*?\) after \d+ attempts in 0 seconds\./, @logger.messages)
451+
end
452+
453+
unless adapter_is?(:inline, :sneakers)
454+
def test_retry_stopped_log_level
455+
@logger.level = FATAL
456+
perform_enqueued_jobs { RetryJob.perform_later "CustomCatchError", 6 }
457+
assert_no_match(/RetryJob/, @logger.messages)
458+
assert_empty @logger.messages
459+
460+
@logger.level = ERROR
461+
perform_enqueued_jobs { RetryJob.perform_later "CustomCatchError", 6 }
462+
assert_match(/Stopped retrying RetryJob \(Job ID: .*?\) due to a CustomCatchError.*, which reoccurred on \d+ attempts\./, @logger.messages)
463+
end
464+
end
465+
466+
def test_discard_log_level
467+
@logger.level = FATAL
468+
perform_enqueued_jobs { RetryJob.perform_later "DiscardableError", 2 }
469+
assert_no_match(/RetryJob/, @logger.messages)
470+
assert_empty @logger.messages
471+
472+
@logger.level = ERROR
473+
perform_enqueued_jobs { RetryJob.perform_later "DiscardableError", 2 }
474+
assert_match(/Discarded RetryJob \(Job ID: .*?\) due to a DiscardableError.*\./, @logger.messages)
475+
end
371476
end

0 commit comments

Comments
 (0)