From 802f855ecf95d36982482182adcb007d505e2178 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 23 Sep 2014 21:11:54 -0700 Subject: `ActiveJob::QueueAdapters::TestAdapter` is now a singleton Since `ActiveJob::TestHelper` globally sets `ActiveJob::Base.queue_adapter` on setup, there is no benefit in instantiating a new `TestAdapter` per tests. The original rationale was to allow parallel tests to run without interference, but since they'd all mutate the global `ActiveJob::Base.queue_adapter`, that was never realized. --- .../lib/active_job/queue_adapters/test_adapter.rb | 52 +++++++++++----------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'activejob/lib/active_job/queue_adapters/test_adapter.rb') diff --git a/activejob/lib/active_job/queue_adapters/test_adapter.rb b/activejob/lib/active_job/queue_adapters/test_adapter.rb index c9e2bdca27..7254e06031 100644 --- a/activejob/lib/active_job/queue_adapters/test_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/test_adapter.rb @@ -10,40 +10,39 @@ module ActiveJob # # Rails.application.config.active_job.queue_adapter = :test class TestAdapter - delegate :name, to: :class - attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter) - attr_writer(:enqueued_jobs, :performed_jobs) + class << self + attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter) + attr_writer(:enqueued_jobs, :performed_jobs) - def initialize - self.perform_enqueued_jobs = false - self.perform_enqueued_at_jobs = false - end + # Provides a store of all the enqueued jobs with the TestAdapter so you can check them. + def enqueued_jobs + @enqueued_jobs ||= [] + end - # Provides a store of all the enqueued jobs with the TestAdapter so you can check them. - def enqueued_jobs - @enqueued_jobs ||= [] - end + # Provides a store of all the performed jobs with the TestAdapter so you can check them. + def performed_jobs + @performed_jobs ||= [] + end - # Provides a store of all the performed jobs with the TestAdapter so you can check them. - def performed_jobs - @performed_jobs ||= [] - end + def enqueue(job) #:nodoc: + return if filtered?(job) - def enqueue(job) #:nodoc: - return if filtered?(job) + job_data = job_to_hash(job) + enqueue_or_perform(perform_enqueued_jobs, job, job_data) + end - job_data = { job: job.class, args: job.serialize['arguments'], queue: job.queue_name } - enqueue_or_perform(perform_enqueued_jobs, job, job_data) - end + def enqueue_at(job, timestamp) #:nodoc: + return if filtered?(job) - def enqueue_at(job, timestamp) #:nodoc: - return if filtered?(job) + job_data = job_to_hash(job, at: timestamp) + enqueue_or_perform(perform_enqueued_at_jobs, job, job_data) + end - job_data = { job: job.class, args: job.serialize['arguments'], queue: job.queue_name, at: timestamp } - enqueue_or_perform(perform_enqueued_at_jobs, job, job_data) - end + private - private + def job_to_hash(job, extras = {}) + { job: job.class, args: job.serialize.fetch('arguments'), queue: job.queue_name }.merge(extras) + end def enqueue_or_perform(perform, job, job_data) if perform @@ -57,6 +56,7 @@ module ActiveJob def filtered?(job) filter && !Array(filter).include?(job.class) end + end end end end -- cgit v1.2.3