Module: RubyReactor::RSpec::ActiveJobHelpers
- Defined in:
- lib/ruby_reactor/rspec/active_job_helpers.rb
Overview
ActiveJob counterpart to SidekiqHelpers, used internally by
AsyncTestHelpers to drain the ActiveJob :test queue adapter. Only
the static methods are needed (AsyncTestHelpers dispatches to them);
Rails::ActiveJob::TestHelper already covers most spec-author-facing
assertions, so this module is not auto-included into examples.
Defined Under Namespace
Classes: PendingJob
Class Method Summary collapse
-
.drain_async_jobs(max_iterations: 100) ⇒ Object
Drain every queued job in the ActiveJob
:testadapter until the queue is empty. - .pending_async_jobs ⇒ Object
- .test_adapter? ⇒ Boolean
Class Method Details
.drain_async_jobs(max_iterations: 100) ⇒ Object
Drain every queued job in the ActiveJob :test adapter until the
queue is empty. Recursive — handles jobs that re-enqueue themselves
(e.g. ordered_lock snoozes) and job chains that queue additional jobs.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_reactor/rspec/active_job_helpers.rb', line 30 def self.drain_async_jobs(max_iterations: 100) return unless test_adapter? queue = ::ActiveJob::Base.queue_adapter.enqueued_jobs max_iterations.times do break if queue.empty? queue.dup.each do |job| queue.delete(job) job[:job].new(*job[:args]).perform_now end end end |
.pending_async_jobs ⇒ Object
45 46 47 48 49 |
# File 'lib/ruby_reactor/rspec/active_job_helpers.rb', line 45 def self.pending_async_jobs return [] unless test_adapter? ::ActiveJob::Base.queue_adapter.enqueued_jobs.map { |raw| PendingJob.new(raw[:job], raw) } end |
.test_adapter? ⇒ Boolean
22 23 24 25 |
# File 'lib/ruby_reactor/rspec/active_job_helpers.rb', line 22 def self.test_adapter? defined?(::ActiveJob::Base) && ::ActiveJob::Base.queue_adapter.is_a?(::ActiveJob::QueueAdapters::TestAdapter) end |