1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/lib/active_job/queue_adapters/test_adapter.rb

52 lines
1.7 KiB
Ruby
Raw Normal View History

2014-08-26 16:08:49 -04:00
module ActiveJob
module QueueAdapters
2014-09-21 16:20:23 -04:00
# == Test adapter for Active Job
#
# The test adapter should be used only in testing. Along with
# <tt>ActiveJob::TestCase</tt> and <tt>ActiveJob::TestHelper</tt>
# it makes a great tool to test your Rails application.
#
# To use the test adapter set queue_adapter config to +:test+.
#
# Rails.application.config.active_job.queue_adapter = :test
2014-08-26 16:08:49 -04:00
class TestAdapter
2014-09-02 16:27:32 -04:00
delegate :name, to: :class
2014-09-14 18:11:03 -04:00
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs)
attr_writer(:enqueued_jobs, :performed_jobs)
2014-08-26 16:08:49 -04:00
def initialize
self.perform_enqueued_jobs = false
self.perform_enqueued_at_jobs = false
end
2014-08-29 16:11:17 -04:00
# Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
def enqueued_jobs
@enqueued_jobs ||= []
2014-08-29 16:11:17 -04:00
end
2014-08-26 16:08:49 -04:00
2014-08-29 16:11:17 -04:00
# Provides a store of all the performed jobs with the TestAdapter so you can check them.
def performed_jobs
@performed_jobs ||= []
2014-08-29 16:11:17 -04:00
end
2014-08-26 16:08:49 -04:00
2014-09-21 16:20:23 -04:00
def enqueue(job) #:nodoc:
2014-09-14 18:11:03 -04:00
if perform_enqueued_jobs
performed_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name}
Base.execute job.serialize
2014-08-29 16:11:17 -04:00
else
enqueued_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name}
2014-08-26 16:08:49 -04:00
end
2014-08-29 16:11:17 -04:00
end
2014-08-26 16:08:49 -04:00
2014-09-21 16:20:23 -04:00
def enqueue_at(job, timestamp) #:nodoc:
2014-09-14 18:11:03 -04:00
if perform_enqueued_at_jobs
performed_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name, at: timestamp}
Base.execute job.serialize
2014-08-29 16:11:17 -04:00
else
enqueued_jobs << {job: job.class, args: job.serialize['arguments'], queue: job.queue_name, at: timestamp}
2014-08-26 16:08:49 -04:00
end
2014-08-29 16:11:17 -04:00
end
2014-08-26 16:08:49 -04:00
end
end
end