2012-02-10 00:46:44 -05:00
|
|
|
module Sidekiq
|
|
|
|
module Worker
|
2012-02-10 23:30:14 -05:00
|
|
|
|
|
|
|
##
|
2012-02-14 12:00:26 -05:00
|
|
|
# The Sidekiq testing infrastructure overrides perform_async
|
2012-02-10 23:30:14 -05:00
|
|
|
# so that it does not actually touch the network. Instead it
|
2012-02-14 12:00:26 -05:00
|
|
|
# stores the asynchronous jobs in a per-class array so that
|
2012-02-10 23:30:14 -05:00
|
|
|
# their presence/absence can be asserted by your tests.
|
|
|
|
#
|
|
|
|
# This is similar to ActionMailer's :test delivery_method and its
|
|
|
|
# ActionMailer::Base.deliveries array.
|
2012-02-14 12:00:26 -05:00
|
|
|
#
|
|
|
|
# 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]
|
|
|
|
#
|
2012-04-05 23:06:47 -04:00
|
|
|
# assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
# MyMailer.delayed.send_welcome_email('foo@example.com')
|
|
|
|
# assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
#
|
2012-02-10 00:46:44 -05:00
|
|
|
module ClassMethods
|
2012-02-10 01:33:36 -05:00
|
|
|
alias_method :perform_async_old, :perform_async
|
2012-02-10 00:46:44 -05:00
|
|
|
def perform_async(*args)
|
2012-02-14 12:00:26 -05:00
|
|
|
jobs << { 'class' => self.name, 'args' => args }
|
2012-02-10 23:30:14 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-06-03 14:03:22 -04:00
|
|
|
alias_method :perform_in_old, :perform_in
|
|
|
|
alias_method :perform_at_old, :perform_at
|
|
|
|
def perform_in(interval, *args)
|
|
|
|
int = interval.to_f
|
|
|
|
ts = (int < 1_000_000_000 ? Time.now.to_f + int : int)
|
|
|
|
jobs << { 'class' => self.name, 'args' => args, 'at' => ts }
|
|
|
|
true
|
|
|
|
end
|
|
|
|
alias_method :perform_at, :perform_in
|
|
|
|
|
2012-02-10 23:30:14 -05:00
|
|
|
def jobs
|
|
|
|
@pushed ||= []
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
2012-05-02 13:45:05 -04:00
|
|
|
|
2012-05-02 13:59:31 -04:00
|
|
|
def drain
|
2012-05-02 13:45:05 -04:00
|
|
|
while job = jobs.shift do
|
|
|
|
new.perform(*job['args'])
|
|
|
|
end
|
|
|
|
end
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|