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
|
|
|
|
|
|
|
class TestClient < MiniTest::Unit::TestCase
|
2012-02-07 06:29:09 -05:00
|
|
|
describe 'with real redis' do
|
|
|
|
before do
|
2012-03-14 00:19:46 -04:00
|
|
|
Sidekiq.redis = REDIS
|
2012-03-28 22:16:54 -04:00
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
2012-02-07 06:29:09 -05:00
|
|
|
end
|
|
|
|
|
2012-04-01 22:53:45 -04:00
|
|
|
class QueueWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
sidekiq_options :queue => 'customqueue'
|
|
|
|
end
|
|
|
|
|
2012-02-07 06:29:09 -05:00
|
|
|
it 'does not push duplicate messages when configured for unique only' do
|
2012-04-11 22:41:08 -04:00
|
|
|
Sidekiq.client_middleware do |chain|
|
|
|
|
chain.add Sidekiq::Middleware::Client::UniqueJobs
|
|
|
|
end
|
2012-04-01 22:53:45 -04:00
|
|
|
QueueWorker.sidekiq_options :unique => true
|
|
|
|
10.times { Sidekiq::Client.push('class' => QueueWorker, 'args' => [1, 2]) }
|
2012-03-28 22:16:54 -04:00
|
|
|
assert_equal 1, Sidekiq.redis {|c| c.llen("queue:customqueue") }
|
2012-02-07 06:29:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does push duplicate messages when not configured for unique only' do
|
2012-04-11 22:41:08 -04:00
|
|
|
Sidekiq.client_middleware do |chain|
|
|
|
|
chain.add Sidekiq::Middleware::Client::UniqueJobs
|
|
|
|
end
|
2012-04-01 22:53:45 -04:00
|
|
|
QueueWorker.sidekiq_options :unique => false
|
|
|
|
10.times { Sidekiq::Client.push('class' => QueueWorker, 'args' => [1, 2]) }
|
|
|
|
assert_equal 10, Sidekiq.redis {|c| c.llen("queue:customqueue") }
|
2012-02-07 06:29:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
describe 'with mock redis' do
|
|
|
|
before do
|
|
|
|
@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)
|
2012-01-21 19:42:21 -05: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
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'pushes messages to redis' do
|
|
|
|
@redis.expect :rpush, 1, ['queue:foo', String]
|
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-01-23 15:56:49 -05:00
|
|
|
@redis.verify
|
2012-01-21 19:42:21 -05:00
|
|
|
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
|
|
|
|
assert_equal Sidekiq::Worker::ClassMethods::DEFAULT_OPTIONS, MyWorker.get_sidekiq_options
|
|
|
|
end
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
it 'handles perform_async' do
|
|
|
|
@redis.expect :rpush, 1, ['queue:default', String]
|
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-04-11 16:17:44 -04:00
|
|
|
it 'handles perform_async on failure' do
|
|
|
|
@redis.expect :rpush, nil, ['queue:default', String]
|
|
|
|
pushed = MyWorker.perform_async(1, 2)
|
|
|
|
refute pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
it 'enqueues messages to redis' do
|
2012-02-09 23:32:59 -05:00
|
|
|
@redis.expect :rpush, 1, ['queue:default', String]
|
2012-02-10 23:20:01 -05:00
|
|
|
pushed = Sidekiq::Client.enqueue(MyWorker, 1, 2)
|
|
|
|
assert pushed
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
class QueuedWorker
|
|
|
|
include Sidekiq::Worker
|
2012-04-26 11:40:07 -04:00
|
|
|
sidekiq_options :queue => :flimflam, :timeout => 1
|
2012-02-10 23:20:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues to the named queue' do
|
|
|
|
@redis.expect :rpush, 1, ['queue:flimflam', String]
|
|
|
|
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']
|
|
|
|
assert_equal ['bob'], Sidekiq::Client.registered_queues
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves workers' do
|
|
|
|
@redis.expect :smembers, ['bob'], ['workers']
|
|
|
|
assert_equal ['bob'], Sidekiq::Client.registered_workers
|
|
|
|
end
|
2012-01-21 19:42:21 -05: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
|
|
|
|
|
|
|
|
describe 'inheritance' do
|
|
|
|
it 'should inherit sidekiq options' do
|
|
|
|
assert_equal 'base', AWorker.get_sidekiq_options['retry']
|
|
|
|
assert_equal 'b', BWorker.get_sidekiq_options['retry']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|