2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2015-10-02 18:44:29 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
require_relative "helper"
|
|
|
|
require "sidekiq/cli"
|
|
|
|
require "sidekiq/fetch"
|
|
|
|
require "sidekiq/scheduled"
|
|
|
|
require "sidekiq/processor"
|
|
|
|
|
|
|
|
describe "Actors" do
|
2015-10-21 12:29:58 -04:00
|
|
|
class JoeWorker
|
2015-10-02 18:44:29 -04:00
|
|
|
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
|
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
before do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq.redis { |c| c.flushdb }
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "scheduler" do
|
|
|
|
it "can start and stop" do
|
2019-02-28 15:43:50 -05:00
|
|
|
f = Sidekiq::Scheduled::Poller.new
|
|
|
|
f.start
|
|
|
|
f.terminate
|
2015-10-21 01:04:36 -04:00
|
|
|
end
|
2015-10-03 00:07:09 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can schedule" do
|
2019-02-28 15:43:50 -05:00
|
|
|
ss = Sidekiq::ScheduledSet.new
|
|
|
|
q = Sidekiq::Queue.new
|
2015-10-03 00:07:09 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
JoeWorker.perform_in(0.01, 0)
|
2015-10-03 00:07:09 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
assert_equal 0, q.size
|
|
|
|
assert_equal 1, ss.size
|
2015-10-03 00:07:09 -04:00
|
|
|
|
2019-02-28 15:43:50 -05: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-03 00:07:09 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "processor" do
|
2019-02-28 15:43:50 -05:00
|
|
|
before do
|
|
|
|
$count = 0
|
2015-10-05 13:13:00 -04:00
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can start and stop" do
|
2020-06-19 11:39:18 -04:00
|
|
|
m = Mgr.new
|
|
|
|
f = Sidekiq::Processor.new(m, m.options)
|
2019-02-28 15:43:50 -05:00
|
|
|
f.terminate
|
|
|
|
end
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
class Mgr
|
|
|
|
attr_reader :latest_error
|
|
|
|
attr_reader :mutex
|
|
|
|
attr_reader :cond
|
|
|
|
def initialize
|
|
|
|
@mutex = ::Mutex.new
|
|
|
|
@cond = ::ConditionVariable.new
|
2015-10-05 13:13:00 -04:00
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
def processor_died(inst, err)
|
|
|
|
@latest_error = err
|
|
|
|
@mutex.synchronize do
|
|
|
|
@cond.signal
|
2015-10-21 01:04:36 -04:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
def processor_stopped(inst)
|
|
|
|
@mutex.synchronize do
|
|
|
|
@cond.signal
|
2015-10-09 00:50:45 -04:00
|
|
|
end
|
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
def options
|
2022-03-03 15:50:03 -05:00
|
|
|
opts = {concurrency: 3, queues: ["default"]}
|
2020-06-19 11:39:18 -04:00
|
|
|
opts[:fetch] = Sidekiq::BasicFetch.new(opts)
|
|
|
|
opts
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
|
|
|
end
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can process" do
|
2019-02-28 15:43:50 -05:00
|
|
|
mgr = Mgr.new
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2020-06-19 11:39:18 -04:00
|
|
|
p = Sidekiq::Processor.new(mgr, mgr.options)
|
2019-02-28 15:43:50 -05:00
|
|
|
JoeWorker.perform_async(0)
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
a = $count
|
|
|
|
p.process_one
|
|
|
|
b = $count
|
|
|
|
assert_equal a + 1, b
|
|
|
|
end
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "deals with errors" do
|
2019-02-28 15:43:50 -05:00
|
|
|
mgr = Mgr.new
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2020-06-19 11:39:18 -04:00
|
|
|
p = Sidekiq::Processor.new(mgr, mgr.options)
|
2019-02-28 15:43:50 -05:00
|
|
|
JoeWorker.perform_async("boom")
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
assert_equal 1, q.size
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
a = $count
|
|
|
|
mgr.mutex.synchronize do
|
|
|
|
p.start
|
|
|
|
mgr.cond.wait(mgr.mutex)
|
2015-10-05 13:13:00 -04:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
b = $count
|
|
|
|
assert_equal a, b
|
|
|
|
|
|
|
|
sleep 0.001
|
|
|
|
assert_equal false, p.thread.status
|
|
|
|
p.terminate(true)
|
|
|
|
refute_nil mgr.latest_error
|
|
|
|
assert_equal RuntimeError, mgr.latest_error.class
|
|
|
|
end
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "gracefully kills" do
|
2019-02-28 15:43:50 -05:00
|
|
|
mgr = Mgr.new
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2020-06-19 11:39:18 -04:00
|
|
|
p = Sidekiq::Processor.new(mgr, mgr.options)
|
2019-02-28 15:43:50 -05:00
|
|
|
JoeWorker.perform_async(1)
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
assert_equal 1, q.size
|
2015-10-05 13:13:00 -04:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
a = $count
|
|
|
|
p.start
|
|
|
|
sleep(0.05)
|
|
|
|
p.terminate
|
|
|
|
p.kill(true)
|
|
|
|
|
|
|
|
b = $count
|
|
|
|
assert_equal a, b
|
|
|
|
assert_equal false, p.thread.status
|
|
|
|
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
|