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_actors.rb

130 lines
2.5 KiB
Ruby
Raw Normal View History

2015-10-02 18:44:29 -04:00
require_relative 'helper'
2015-10-05 13:13:00 -04:00
require 'sidekiq/cli'
2015-10-02 18:44:29 -04:00
require 'sidekiq/fetch'
2015-10-08 12:37:37 -04:00
require 'sidekiq/scheduled'
2015-10-05 13:13:00 -04:00
require 'sidekiq/processor'
2015-10-02 18:44:29 -04:00
class TestActors < Sidekiq::Test
class SomeWorker
include Sidekiq::Worker
2015-10-05 13:13:00 -04:00
def perform(slp)
raise "boom" if slp == "boom"
sleep(slp) if slp > 0
$count += 1
end
2015-10-02 18:44:29 -04:00
end
describe 'scheduler' do
it 'can start and stop' do
f = Sidekiq::Scheduled::Poller.new
f.start
f.terminate
end
it 'can schedule' do
2015-10-05 13:13:00 -04:00
Sidekiq.redis {|c| c.flushdb}
2015-10-05 13:13:00 -04:00
ss = Sidekiq::ScheduledSet.new
q = Sidekiq::Queue.new
2015-10-05 13:13:00 -04:00
SomeWorker.perform_in(0.01, 0)
assert_equal 0, q.size
assert_equal 1, ss.size
2015-10-08 12:37:37 -04:00
sleep 0.015
s = Sidekiq::Scheduled::Poller.new
s.enqueue
assert_equal 1, q.size
assert_equal 0, ss.size
s.terminate
end
end
2015-10-05 13:13:00 -04:00
describe 'processor' do
before do
$count = 0
end
it 'can start and stop' do
2015-10-08 12:37:37 -04:00
f = Sidekiq::Processor.new(Mgr.new)
2015-10-05 13:13:00 -04:00
f.terminate
end
class Mgr
2015-10-07 12:08:47 -04:00
attr_reader :latest_error
2015-10-05 13:13:00 -04:00
attr_reader :mutex
attr_reader :cond
def initialize
@mutex = ::Mutex.new
@cond = ::ConditionVariable.new
end
def processor_died(inst, err)
2015-10-07 12:08:47 -04:00
@latest_error = err
2015-10-05 13:13:00 -04:00
@mutex.synchronize do
@cond.signal
end
end
2015-10-09 00:50:45 -04:00
def processor_stopped(inst)
@mutex.synchronize do
@cond.signal
end
end
2015-10-08 12:37:37 -04:00
def options
{ :concurrency => 3, :queues => ['default'] }
end
2015-10-05 13:13:00 -04:00
end
it 'can process' do
mgr = Mgr.new
p = Sidekiq::Processor.new(mgr)
SomeWorker.perform_async(0)
a = $count
2015-10-08 12:37:37 -04:00
p.process_one
2015-10-05 13:13:00 -04:00
b = $count
assert_equal a + 1, b
end
it 'deals with errors' do
mgr = Mgr.new
p = Sidekiq::Processor.new(mgr)
SomeWorker.perform_async("boom")
a = $count
mgr.mutex.synchronize do
2015-10-08 12:37:37 -04:00
p.start
2015-10-05 13:13:00 -04:00
mgr.cond.wait(mgr.mutex)
end
b = $count
assert_equal a, b
2015-10-07 12:05:23 -04:00
sleep 0.001
2015-10-05 13:13:00 -04:00
assert_equal false, p.thread.status
p.terminate(true)
2015-10-07 12:08:47 -04:00
refute_nil mgr.latest_error
assert_equal RuntimeError, mgr.latest_error.class
2015-10-05 13:13:00 -04:00
end
it 'gracefully kills' do
mgr = Mgr.new
p = Sidekiq::Processor.new(mgr)
2015-10-08 12:37:37 -04:00
SomeWorker.perform_async(1)
2015-10-05 13:13:00 -04:00
a = $count
2015-10-06 15:43:01 -04:00
p.start
2015-10-05 13:13:00 -04:00
sleep(0.02)
p.terminate
p.kill(true)
b = $count
assert_equal a, b
assert_equal false, p.thread.status
2015-10-09 00:50:45 -04:00
refute mgr.latest_error, mgr.latest_error.to_s
2015-10-05 13:13:00 -04:00
end
end
2015-10-02 18:44:29 -04:00
end