2012-02-11 02:16:12 -05:00
|
|
|
require 'helper'
|
|
|
|
require 'sidekiq'
|
|
|
|
require 'sidekiq/processor'
|
|
|
|
|
|
|
|
class TestStats < MiniTest::Unit::TestCase
|
|
|
|
describe 'with redis' do
|
|
|
|
before do
|
2012-03-14 00:19:46 -04:00
|
|
|
@redis = Sidekiq.redis = REDIS
|
2012-03-28 22:16:54 -04:00
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class DumbWorker
|
|
|
|
include Sidekiq::Worker
|
2012-09-03 15:12:51 -04:00
|
|
|
sidekiq_options :queue => 'dumbq'
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-03-14 00:19:46 -04:00
|
|
|
def perform(arg)
|
|
|
|
raise 'bang' if arg == nil
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates global stats in the success case' do
|
2012-06-21 23:51:47 -04:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => DumbWorker.to_s, 'args' => [""] })
|
2012-02-11 02:16:12 -05:00
|
|
|
boss = MiniTest::Mock.new
|
2012-02-11 02:32:42 -05:00
|
|
|
|
2012-03-14 00:19:46 -04:00
|
|
|
@redis.with do |conn|
|
2012-02-11 02:32:42 -05:00
|
|
|
|
2012-03-14 00:19:46 -04:00
|
|
|
set = conn.smembers('workers')
|
|
|
|
assert_equal 0, set.size
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-03-14 00:19:46 -04:00
|
|
|
processor = Sidekiq::Processor.new(boss)
|
|
|
|
boss.expect(:processor_done!, nil, [processor])
|
2012-05-12 01:09:42 -04:00
|
|
|
boss.expect(:processor_done!, nil, [processor])
|
|
|
|
boss.expect(:processor_done!, nil, [processor])
|
2012-02-11 02:32:42 -05:00
|
|
|
|
2012-09-04 01:38:51 -04:00
|
|
|
assert_equal 0, Sidekiq.info[:failed]
|
|
|
|
assert_equal 0, Sidekiq.info[:processed]
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-03-14 00:19:46 -04:00
|
|
|
processor.process(msg, 'xyzzy')
|
|
|
|
processor.process(msg, 'xyzzy')
|
|
|
|
processor.process(msg, 'xyzzy')
|
|
|
|
|
2012-09-04 01:38:51 -04:00
|
|
|
assert_equal 0, Sidekiq.info[:failed]
|
|
|
|
assert_equal 3, Sidekiq.info[:processed]
|
2012-03-14 00:19:46 -04:00
|
|
|
end
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates global stats in the error case' do
|
2012-06-21 23:51:47 -04:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => DumbWorker.to_s, 'args' => [nil] })
|
2012-02-11 02:16:12 -05:00
|
|
|
boss = MiniTest::Mock.new
|
|
|
|
|
2012-04-03 23:00:20 -04:00
|
|
|
@redis.with do |conn|
|
|
|
|
assert_equal [], conn.smembers('workers')
|
2012-09-04 01:38:51 -04:00
|
|
|
assert_equal 0, Sidekiq.info[:failed]
|
|
|
|
assert_equal 0, Sidekiq.info[:processed]
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-04-03 23:00:20 -04:00
|
|
|
processor = Sidekiq::Processor.new(boss)
|
2012-02-11 02:32:42 -05:00
|
|
|
|
2012-04-03 23:00:20 -04:00
|
|
|
pstr = processor.to_s
|
|
|
|
assert_raises RuntimeError do
|
|
|
|
processor.process(msg, 'xyzzy')
|
|
|
|
end
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-09-04 01:38:51 -04:00
|
|
|
assert_equal 1, Sidekiq.info[:failed]
|
|
|
|
assert_equal 1, Sidekiq.info[:processed]
|
2012-04-03 23:00:20 -04:00
|
|
|
end
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
|
|
|
|
2012-09-04 01:55:25 -04:00
|
|
|
describe "info counts" do
|
2012-09-03 15:19:01 -04:00
|
|
|
before do
|
2012-09-03 15:12:51 -04:00
|
|
|
@redis.with do |conn|
|
|
|
|
conn.rpush 'queue:foo', '{}'
|
|
|
|
conn.sadd 'queues', 'foo'
|
|
|
|
|
|
|
|
conn.rpush 'queue:bar', '{}'
|
|
|
|
conn.rpush 'queue:bar', '{}'
|
|
|
|
conn.sadd 'queues', 'bar'
|
2012-09-04 01:55:25 -04:00
|
|
|
|
|
|
|
conn.rpush 'queue:baz', '{}'
|
|
|
|
conn.sadd 'queues', 'baz'
|
2012-09-03 15:12:51 -04:00
|
|
|
end
|
2012-09-03 15:19:01 -04:00
|
|
|
end
|
2012-09-03 15:36:38 -04:00
|
|
|
|
2012-09-04 01:38:51 -04:00
|
|
|
describe "queues_with_sizes" do
|
2012-09-03 15:19:01 -04:00
|
|
|
it "returns queue names and corresponding job counts" do
|
2012-09-04 01:55:25 -04:00
|
|
|
assert_equal [["foo", 1], ["baz", 1], ["bar", 2]], Sidekiq.info[:queues_with_sizes]
|
2012-09-03 15:19:01 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-03 15:36:38 -04:00
|
|
|
|
2012-09-03 15:19:01 -04:00
|
|
|
describe "backlog" do
|
|
|
|
it "returns count of all jobs yet to be processed" do
|
2012-09-04 01:55:25 -04:00
|
|
|
assert_equal 4, Sidekiq.info[:backlog]
|
2012-09-03 15:19:01 -04:00
|
|
|
end
|
2012-09-03 15:12:51 -04:00
|
|
|
end
|
2012-09-03 15:36:38 -04:00
|
|
|
|
|
|
|
describe "size" do
|
|
|
|
it "returns size of queues" do
|
2012-09-04 01:55:25 -04:00
|
|
|
assert_equal 0, Sidekiq.size("foox")
|
2012-09-04 01:38:51 -04:00
|
|
|
assert_equal 1, Sidekiq.size(:foo)
|
|
|
|
assert_equal 1, Sidekiq.size("foo")
|
|
|
|
assert_equal 3, Sidekiq.size("foo", "bar")
|
2012-09-04 01:55:25 -04:00
|
|
|
assert_equal 4, Sidekiq.size
|
2012-09-03 15:36:38 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-03 15:12:51 -04:00
|
|
|
end
|
|
|
|
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
|
|
|
end
|