2012-10-20 17:03:43 -04:00
|
|
|
require 'helper'
|
|
|
|
|
|
|
|
class TestApi < MiniTest::Unit::TestCase
|
2012-12-04 08:11:25 -05:00
|
|
|
describe "stats" do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "processed" do
|
|
|
|
it "is initially zero" do
|
2012-12-04 13:14:38 -05:00
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal 0, s.processed
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns number of processed jobs" do
|
|
|
|
Sidekiq.redis { |conn| conn.set("stat:processed", 5) }
|
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal 5, s.processed
|
2012-12-04 08:11:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "failed" do
|
|
|
|
it "is initially zero" do
|
2012-12-04 13:14:38 -05:00
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal 0, s.processed
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns number of failed jobs" do
|
|
|
|
Sidekiq.redis { |conn| conn.set("stat:failed", 5) }
|
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal 5, s.failed
|
2012-12-04 08:11:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "queues" do
|
|
|
|
it "is initially empty" do
|
|
|
|
s = Sidekiq::Stats.new
|
2012-12-04 13:14:38 -05:00
|
|
|
assert_equal 0, s.queues.size
|
2012-12-04 08:11:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a hash of queue and size" do
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.rpush 'queue:foo', '{}'
|
|
|
|
conn.sadd 'queues', 'foo'
|
|
|
|
|
|
|
|
3.times { conn.rpush 'queue:bar', '{}' }
|
|
|
|
conn.sadd 'queues', 'bar'
|
|
|
|
end
|
|
|
|
|
2012-12-04 13:14:38 -05:00
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal ({ "foo" => 1, "bar" => 3 }), s.queues
|
2012-12-04 08:11:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 12:18:05 -05:00
|
|
|
describe "enqueued" do
|
2012-12-04 08:11:25 -05:00
|
|
|
it "is initially empty" do
|
2012-12-04 13:14:38 -05:00
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal 0, s.enqueued
|
2012-12-04 08:11:25 -05:00
|
|
|
end
|
|
|
|
|
2012-12-04 12:18:05 -05:00
|
|
|
it "returns total enqueued jobs" do
|
2012-12-04 08:11:25 -05:00
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.rpush 'queue:foo', '{}'
|
|
|
|
conn.sadd 'queues', 'foo'
|
|
|
|
|
|
|
|
3.times { conn.rpush 'queue:bar', '{}' }
|
|
|
|
conn.sadd 'queues', 'bar'
|
|
|
|
end
|
|
|
|
|
2012-12-04 13:14:38 -05:00
|
|
|
s = Sidekiq::Stats.new
|
|
|
|
assert_equal 4, s.enqueued
|
2012-12-04 08:11:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-20 17:03:43 -04:00
|
|
|
describe 'with an empty database' do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'shows queue as empty' do
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
assert_equal 0, q.size
|
|
|
|
end
|
|
|
|
|
|
|
|
class ApiWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can enumerate jobs' do
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
ApiWorker.perform_async(1, 'mike')
|
|
|
|
assert_equal ['TestApi::ApiWorker'], q.map(&:klass)
|
|
|
|
|
|
|
|
job = q.first
|
|
|
|
assert_equal 24, job.jid.size
|
|
|
|
assert_equal [1, 'mike'], job.args
|
|
|
|
|
|
|
|
q = Sidekiq::Queue.new('other')
|
|
|
|
assert_equal 0, q.size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can delete jobs' do
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
ApiWorker.perform_async(1, 'mike')
|
|
|
|
assert_equal 1, q.size
|
|
|
|
assert_equal [true], q.map(&:delete)
|
|
|
|
assert_equal 0, q.size
|
|
|
|
end
|
|
|
|
|
2012-11-25 21:43:48 -05:00
|
|
|
it 'can clear a queue' do
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
2.times { ApiWorker.perform_async(1, 'mike') }
|
|
|
|
q.clear
|
|
|
|
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
refute conn.smembers('queues').include?('foo')
|
|
|
|
refute conn.exists('queues:foo')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-02 23:32:16 -05:00
|
|
|
it 'can fetch by score' do
|
|
|
|
same_time = Time.now.to_f
|
|
|
|
add_retry('bob1', same_time)
|
|
|
|
add_retry('bob2', same_time)
|
|
|
|
r = Sidekiq::RetrySet.new
|
|
|
|
assert_equal 2, r.fetch(same_time).size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can fetch by score and jid' do
|
|
|
|
same_time = Time.now.to_f
|
|
|
|
add_retry('bob1', same_time)
|
|
|
|
add_retry('bob2', same_time)
|
|
|
|
r = Sidekiq::RetrySet.new
|
|
|
|
# jobs = r.fetch(same_time)
|
|
|
|
# puts jobs[1].jid
|
|
|
|
assert_equal 1, r.fetch(same_time, 'bob1').size
|
|
|
|
end
|
|
|
|
|
2012-10-20 17:03:43 -04:00
|
|
|
it 'shows empty retries' do
|
2012-10-30 13:06:20 -04:00
|
|
|
r = Sidekiq::RetrySet.new
|
2012-10-20 17:03:43 -04:00
|
|
|
assert_equal 0, r.size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can enumerate retries' do
|
|
|
|
add_retry
|
|
|
|
|
2012-10-30 13:06:20 -04:00
|
|
|
r = Sidekiq::RetrySet.new
|
2012-10-20 17:03:43 -04:00
|
|
|
assert_equal 1, r.size
|
|
|
|
array = r.to_a
|
|
|
|
assert_equal 1, array.size
|
|
|
|
|
|
|
|
retri = array.first
|
|
|
|
assert_equal 'ApiWorker', retri.klass
|
|
|
|
assert_equal 'default', retri.queue
|
|
|
|
assert_equal 'bob', retri.jid
|
2012-10-30 13:59:02 -04:00
|
|
|
assert_in_delta Time.now.to_f, retri.at.to_f, 0.01
|
2012-10-20 17:03:43 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 14:53:22 -05:00
|
|
|
it 'can delete multiple retries from score' do
|
|
|
|
same_time = Time.now.to_f
|
|
|
|
add_retry('bob1', same_time)
|
|
|
|
add_retry('bob2', same_time)
|
2012-10-30 13:06:20 -04:00
|
|
|
r = Sidekiq::RetrySet.new
|
2012-11-26 14:53:22 -05:00
|
|
|
assert_equal 2, r.size
|
|
|
|
Sidekiq::RetrySet.new.delete(same_time)
|
2012-10-20 17:03:43 -04:00
|
|
|
assert_equal 0, r.size
|
|
|
|
end
|
|
|
|
|
2012-11-26 14:53:22 -05:00
|
|
|
it 'can delete a single retry from score and jid' do
|
2012-11-26 12:56:08 -05:00
|
|
|
same_time = Time.now.to_f
|
|
|
|
add_retry('bob1', same_time)
|
|
|
|
add_retry('bob2', same_time)
|
|
|
|
r = Sidekiq::RetrySet.new
|
|
|
|
assert_equal 2, r.size
|
2012-11-26 14:53:22 -05:00
|
|
|
Sidekiq::RetrySet.new.delete(same_time, 'bob1')
|
2012-11-26 12:56:08 -05:00
|
|
|
assert_equal 1, r.size
|
|
|
|
end
|
|
|
|
|
2012-11-26 11:22:48 -05:00
|
|
|
it 'can retry a retry' do
|
|
|
|
add_retry
|
|
|
|
r = Sidekiq::RetrySet.new
|
|
|
|
assert_equal 1, r.size
|
|
|
|
r.first.retry
|
|
|
|
assert_equal 0, r.size
|
|
|
|
assert_equal 1, Sidekiq::Queue.new('default').size
|
|
|
|
job = Sidekiq::Queue.new('default').first
|
|
|
|
assert_equal 'bob', job.jid
|
|
|
|
assert_equal 1, job['retry_count']
|
|
|
|
end
|
|
|
|
|
2012-11-25 21:43:48 -05:00
|
|
|
it 'can clear retries' do
|
|
|
|
add_retry
|
|
|
|
add_retry('test')
|
|
|
|
r = Sidekiq::RetrySet.new
|
|
|
|
assert_equal 2, r.size
|
|
|
|
r.clear
|
|
|
|
assert_equal 0, r.size
|
|
|
|
end
|
|
|
|
|
2012-11-26 12:56:08 -05:00
|
|
|
def add_retry(jid = 'bob', at = Time.now.to_f)
|
|
|
|
payload = Sidekiq.dump_json('class' => 'ApiWorker', 'args' => [1, 'mike'], 'queue' => 'default', 'jid' => jid, 'retry_count' => 2, 'failed_at' => Time.now.utc)
|
2012-10-20 17:03:43 -04:00
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.zadd('retry', at.to_s, payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|