2019-03-04 14:16:58 -05:00
|
|
|
require_relative 'helper'
|
|
|
|
|
|
|
|
describe Sidekiq::Worker do
|
|
|
|
describe '#set' do
|
|
|
|
|
|
|
|
class SetWorker
|
|
|
|
include Sidekiq::Worker
|
2021-09-28 14:24:17 -04:00
|
|
|
queue_as :foo
|
|
|
|
sidekiq_options 'retry' => 12
|
2019-03-04 14:16:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
|
|
|
|
2021-09-28 14:24:17 -04:00
|
|
|
it "provides basic ActiveJob compatibilility" do
|
|
|
|
q = Sidekiq::ScheduledSet.new
|
|
|
|
assert_equal 0, q.size
|
2021-10-05 17:03:53 -04:00
|
|
|
jid = SetWorker.set(wait_until: 1.hour.from_now).perform_async(123)
|
2021-09-28 14:24:17 -04:00
|
|
|
assert jid
|
|
|
|
assert_equal 1, q.size
|
2021-10-05 17:03:53 -04:00
|
|
|
jid = SetWorker.set(wait: 1.hour).perform_async(123)
|
|
|
|
assert jid
|
|
|
|
assert_equal 2, q.size
|
2021-09-28 14:24:17 -04:00
|
|
|
|
|
|
|
q = Sidekiq::Queue.new("foo")
|
|
|
|
assert_equal 0, q.size
|
|
|
|
SetWorker.perform_async
|
|
|
|
assert_equal 1, q.size
|
|
|
|
|
|
|
|
SetWorker.set(queue: 'xyz').perform_async
|
|
|
|
assert_equal 1, Sidekiq::Queue.new("xyz").size
|
|
|
|
end
|
|
|
|
|
2019-03-04 14:16:58 -05:00
|
|
|
it 'can be memoized' do
|
|
|
|
q = Sidekiq::Queue.new('bar')
|
|
|
|
assert_equal 0, q.size
|
|
|
|
set = SetWorker.set(queue: :bar, foo: 'qaaz')
|
|
|
|
set.perform_async(1)
|
|
|
|
set.perform_async(1)
|
|
|
|
set.perform_async(1)
|
|
|
|
set.perform_async(1)
|
|
|
|
assert_equal 4, q.size
|
|
|
|
assert_equal 4, q.map{|j| j['jid'] }.uniq.size
|
|
|
|
set.perform_in(10, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows option overrides' do
|
|
|
|
q = Sidekiq::Queue.new('bar')
|
|
|
|
assert_equal 0, q.size
|
|
|
|
assert SetWorker.set(queue: :bar).perform_async(1)
|
|
|
|
job = q.first
|
|
|
|
assert_equal 'bar', job['queue']
|
|
|
|
assert_equal 12, job['retry']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles symbols and strings' do
|
|
|
|
q = Sidekiq::Queue.new('bar')
|
|
|
|
assert_equal 0, q.size
|
|
|
|
assert SetWorker.set('queue' => 'bar', :retry => 11).perform_async(1)
|
|
|
|
job = q.first
|
|
|
|
assert_equal 'bar', job['queue']
|
|
|
|
assert_equal 11, job['retry']
|
|
|
|
|
|
|
|
q.clear
|
|
|
|
assert SetWorker.perform_async(1)
|
|
|
|
assert_equal 0, q.size
|
|
|
|
|
|
|
|
q = Sidekiq::Queue.new('foo')
|
|
|
|
job = q.first
|
|
|
|
assert_equal 'foo', job['queue']
|
|
|
|
assert_equal 12, job['retry']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows multiple calls' do
|
|
|
|
SetWorker.set(queue: :foo).set(bar: 'xyz').perform_async
|
|
|
|
|
|
|
|
q = Sidekiq::Queue.new('foo')
|
|
|
|
job = q.first
|
|
|
|
assert_equal 'foo', job['queue']
|
|
|
|
assert_equal 'xyz', job['bar']
|
|
|
|
end
|
2021-11-02 12:54:44 -04:00
|
|
|
|
|
|
|
it 'works with .perform_bulk' do
|
|
|
|
q = Sidekiq::Queue.new('bar')
|
|
|
|
assert_equal 0, q.size
|
|
|
|
|
|
|
|
set = SetWorker.set('queue' => 'bar')
|
|
|
|
jids = set.perform_bulk((1..1_001).to_a.map { |x| Array(x) })
|
|
|
|
|
|
|
|
assert_equal 1_001, q.size
|
|
|
|
assert_equal 1_001, jids.size
|
|
|
|
end
|
2019-03-04 14:16:58 -05:00
|
|
|
end
|
|
|
|
end
|