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.delayed.send_welcome_email('[email protected]')
assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size

Instance Method Summary collapse

Instance Method Details

#get_sidekiq_optionsObject

:nodoc:



48
49
50
# File 'lib/sidekiq/worker.rb', line 48

def get_sidekiq_options # :nodoc:
  @sidekiq_options || { 'unique' => true, 'retry' => true, 'queue' => 'default' }
end

#jobsObject



33
34
35
# File 'lib/sidekiq/testing.rb', line 33

def jobs
  @pushed ||= []
end

#perform_async(*args) ⇒ Object



28
29
30
# File 'lib/sidekiq/worker.rb', line 28

def perform_async(*args)
  Sidekiq::Client.push('class' => self, 'args' => args)
end

#perform_async_oldObject



27
28
29
# File 'lib/sidekiq/testing.rb', line 27

def perform_async(*args)
  Sidekiq::Client.push('class' => self, 'args' => args)
end

#queue(name) ⇒ Object



32
33
34
35
# File 'lib/sidekiq/worker.rb', line 32

def queue(name)
  puts "DEPRECATED: `queue :name` is now `sidekiq_options :queue => :name`"
  Sidekiq::Client.queue_mappings[self.name] = name.to_s
end

#sidekiq_options(opts = {}) ⇒ Object

Allows customization for this type of Worker. Legal options:

:unique - enable the UniqueJobs middleware for this Worker, default *true*
:queue - use a named queue for this Worker, default 'default'
:retry - enable the RetryJobs middleware for this Worker, default *true*


44
45
46
# File 'lib/sidekiq/worker.rb', line 44

def sidekiq_options(opts={})
  @sidekiq_options = get_sidekiq_options.merge(stringify_keys(opts || {}))
end

#stringify_keys(hash) ⇒ Object

:nodoc:



52
53
54
55
56
57
# File 'lib/sidekiq/worker.rb', line 52

def stringify_keys(hash) # :nodoc:
  hash.keys.each do |key|
    hash[key.to_s] = hash.delete(key)
  end
  hash
end