From 6eb163a706635c0ab5ede9056a0720f1a5f18f22 Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Thu, 22 May 2014 23:38:01 +0300 Subject: Tagged logging --- test/cases/logging_test.rb | 57 +++++++++++++++++++++++++++++++++++++++++----- test/jobs/logging_job.rb | 10 ++++++++ test/jobs/nested_job.rb | 10 ++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 test/jobs/logging_job.rb create mode 100644 test/jobs/nested_job.rb (limited to 'test') diff --git a/test/cases/logging_test.rb b/test/cases/logging_test.rb index fa9430436d..194944db11 100644 --- a/test/cases/logging_test.rb +++ b/test/cases/logging_test.rb @@ -1,15 +1,30 @@ require 'helper' require "active_support/log_subscriber/test_helper" +require 'jobs/logging_job' +require 'jobs/nested_job' class AdapterTest < ActiveSupport::TestCase include ActiveSupport::LogSubscriber::TestHelper include ActiveSupport::Logger::Severity + class TestLogger < ActiveSupport::Logger + def initialize + @file = StringIO.new + super(@file) + end + + def messages + @file.rewind + @file.read + end + end + def setup super $BUFFER = [] @old_logger = ActiveJob::Base.logger - ActiveJob::Base.logger = @logger + @logger = ActiveSupport::TaggedLogging.new(TestLogger.new) + set_logger @logger ActiveJob::Logging::LogSubscriber.attach_to :active_job end @@ -23,26 +38,56 @@ class AdapterTest < ActiveSupport::TestCase ActiveJob::Base.logger = logger end + + def test_uses_active_job_as_tag + HelloJob.enqueue "Cristian" + assert_match(/\[ActiveJob\]/, @logger.messages) + end + def test_enqueue_job_logging HelloJob.enqueue "Cristian" - assert_match(/Enqueued HelloJob to .*?:.*Cristian/, @logger.logged(:info).join) + assert_match(/Enqueued HelloJob to .*?:.*Cristian/, @logger.messages) end def test_perform_job_logging - HelloJob.enqueue "Cristian" - assert_match(/Performed HelloJob from .*?:.*Cristian/, @logger.logged(:info).join) + LoggingJob.enqueue "Dummy" + assert_match(/Performing LoggingJob from .*? with arguments:.*Dummy/, @logger.messages) + assert_match(/Dummy, here is it: Dummy/, @logger.messages) + assert_match(/Performed LoggingJob from .*? in .*ms/, @logger.messages) + end + + def test_perform_uses_job_name_job_logging + LoggingJob.enqueue "Dummy" + assert_match(/\[LoggingJob\]/, @logger.messages) + end + + def test_perform_uses_job_id_job_logging + LoggingJob.enqueue "Dummy" + assert_match(/\[LOGGING-JOB-ID\]/, @logger.messages) + end + + def test_perform_nested_jobs_logging + NestedJob.enqueue + assert_match(/\[LoggingJob\] \[.*?\]/, @logger.messages) + assert_match(/\[ActiveJob\] Enqueued NestedJob to/, @logger.messages) + assert_match(/\[ActiveJob\] \[NestedJob\] \[NESTED-JOB-ID\] Performing NestedJob from/, @logger.messages) + assert_match(/\[ActiveJob\] \[NestedJob\] \[NESTED-JOB-ID\] Enqueued LoggingJob to .* with arguments: "NestedJob"/, @logger.messages) + assert_match(/\[ActiveJob\].*\[LoggingJob\] \[LOGGING-JOB-ID\] Performing LoggingJob 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 from .* in/, @logger.messages) + assert_match(/\[ActiveJob\] \[NestedJob\] \[NESTED-JOB-ID\] Performed NestedJob from .* in/, @logger.messages) end def test_enqueue_at_job_logging HelloJob.enqueue_at 1, "Cristian" - assert_match(/Enqueued HelloJob to .*? at.*Cristian/, @logger.logged(:info).join) + assert_match(/Enqueued HelloJob to .*? at.*Cristian/, @logger.messages) rescue NotImplementedError skip end def test_enqueue_in_job_logging HelloJob.enqueue_in 2, "Cristian" - assert_match(/Enqueued HelloJob to .*? at.*Cristian/, @logger.logged(:info).join) + assert_match(/Enqueued HelloJob to .*? at.*Cristian/, @logger.messages) rescue NotImplementedError skip end diff --git a/test/jobs/logging_job.rb b/test/jobs/logging_job.rb new file mode 100644 index 0000000000..d84ed8589b --- /dev/null +++ b/test/jobs/logging_job.rb @@ -0,0 +1,10 @@ +class LoggingJob < ActiveJob::Base + def perform(dummy) + logger.info "Dummy, here is it: #{dummy}" + end + + def job_id + "LOGGING-JOB-ID" + end +end + diff --git a/test/jobs/nested_job.rb b/test/jobs/nested_job.rb new file mode 100644 index 0000000000..fd66f68991 --- /dev/null +++ b/test/jobs/nested_job.rb @@ -0,0 +1,10 @@ +class NestedJob < ActiveJob::Base + def perform + LoggingJob.enqueue "NestedJob" + end + + def job_id + "NESTED-JOB-ID" + end +end + -- cgit v1.2.3