1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/test/cases/logging_test.rb

106 lines
3.5 KiB
Ruby
Raw Normal View History

2014-05-22 07:19:48 -04:00
require 'helper'
require "active_support/log_subscriber/test_helper"
require 'active_support/core_ext/numeric/time'
2014-08-12 05:29:21 -04:00
require 'jobs/hello_job'
2014-05-22 16:38:01 -04:00
require 'jobs/logging_job'
require 'jobs/nested_job'
2014-05-22 07:19:48 -04:00
class AdapterTest < ActiveSupport::TestCase
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
ActiveJob::Base.logger = @old_logger
end
def set_logger(logger)
ActiveJob::Base.logger = logger
end
2014-05-22 16:38:01 -04:00
def test_uses_active_job_as_tag
2014-08-25 10:34:50 -04: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
LoggingJob.perform_later "Dummy"
assert_match(/\[LoggingJob\]/, @logger.messages)
end
def test_uses_job_id_as_tag
LoggingJob.perform_later "Dummy"
assert_match(/\[LOGGING-JOB-ID\]/, @logger.messages)
end
def test_logs_correct_queue_name
original_queue_name = LoggingJob.queue_name
LoggingJob.queue_as :php_jobs
LoggingJob.perform_later("Dummy")
assert_match(/to .*?\(php_jobs\).*/, @logger.messages)
ensure
LoggingJob.queue_name = original_queue_name
end
2014-05-22 07:19:48 -04:00
def test_enqueue_job_logging
2014-08-25 10:34:50 -04:00
HelloJob.perform_later "Cristian"
2014-05-30 19:19:30 -04:00
assert_match(/Enqueued HelloJob \(Job ID: .*?\) to .*?:.*Cristian/, @logger.messages)
2014-05-22 07:19:48 -04:00
end
def test_perform_job_logging
2014-08-25 10:34:50 -04:00
LoggingJob.perform_later "Dummy"
2014-05-22 16:38:01 -04:00
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_nested_jobs_logging
2014-08-25 10:34:50 -04:00
NestedJob.perform_later
2014-05-22 16:38:01 -04:00
assert_match(/\[LoggingJob\] \[.*?\]/, @logger.messages)
2014-05-30 19:19:30 -04:00
assert_match(/\[ActiveJob\] Enqueued NestedJob \(Job ID: .*\) to/, @logger.messages)
2014-05-22 16:38:01 -04:00
assert_match(/\[ActiveJob\] \[NestedJob\] \[NESTED-JOB-ID\] Performing NestedJob from/, @logger.messages)
2014-05-30 19:19:30 -04:00
assert_match(/\[ActiveJob\] \[NestedJob\] \[NESTED-JOB-ID\] Enqueued LoggingJob \(Job ID: .*?\) to .* with arguments: "NestedJob"/, @logger.messages)
2014-05-22 16:38:01 -04:00
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)
2014-05-22 07:19:48 -04:00
end
def test_enqueue_at_job_logging
2014-08-25 10:34:50 -04:00
HelloJob.set(wait_until: 24.hours.from_now).perform_later "Cristian"
2014-05-30 19:19:30 -04:00
assert_match(/Enqueued HelloJob \(Job ID: .*\) to .*? at.*Cristian/, @logger.messages)
2014-05-22 07:19:48 -04:00
rescue NotImplementedError
skip
end
def test_enqueue_in_job_logging
2014-08-25 10:34:50 -04:00
HelloJob.set(wait: 2.seconds).perform_later "Cristian"
2014-05-30 19:19:30 -04:00
assert_match(/Enqueued HelloJob \(Job ID: .*\) to .*? at.*Cristian/, @logger.messages)
2014-05-22 07:19:48 -04:00
rescue NotImplementedError
skip
end
end