Module: RubyReactor::RSpec::SidekiqHelpers

Defined in:
lib/ruby_reactor/rspec/sidekiq_helpers.rb

Overview

Async-job manipulation helpers. Names like ‘drain_async_jobs` are too generic to live in the global spec namespace, so this module is only auto-included into examples tagged `type: :reactor`. Specs that need it outside that tag should `include RubyReactor::RSpec::SidekiqHelpers` explicitly.

Defined Under Namespace

Classes: PendingJob

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.drain_async_jobs(max_iterations: 100) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_reactor/rspec/sidekiq_helpers.rb', line 45

def self.drain_async_jobs(max_iterations: 100)
  return unless defined?(Sidekiq::Testing)

  max_iterations.times do
    processed_any = false
    worker_classes.each do |worker_class|
      while (job = worker_class.jobs.shift)
        worker_class.new.perform(*job["args"])
        processed_any = true
      end
    end

    break unless processed_any
  end
end

.pending_async_jobsObject



61
62
63
64
65
66
67
# File 'lib/ruby_reactor/rspec/sidekiq_helpers.rb', line 61

def self.pending_async_jobs
  return [] unless defined?(Sidekiq::Testing)

  worker_classes.flat_map do |worker_class|
    worker_class.jobs.map { |raw| PendingJob.new(worker_class, raw) }
  end
end

.worker_classesObject



37
38
39
40
41
42
43
# File 'lib/ruby_reactor/rspec/sidekiq_helpers.rb', line 37

def self.worker_classes
  @worker_classes ||= [
    RubyReactor::SidekiqWorkers::Worker,
    RubyReactor::SidekiqWorkers::MapElementWorker,
    RubyReactor::SidekiqWorkers::MapCollectorWorker
  ]
end

Instance Method Details

#drain_async_jobs(max_iterations: 100) ⇒ Object

Drain every queued async job across all RubyReactor worker classes until the queues are empty. Recursive — handles jobs that re-enqueue themselves (e.g. ordered_lock snoozes) and worker chains that queue additional jobs.



15
16
17
# File 'lib/ruby_reactor/rspec/sidekiq_helpers.rb', line 15

def drain_async_jobs(max_iterations: 100)
  SidekiqHelpers.drain_async_jobs(max_iterations: max_iterations)
end

#pending_async_jobsObject

All currently-pending async jobs, wrapped in ‘PendingJob` so callers can perform individual jobs out-of-order (e.g. to assert ordered_lock’s snooze behavior) without touching Sidekiq internals.



22
23
24
# File 'lib/ruby_reactor/rspec/sidekiq_helpers.rb', line 22

def pending_async_jobs
  SidekiqHelpers.pending_async_jobs
end