1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/test/test_client.rb

267 lines
7.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require_relative 'helper'
2012-01-21 19:42:21 -05:00
class TestClient < Sidekiq::Test
describe 'errors' do
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
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
end
2013-10-26 00:07:54 -04:00
describe 'as instance' do
it 'can push' do
client = Sidekiq::Client.new
jid = client.push('class' => 'Blah', 'args' => [1,2,3])
assert_equal 24, jid.size
end
2013-10-26 00:07:54 -04:00
it 'allows middleware to stop bulk jobs' do
mware = Class.new do
def call(worker_klass,msg,q,r)
msg['args'][0] == 1 ? yield : false
end
end
client = Sidekiq::Client.new
client.middleware do |chain|
chain.add mware
end
q = Sidekiq::Queue.new
q.clear
result = client.push_bulk('class' => 'Blah', 'args' => [[1],[2],[3]])
assert_equal 1, result.size
assert_equal 1, q.size
end
it 'allows local middleware modification' do
$called = false
mware = Class.new { def call(worker_klass,msg,q,r); $called = true; msg;end }
client = Sidekiq::Client.new
client.middleware do |chain|
chain.add mware
2013-10-26 00:07:54 -04:00
end
client.push('class' => 'Blah', 'args' => [1,2,3])
assert $called
assert client.middleware.exists?(mware)
refute Sidekiq.client_middleware.exists?(mware)
2012-01-23 15:56:49 -05:00
end
end
2012-01-23 15:56:49 -05:00
describe 'client' do
2012-01-23 15:56:49 -05:00
it 'pushes messages to redis' do
q = Sidekiq::Queue.new('foo')
pre = q.size
jid = Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'args' => [1, 2])
assert jid
assert_equal 24, jid.size
assert_equal pre + 1, q.size
2012-01-21 19:42:21 -05:00
end
it 'pushes messages to redis using a String class' do
q = Sidekiq::Queue.new('foo')
pre = q.size
jid = Sidekiq::Client.push('queue' => 'foo', 'class' => 'MyWorker', 'args' => [1, 2])
assert jid
assert_equal 24, jid.size
assert_equal pre + 1, q.size
end
2012-01-23 15:56:49 -05:00
class MyWorker
include Sidekiq::Worker
2012-01-23 15:56:49 -05:00
end
class QueuedWorker
include Sidekiq::Worker
sidekiq_options :queue => :flimflam
end
it 'enqueues' do
Sidekiq.redis {|c| c.flushdb }
assert_equal Sidekiq.default_worker_options, MyWorker.get_sidekiq_options
assert MyWorker.perform_async(1, 2)
assert Sidekiq::Client.enqueue(MyWorker, 1, 2)
assert Sidekiq::Client.enqueue_to(:custom_queue, MyWorker, 1, 2)
assert_equal 1, Sidekiq::Queue.new('custom_queue').size
assert Sidekiq::Client.enqueue_to_in(:custom_queue, 3.minutes, MyWorker, 1, 2)
assert Sidekiq::Client.enqueue_to_in(:custom_queue, -3.minutes, MyWorker, 1, 2)
assert_equal 2, Sidekiq::Queue.new('custom_queue').size
assert Sidekiq::Client.enqueue_in(3.minutes, MyWorker, 1, 2)
assert QueuedWorker.perform_async(1, 2)
assert_equal 1, Sidekiq::Queue.new('flimflam').size
end
2012-01-21 19:42:21 -05:00
end
2012-09-13 20:38:56 -04:00
describe 'bulk' do
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
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
end
it 'handles no jobs' do
result = Sidekiq::Client.push_bulk('class' => 'QueuedWorker', 'args' => [])
assert_equal 0, result.size
end
2012-09-11 23:53:22 -04:00
end
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
describe 'client middleware' do
class Stopper
def call(worker_class, job, queue, r)
raise ArgumentError unless r
yield if job['args'].first.odd?
end
end
it 'can stop some of the jobs from pushing' do
client = Sidekiq::Client.new
client.middleware do |chain|
chain.add Stopper
end
assert_nil client.push('class' => MyWorker, 'args' => [0])
assert_match(/[0-9a-f]{12}/, client.push('class' => MyWorker, 'args' => [1]))
client.push_bulk('class' => MyWorker, 'args' => [[0], [1]]).each do |jid|
assert_match(/[0-9a-f]{12}/, jid)
end
end
end
describe 'inheritance' do
2012-11-08 14:34:56 -05:00
it 'inherits sidekiq options' do
assert_equal 'base', AWorker.get_sidekiq_options['retry']
assert_equal 'b', BWorker.get_sidekiq_options['retry']
end
end
2014-03-25 14:56:15 -04:00
describe 'sharding' do
class DWorker < BaseWorker
end
2014-03-25 14:56:15 -04:00
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
it 'allows #via to point to same Redi' do
conn = MiniTest::Mock.new
conn.expect(:multi, [0, 1])
sharded_pool = ConnectionPool.new(size: 1) { conn }
Sidekiq::Client.via(sharded_pool) do
Sidekiq::Client.via(sharded_pool) do
CWorker.perform_async(1,2,3)
end
end
conn.verify
end
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
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
end
2014-03-26 23:58:45 -04:00
assert_equal default, Sidekiq::Client.new.redis_pool
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
describe 'Sidekiq::Worker#set' do
class SetWorker
include Sidekiq::Worker
2016-01-21 12:29:37 -05:00
sidekiq_options :queue => :foo, 'retry' => 12
end
def setup
Sidekiq.redis {|c| c.flushdb }
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
end
2012-01-21 19:42:21 -05:00
end