2012-01-21 19:42:21 -05:00
|
|
|
require 'helper'
|
|
|
|
require 'sidekiq/client'
|
2012-01-25 16:53:00 -05:00
|
|
|
require 'sidekiq/worker'
|
2012-01-21 19:42:21 -05:00
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestClient < Sidekiq::Test
|
2012-01-23 15:56:49 -05:00
|
|
|
describe 'with mock redis' do
|
|
|
|
before do
|
2013-05-12 17:25:30 -04:00
|
|
|
@redis = Minitest::Mock.new
|
2012-04-11 16:17:44 -04:00
|
|
|
def @redis.multi; [yield] * 2 if block_given?; end
|
2012-02-08 20:04:02 -05:00
|
|
|
def @redis.set(*); true; end
|
2012-02-10 23:20:01 -05:00
|
|
|
def @redis.sadd(*); true; end
|
2012-02-11 02:16:12 -05:00
|
|
|
def @redis.srem(*); true; end
|
2012-02-10 23:20:01 -05:00
|
|
|
def @redis.get(*); nil; end
|
2012-02-11 02:16:12 -05:00
|
|
|
def @redis.del(*); nil; end
|
|
|
|
def @redis.incrby(*); nil; end
|
2012-03-22 23:43:14 -04:00
|
|
|
def @redis.setex(*); true; end
|
2012-02-08 20:04:02 -05:00
|
|
|
def @redis.expire(*); true; end
|
2012-03-22 23:43:14 -04:00
|
|
|
def @redis.watch(*); true; end
|
2012-02-17 16:39:36 -05:00
|
|
|
def @redis.with_connection; yield self; end
|
2012-03-14 12:56:13 -04:00
|
|
|
def @redis.with; yield self; end
|
2012-03-22 23:43:14 -04:00
|
|
|
def @redis.exec; true; end
|
2012-03-01 16:41:12 -05:00
|
|
|
Sidekiq.instance_variable_set(:@redis, @redis)
|
2014-03-24 13:20:16 -04:00
|
|
|
Sidekiq::Client.instance_variable_set(:@default, nil)
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|
|
|
|
|
2013-05-12 17:25:30 -04:00
|
|
|
after do
|
2014-05-17 18:55:21 -04:00
|
|
|
Sidekiq.redis = REDIS
|
2014-03-24 13:20:16 -04:00
|
|
|
Sidekiq::Client.instance_variable_set(:@default, nil)
|
2013-05-12 17:25:30 -04:00
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
it 'raises ArgumentError with invalid params' do
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Sidekiq::Client.push('foo', 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Sidekiq::Client.push('foo', :class => 'Foo', :noargs => [1, 2])
|
|
|
|
end
|
2012-11-15 15:58:58 -05:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'noargs' => [1, 2])
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Sidekiq::Client.push('queue' => 'foo', 'class' => 42, 'args' => [1, 2])
|
|
|
|
end
|
|
|
|
|
2013-01-05 04:32:05 -05:00
|
|
|
assert_raises ArgumentError do
|
|
|
|
Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'args' => 1)
|
|
|
|
end
|
2013-10-26 00:07:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'as instance' do
|
|
|
|
it 'can push' do
|
|
|
|
@redis.expect :lpush, 1, ['queue:default', Array]
|
|
|
|
client = Sidekiq::Client.new
|
|
|
|
jid = client.push('class' => 'Blah', 'args' => [1,2,3])
|
|
|
|
assert_equal 24, jid.size
|
|
|
|
end
|
2013-01-05 04:32:05 -05:00
|
|
|
|
2013-10-26 00:07:54 -04:00
|
|
|
it 'allows local middleware modification' do
|
|
|
|
@redis.expect :lpush, 1, ['queue:default', Array]
|
|
|
|
$called = false
|
2014-03-26 00:38:17 -04:00
|
|
|
mware = Class.new { def call(worker_klass,msg,q,r); $called = true; msg;end }
|
2013-10-26 00:07:54 -04:00
|
|
|
client = Sidekiq::Client.new
|
|
|
|
client.middleware do |chain|
|
|
|
|
chain.add mware
|
|
|
|
end
|
|
|
|
client.push('class' => 'Blah', 'args' => [1,2,3])
|
|
|
|
|
|
|
|
assert $called
|
|
|
|
assert client.middleware.exists?(mware)
|
|
|
|
refute Sidekiq.client_middleware.exists?(mware)
|
|
|
|
end
|
2012-01-23 15:56:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'pushes messages to redis' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :lpush, 1, ['queue:foo', Array]
|
2012-04-01 22:53:45 -04:00
|
|
|
pushed = Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'args' => [1, 2])
|
2012-02-10 23:20:01 -05:00
|
|
|
assert pushed
|
2012-08-11 14:47:25 -04:00
|
|
|
assert_equal 24, pushed.size
|
2012-01-23 15:56:49 -05:00
|
|
|
@redis.verify
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|
|
|
|
|
2012-11-15 15:58:58 -05:00
|
|
|
it 'pushes messages to redis using a String class' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :lpush, 1, ['queue:foo', Array]
|
2012-11-15 15:58:58 -05:00
|
|
|
pushed = Sidekiq::Client.push('queue' => 'foo', 'class' => 'MyWorker', 'args' => [1, 2])
|
|
|
|
assert pushed
|
|
|
|
assert_equal 24, pushed.size
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
class MyWorker
|
2012-01-25 16:53:00 -05:00
|
|
|
include Sidekiq::Worker
|
2012-01-23 15:56:49 -05:00
|
|
|
end
|
|
|
|
|
2012-06-16 23:56:55 -04:00
|
|
|
it 'has default options' do
|
2013-09-07 12:54:13 -04:00
|
|
|
assert_equal Sidekiq.default_worker_options, MyWorker.get_sidekiq_options
|
2012-06-16 23:56:55 -04:00
|
|
|
end
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
it 'handles perform_async' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :lpush, 1, ['queue:default', Array]
|
2012-02-10 23:20:01 -05:00
|
|
|
pushed = MyWorker.perform_async(1, 2)
|
|
|
|
assert pushed
|
2012-01-25 16:53:00 -05:00
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
it 'enqueues messages to redis' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :lpush, 1, ['queue:default', Array]
|
2012-02-10 23:20:01 -05:00
|
|
|
pushed = Sidekiq::Client.enqueue(MyWorker, 1, 2)
|
|
|
|
assert pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-08-21 05:02:07 -04:00
|
|
|
it 'enqueues messages to redis' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :lpush, 1, ['queue:custom_queue', Array]
|
2012-08-21 05:02:07 -04:00
|
|
|
pushed = Sidekiq::Client.enqueue_to(:custom_queue, MyWorker, 1, 2)
|
|
|
|
assert pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2013-09-29 03:58:16 -04:00
|
|
|
it 'enqueues messages to redis (delayed, custom queue)' do
|
|
|
|
@redis.expect :zadd, 1, ['schedule', Array]
|
|
|
|
pushed = Sidekiq::Client.enqueue_to_in(:custom_queue, 3.minutes, MyWorker, 1, 2)
|
|
|
|
assert pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues messages to redis (delayed into past, custom queue)' do
|
|
|
|
@redis.expect :lpush, 1, ['queue:custom_queue', Array]
|
|
|
|
pushed = Sidekiq::Client.enqueue_to_in(:custom_queue, -3.minutes, MyWorker, 1, 2)
|
|
|
|
assert pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues messages to redis (delayed)' do
|
|
|
|
@redis.expect :zadd, 1, ['schedule', Array]
|
|
|
|
pushed = Sidekiq::Client.enqueue_in(3.minutes, MyWorker, 1, 2)
|
|
|
|
assert pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-02-10 23:20:01 -05:00
|
|
|
class QueuedWorker
|
|
|
|
include Sidekiq::Worker
|
2013-04-18 12:11:49 -04:00
|
|
|
sidekiq_options :queue => :flimflam
|
2012-02-10 23:20:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues to the named queue' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :lpush, 1, ['queue:flimflam', Array]
|
2012-02-10 23:20:01 -05:00
|
|
|
pushed = QueuedWorker.perform_async(1, 2)
|
|
|
|
assert pushed
|
2012-01-23 15:56:49 -05:00
|
|
|
@redis.verify
|
|
|
|
end
|
2012-02-15 14:28:19 -05:00
|
|
|
|
|
|
|
it 'retrieves queues' do
|
|
|
|
@redis.expect :smembers, ['bob'], ['queues']
|
2013-10-26 00:07:54 -04:00
|
|
|
assert_equal ['bob'], Sidekiq::Queue.all.map(&:name)
|
2012-02-15 14:28:19 -05:00
|
|
|
end
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|
2012-06-16 23:56:55 -04:00
|
|
|
|
2012-09-13 20:38:56 -04:00
|
|
|
describe 'bulk' do
|
2013-01-06 00:17:08 -05:00
|
|
|
after do
|
|
|
|
Sidekiq::Queue.new.clear
|
|
|
|
end
|
2012-09-11 23:53:22 -04:00
|
|
|
it 'can push a large set of jobs at once' do
|
2013-11-05 12:17:30 -05:00
|
|
|
jids = Sidekiq::Client.push_bulk('class' => QueuedWorker, 'args' => (1..1_000).to_a.map { |x| Array(x) })
|
|
|
|
assert_equal 1_000, jids.size
|
2012-09-11 23:53:22 -04:00
|
|
|
end
|
2012-11-15 15:58:58 -05:00
|
|
|
it 'can push a large set of jobs at once using a String class' do
|
2013-11-05 12:17:30 -05:00
|
|
|
jids = Sidekiq::Client.push_bulk('class' => 'QueuedWorker', 'args' => (1..1_000).to_a.map { |x| Array(x) })
|
|
|
|
assert_equal 1_000, jids.size
|
|
|
|
end
|
|
|
|
it 'returns the jids for the jobs' do
|
|
|
|
Sidekiq::Client.push_bulk('class' => 'QueuedWorker', 'args' => (1..2).to_a.map { |x| Array(x) }).each do |jid|
|
2014-03-19 20:41:11 -04:00
|
|
|
assert_match(/[0-9a-f]{12}/, jid)
|
2013-11-05 12:17:30 -05:00
|
|
|
end
|
2012-11-15 15:58:58 -05:00
|
|
|
end
|
2012-09-11 23:53:22 -04:00
|
|
|
end
|
|
|
|
|
2012-06-16 23:56:55 -04:00
|
|
|
class BaseWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
sidekiq_options 'retry' => 'base'
|
|
|
|
end
|
|
|
|
class AWorker < BaseWorker
|
|
|
|
end
|
|
|
|
class BWorker < BaseWorker
|
|
|
|
sidekiq_options 'retry' => 'b'
|
|
|
|
end
|
2012-11-08 15:28:50 -05:00
|
|
|
class CWorker < BaseWorker
|
|
|
|
sidekiq_options 'retry' => 2
|
|
|
|
end
|
2012-06-16 23:56:55 -04:00
|
|
|
|
2012-09-13 00:22:13 -04:00
|
|
|
describe 'client middleware' do
|
|
|
|
|
|
|
|
class Stopper
|
2014-03-26 00:38:17 -04:00
|
|
|
def call(worker_class, message, queue, r)
|
|
|
|
raise ArgumentError unless r
|
2012-09-13 00:22:13 -04:00
|
|
|
yield if message['args'].first.odd?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can stop some of the jobs from pushing' do
|
|
|
|
Sidekiq.client_middleware.add Stopper
|
|
|
|
begin
|
|
|
|
assert_equal nil, Sidekiq::Client.push('class' => MyWorker, 'args' => [0])
|
2014-03-19 20:41:11 -04:00
|
|
|
assert_match(/[0-9a-f]{12}/, Sidekiq::Client.push('class' => MyWorker, 'args' => [1]))
|
2013-11-05 12:17:30 -05:00
|
|
|
Sidekiq::Client.push_bulk('class' => MyWorker, 'args' => [[0], [1]]).each do |jid|
|
2014-03-19 20:41:11 -04:00
|
|
|
assert_match(/[0-9a-f]{12}/, jid)
|
2013-11-05 12:17:30 -05:00
|
|
|
end
|
2012-09-13 00:22:13 -04:00
|
|
|
ensure
|
|
|
|
Sidekiq.client_middleware.remove Stopper
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-16 23:56:55 -04:00
|
|
|
describe 'inheritance' do
|
2012-11-08 14:34:56 -05:00
|
|
|
it 'inherits sidekiq options' do
|
2012-06-16 23:56:55 -04:00
|
|
|
assert_equal 'base', AWorker.get_sidekiq_options['retry']
|
|
|
|
assert_equal 'b', BWorker.get_sidekiq_options['retry']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-17 23:07:30 -04:00
|
|
|
describe 'item normalization' do
|
|
|
|
it 'defaults retry to true' do
|
2014-02-14 09:43:34 -05:00
|
|
|
assert_equal true, Sidekiq::Client.new.__send__(:normalize_item, 'class' => QueuedWorker, 'args' => [])['retry']
|
2012-09-17 23:07:30 -04:00
|
|
|
end
|
2012-11-08 15:28:50 -05:00
|
|
|
|
2012-11-08 14:34:56 -05:00
|
|
|
it "does not normalize numeric retry's" do
|
2014-02-14 09:43:34 -05:00
|
|
|
assert_equal 2, Sidekiq::Client.new.__send__(:normalize_item, 'class' => CWorker, 'args' => [])['retry']
|
2012-11-08 15:28:50 -05:00
|
|
|
end
|
2012-09-17 23:07:30 -04:00
|
|
|
end
|
2014-03-25 14:56:15 -04:00
|
|
|
|
|
|
|
describe 'sharding' do
|
|
|
|
class DWorker < BaseWorker
|
|
|
|
end
|
|
|
|
it 'allows sidekiq_options to point to different Redi' do
|
|
|
|
conn = MiniTest::Mock.new
|
|
|
|
conn.expect(:multi, [0, 1])
|
|
|
|
DWorker.sidekiq_options('pool' => ConnectionPool.new(size: 1) { conn })
|
|
|
|
DWorker.perform_async(1,2,3)
|
|
|
|
conn.verify
|
|
|
|
end
|
2014-03-26 23:35:57 -04:00
|
|
|
it 'allows #via to point to different Redi' do
|
|
|
|
conn = MiniTest::Mock.new
|
|
|
|
conn.expect(:multi, [0, 1])
|
2014-03-26 23:58:45 -04:00
|
|
|
default = Sidekiq::Client.new.redis_pool
|
|
|
|
sharded_pool = ConnectionPool.new(size: 1) { conn }
|
|
|
|
Sidekiq::Client.via(sharded_pool) do
|
2014-03-26 23:35:57 -04:00
|
|
|
CWorker.perform_async(1,2,3)
|
2014-03-26 23:58:45 -04:00
|
|
|
assert_equal sharded_pool, Sidekiq::Client.new.redis_pool
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
Sidekiq::Client.via(default) do
|
|
|
|
# nothing
|
|
|
|
end
|
|
|
|
end
|
2014-03-26 23:35:57 -04:00
|
|
|
end
|
2014-03-26 23:58:45 -04:00
|
|
|
assert_equal default, Sidekiq::Client.new.redis_pool
|
2014-03-26 23:35:57 -04:00
|
|
|
conn.verify
|
|
|
|
end
|
2014-03-25 14:56:15 -04:00
|
|
|
it 'allows Resque helpers to point to different Redi' do
|
|
|
|
conn = MiniTest::Mock.new
|
2014-08-31 17:15:06 -04:00
|
|
|
conn.expect(:multi, []) { |*args, &block| block.call }
|
2014-03-25 14:56:15 -04:00
|
|
|
conn.expect(:zadd, 1, [String, Array])
|
|
|
|
DWorker.sidekiq_options('pool' => ConnectionPool.new(size: 1) { conn })
|
|
|
|
Sidekiq::Client.enqueue_in(10, DWorker, 3)
|
|
|
|
conn.verify
|
|
|
|
end
|
|
|
|
end
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|