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
|
|
|
|
Sidekiq::Client.redis = Redis.connect(:url => 'redis://localhost/sidekiq_test')
|
|
|
|
Sidekiq::Client.redis.flushdb
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not push duplicate messages when configured for unique only' do
|
2012-02-07 20:18:28 -05:00
|
|
|
Sidekiq::Client.ignore_duplicate_jobs = true
|
2012-02-07 06:29:09 -05:00
|
|
|
10.times { Sidekiq::Client.push('customqueue', 'class' => 'Foo', 'args' => [1, 2]) }
|
|
|
|
assert_equal Sidekiq::Client.redis.llen("queue:customqueue"), 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does push duplicate messages when not configured for unique only' do
|
2012-02-07 20:18:28 -05:00
|
|
|
Sidekiq::Client.ignore_duplicate_jobs = false
|
2012-02-07 06:29:09 -05:00
|
|
|
10.times { Sidekiq::Client.push('customqueue2', 'class' => 'Foo', 'args' => [1, 2]) }
|
|
|
|
assert_equal Sidekiq::Client.redis.llen("queue:customqueue2"), 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
describe 'with mock redis' do
|
|
|
|
before do
|
|
|
|
@redis = MiniTest::Mock.new
|
2012-02-07 06:29:09 -05:00
|
|
|
def @redis.multi; yield; end
|
|
|
|
def @redis.sadd(*); true; end
|
2012-01-23 15:56:49 -05:00
|
|
|
Sidekiq::Client.redis = @redis
|
2012-02-07 20:18:28 -05:00
|
|
|
Sidekiq::Client.ignore_duplicate_jobs = false
|
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]
|
|
|
|
count = Sidekiq::Client.push('foo', 'class' => 'Foo', 'args' => [1, 2])
|
|
|
|
assert count > 0
|
|
|
|
@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
|
|
|
def self.queue
|
|
|
|
'foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
it 'handles perform_async' do
|
|
|
|
@redis.expect :rpush, 1, ['queue:default', String]
|
|
|
|
count = MyWorker.perform_async(1, 2)
|
|
|
|
assert count > 0
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
it 'enqueues messages to redis' do
|
|
|
|
@redis.expect :rpush, 1, ['queue:foo', String]
|
|
|
|
count = Sidekiq::Client.enqueue(MyWorker, 1, 2)
|
|
|
|
assert count > 0
|
|
|
|
@redis.verify
|
|
|
|
end
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|
|
|
|
end
|