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]"
Constant Summary collapse
- DEFAULT_OPTIONS =
{ 'retry' => true, 'queue' => 'default' }
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all jobs for this worker.
-
#client_push(opts) ⇒ Object
:nodoc:.
-
#client_push_old ⇒ Object
:nodoc:.
-
#drain ⇒ Object
Drain and run all jobs for this worker.
-
#get_sidekiq_options ⇒ Object
:nodoc:.
-
#jobs ⇒ Object
Jobs queued for this worker.
- #perform_async(*args) ⇒ Object
- #perform_in(interval, *args) ⇒ Object (also: #perform_at)
-
#sidekiq_options(opts = {}) ⇒ Object
Allows customization for this type of Worker.
-
#stringify_keys(hash) ⇒ Object
:nodoc:.
Instance Method Details
#clear ⇒ Object
Clear all jobs for this worker
72 73 74 |
# File 'lib/sidekiq/testing.rb', line 72 def clear jobs.clear end |
#client_push(opts) ⇒ Object
:nodoc:
71 72 73 |
# File 'lib/sidekiq/worker.rb', line 71 def client_push(item) # :nodoc: Sidekiq::Client.push(stringify_keys(item)) end |
#client_push_old ⇒ Object
:nodoc:
59 60 61 |
# File 'lib/sidekiq/testing.rb', line 59 def client_push(item) # :nodoc: Sidekiq::Client.push(stringify_keys(item)) end |
#drain ⇒ Object
Drain and run all jobs for this worker
77 78 79 80 81 |
# File 'lib/sidekiq/testing.rb', line 77 def drain while job = jobs.shift do new.perform(*job['args']) end end |
#get_sidekiq_options ⇒ Object
:nodoc:
60 61 62 |
# File 'lib/sidekiq/worker.rb', line 60 def # :nodoc: self. ||= DEFAULT_OPTIONS end |
#jobs ⇒ Object
Jobs queued for this worker
67 68 69 |
# File 'lib/sidekiq/testing.rb', line 67 def jobs Worker.jobs[self] end |
#perform_async(*args) ⇒ Object
34 35 36 |
# File 'lib/sidekiq/worker.rb', line 34 def perform_async(*args) client_push('class' => self, 'args' => args) end |
#perform_in(interval, *args) ⇒ Object Also known as: perform_at
38 39 40 41 42 |
# File 'lib/sidekiq/worker.rb', line 38 def perform_in(interval, *args) int = interval.to_f ts = (int < 1_000_000_000 ? Time.now.to_f + int : int) client_push('class' => self, 'args' => args, 'at' => ts) 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, default *true*
:timeout - timeout the perform method after N seconds, default *nil*
: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*
54 55 56 |
# File 'lib/sidekiq/worker.rb', line 54 def (opts={}) self. = .merge(stringify_keys(opts || {})) end |
#stringify_keys(hash) ⇒ Object
:nodoc:
64 65 66 67 68 69 |
# File 'lib/sidekiq/worker.rb', line 64 def stringify_keys(hash) # :nodoc: hash.keys.each do |key| hash[key.to_s] = hash.delete(key) end hash end |