Module: Sidekiq::Worker::ClassMethods
- Defined in:
- lib/sidekiq/testing.rb,
lib/sidekiq/worker.rb
Overview
The Sidekiq testing infrastructure overrides perform_async so that it does not actually touch the network. Instead it stores the asynchronous jobs in a per-class array so that their presence/absence can be asserted by your tests.
This is similar to ActionMailer’s :test delivery_method and its ActionMailer::Base.deliveries array.
Example:
require 'sidekiq/testing'
assert_equal 0, HardWorker.jobs.size
HardWorker.perform_async(:something)
assert_equal 1, HardWorker.jobs.size
assert_equal :something, HardWorker.jobs[0]['args'][0]
assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
MyMailer.delay.send_welcome_email('[email protected]')
assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
You can also clear and drain all workers’ jobs:
assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
assert_equal 0, Sidekiq::Extensions::DelayedModel.jobs.size
MyMailer.delay.send_welcome_email('[email protected]')
MyModel.delay.do_something_hard
assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
assert_equal 1, Sidekiq::Extensions::DelayedModel.jobs.size
Sidekiq::Worker.clear_all # or .drain_all
assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
assert_equal 0, Sidekiq::Extensions::DelayedModel.jobs.size
This can be useful to make sure jobs don’t linger between tests:
RSpec.configure do |config|
config.before(:each) do
Sidekiq::Worker.clear_all
end
end
or for acceptance testing, i.e. with cucumber:
AfterStep do
Sidekiq::Worker.drain_all
end
When I sign up as "[email protected]"
Then I should receive a welcome email to "[email protected]"
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all jobs for this worker.
-
#client_push(item) ⇒ Object
:nodoc:.
- #delay(*args) ⇒ Object
- #delay_for(*args) ⇒ Object
- #delay_until(*args) ⇒ Object
-
#drain ⇒ Object
Drain and run all jobs for this worker.
- #execute_job(worker, args) ⇒ Object
-
#get_sidekiq_options ⇒ Object
:nodoc:.
-
#jobs ⇒ Object
Jobs queued for this worker.
- #perform_async(*args) ⇒ Object
-
#perform_in(interval, *args) ⇒ Object
(also: #perform_at)
interval
must be a timestamp, numeric or something that acts numeric (like an activesupport time interval). -
#perform_one ⇒ Object
Pop out a single job and perform it.
- #process_job(job) ⇒ Object
-
#queue ⇒ Object
Queue for this worker.
- #set(options) ⇒ Object
-
#sidekiq_options(opts = {}) ⇒ Object
Allows customization for this type of Worker.
- #sidekiq_retries_exhausted(&block) ⇒ Object
- #sidekiq_retry_in(&block) ⇒ Object
Instance Method Details
#clear ⇒ Object
Clear all jobs for this worker
259 260 261 |
# File 'lib/sidekiq/testing.rb', line 259 def clear Queues.clear_for(queue, self.to_s) end |
#client_push(item) ⇒ Object
:nodoc:
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sidekiq/worker.rb', line 108 def client_push(item) # :nodoc: pool = Thread.current[:sidekiq_via_pool] || ['pool'] || Sidekiq.redis_pool hash = if Thread.current[:sidekiq_worker_set] x, Thread.current[:sidekiq_worker_set] = Thread.current[:sidekiq_worker_set], nil x.stringify_keys.merge(item.stringify_keys) else item.stringify_keys end Sidekiq::Client.new(pool).push(hash) end |
#delay(*args) ⇒ Object
42 43 44 |
# File 'lib/sidekiq/worker.rb', line 42 def delay(*args) raise ArgumentError, "Do not call .delay on a Sidekiq::Worker class, call .perform_async" end |
#delay_for(*args) ⇒ Object
46 47 48 |
# File 'lib/sidekiq/worker.rb', line 46 def delay_for(*args) raise ArgumentError, "Do not call .delay_for on a Sidekiq::Worker class, call .perform_in" end |
#delay_until(*args) ⇒ Object
50 51 52 |
# File 'lib/sidekiq/worker.rb', line 50 def delay_until(*args) raise ArgumentError, "Do not call .delay_until on a Sidekiq::Worker class, call .perform_at" end |
#drain ⇒ Object
Drain and run all jobs for this worker
264 265 266 267 268 269 270 |
# File 'lib/sidekiq/testing.rb', line 264 def drain while jobs.any? next_job = jobs.first Queues.delete_for(next_job["jid"], next_job["queue"], self.to_s) process_job(next_job) end end |
#execute_job(worker, args) ⇒ Object
289 290 291 |
# File 'lib/sidekiq/testing.rb', line 289 def execute_job(worker, args) worker.perform(*args) end |
#get_sidekiq_options ⇒ Object
:nodoc:
104 105 106 |
# File 'lib/sidekiq/worker.rb', line 104 def # :nodoc: self. ||= Sidekiq. end |
#jobs ⇒ Object
Jobs queued for this worker
254 255 256 |
# File 'lib/sidekiq/testing.rb', line 254 def jobs Queues.jobs_by_worker[self.to_s] end |
#perform_async(*args) ⇒ Object
59 60 61 |
# File 'lib/sidekiq/worker.rb', line 59 def perform_async(*args) client_push('class' => self, 'args' => args) end |
#perform_in(interval, *args) ⇒ Object Also known as: perform_at
interval
must be a timestamp, numeric or something that acts
numeric (like an activesupport time interval).
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sidekiq/worker.rb', line 65 def perform_in(interval, *args) int = interval.to_f now = Time.now ts = (int < 1_000_000_000 ? (now + interval).to_f : int) item = { 'class' => self, 'args' => args, 'at' => ts } # Optimization to enqueue something now that is scheduled to go out now or in the past item.delete('at'.freeze) if ts <= now.to_f client_push(item) end |
#perform_one ⇒ Object
Pop out a single job and perform it
273 274 275 276 277 278 |
# File 'lib/sidekiq/testing.rb', line 273 def perform_one raise(EmptyQueueError, "perform_one called with empty job queue") if jobs.empty? next_job = jobs.first Queues.delete_for(next_job["jid"], queue, self.to_s) process_job(next_job) end |
#process_job(job) ⇒ Object
280 281 282 283 284 285 286 287 |
# File 'lib/sidekiq/testing.rb', line 280 def process_job(job) worker = new worker.jid = job['jid'] worker.bid = job['bid'] if worker.respond_to?(:bid=) Sidekiq::Testing.server_middleware.invoke(worker, job, job['queue']) do execute_job(worker, job['args']) end end |
#queue ⇒ Object
Queue for this worker
249 250 251 |
# File 'lib/sidekiq/testing.rb', line 249 def queue self.["queue"] end |
#set(options) ⇒ Object
54 55 56 57 |
# File 'lib/sidekiq/worker.rb', line 54 def set() Thread.current[:sidekiq_worker_set] = self end |
#sidekiq_options(opts = {}) ⇒ Object
Allows customization for this type of Worker. Legal options:
queue - use a named queue for this Worker, default 'default'
retry - enable the RetryJobs middleware for this Worker, *true* to use the default
or *Integer* count
backtrace - whether to save any error backtrace in the retry payload to display in web UI,
can be true, false or an integer number of lines to save, default *false*
pool - use the given Redis connection pool to push this type of job to a given shard.
In practice, any option is allowed. This is the main mechanism to configure the options for a specific job.
92 93 94 |
# File 'lib/sidekiq/worker.rb', line 92 def (opts={}) self. = .merge(opts.stringify_keys) end |
#sidekiq_retries_exhausted(&block) ⇒ Object
100 101 102 |
# File 'lib/sidekiq/worker.rb', line 100 def sidekiq_retries_exhausted(&block) self.sidekiq_retries_exhausted_block = block end |
#sidekiq_retry_in(&block) ⇒ Object
96 97 98 |
# File 'lib/sidekiq/worker.rb', line 96 def sidekiq_retry_in(&block) self.sidekiq_retry_in_block = block end |