2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2018-12-30 15:15:21 -05:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
require_relative 'helper'
|
|
|
|
require 'sidekiq/launcher'
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe Sidekiq::Launcher do
|
|
|
|
subject { Sidekiq::Launcher.new(options) }
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_manager(opts)
|
|
|
|
Sidekiq::Manager.new(opts)
|
|
|
|
end
|
2015-10-06 15:43:01 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'heartbeat' do
|
2015-10-06 15:43:01 -04:00
|
|
|
before do
|
2019-03-01 16:25:56 -05:00
|
|
|
@mgr = new_manager(options)
|
|
|
|
@launcher = Sidekiq::Launcher.new(options)
|
|
|
|
@launcher.manager = @mgr
|
|
|
|
@id = @launcher.identity
|
|
|
|
|
|
|
|
Sidekiq::Processor::WORKER_STATE.set('a', {'b' => 1})
|
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
@proctitle = $0
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
after do
|
|
|
|
Sidekiq::Processor::WORKER_STATE.clear
|
|
|
|
$0 = @proctitle
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
describe '#heartbeat' do
|
|
|
|
describe 'run' do
|
|
|
|
it 'sets sidekiq version, tag and the number of busy workers to proctitle' do
|
|
|
|
subject.heartbeat
|
2015-10-06 15:43:01 -04:00
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy]", $0
|
|
|
|
end
|
2015-10-06 15:43:01 -04:00
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
it 'stores process info in redis' do
|
|
|
|
subject.heartbeat
|
2015-10-06 15:43:01 -04:00
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
workers = Sidekiq.redis { |c| c.hmget(subject.identity, 'busy') }
|
2015-10-06 15:43:01 -04:00
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
assert_equal ["1"], workers
|
2016-08-12 15:34:41 -04:00
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
expires = Sidekiq.redis { |c| c.pttl(subject.identity) }
|
2015-10-06 15:43:01 -04:00
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
assert_in_delta 60000, expires, 500
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
describe 'events' do
|
|
|
|
before do
|
|
|
|
@cnt = 0
|
|
|
|
|
|
|
|
Sidekiq.on(:heartbeat) do
|
|
|
|
@cnt += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fires start heartbeat event only once' do
|
|
|
|
assert_equal 0, @cnt
|
|
|
|
subject.heartbeat
|
|
|
|
assert_equal 1, @cnt
|
|
|
|
subject.heartbeat
|
|
|
|
assert_equal 1, @cnt
|
|
|
|
end
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'quiet' do
|
2015-10-06 15:43:01 -04:00
|
|
|
before do
|
2018-12-30 15:15:21 -05:00
|
|
|
subject.quiet
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
|
|
|
|
2018-12-30 15:15:21 -05:00
|
|
|
it 'sets stopping proctitle' do
|
|
|
|
subject.heartbeat
|
2016-05-04 15:43:15 -04:00
|
|
|
|
2015-10-07 18:01:44 -04:00
|
|
|
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] stopping", $0
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores process info in redis' do
|
2018-12-30 15:15:21 -05:00
|
|
|
subject.heartbeat
|
|
|
|
|
|
|
|
info = Sidekiq.redis { |c| c.hmget(subject.identity, 'busy') }
|
|
|
|
|
2015-10-07 18:01:44 -04:00
|
|
|
assert_equal ["1"], info
|
2018-12-30 15:15:21 -05:00
|
|
|
|
|
|
|
expires = Sidekiq.redis { |c| c.pttl(subject.identity) }
|
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
assert_in_delta 60000, expires, 50
|
|
|
|
end
|
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
|
|
|
|
it 'fires new heartbeat events' do
|
|
|
|
i = 0
|
|
|
|
Sidekiq.on(:heartbeat) do
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
assert_equal 0, i
|
|
|
|
@launcher.heartbeat
|
|
|
|
assert_equal 1, i
|
|
|
|
@launcher.heartbeat
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when manager is active' do
|
|
|
|
before do
|
|
|
|
Sidekiq::Launcher::PROCTITLES << proc { "xyz" }
|
|
|
|
@launcher.heartbeat
|
|
|
|
Sidekiq::Launcher::PROCTITLES.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets useful info to proctitle' do
|
|
|
|
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] xyz", $0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores process info in redis' do
|
|
|
|
info = Sidekiq.redis { |c| c.hmget(@id, 'busy') }
|
|
|
|
assert_equal ["1"], info
|
|
|
|
expires = Sidekiq.redis { |c| c.pttl(@id) }
|
|
|
|
assert_in_delta 60000, expires, 500
|
|
|
|
end
|
|
|
|
end
|
2019-03-01 16:37:04 -05:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
|
|
|
|
describe 'when manager is stopped' do
|
|
|
|
before do
|
|
|
|
@launcher.quiet
|
|
|
|
@launcher.heartbeat
|
|
|
|
end
|
|
|
|
|
|
|
|
#after do
|
|
|
|
#puts system('redis-cli -n 15 keys "*" | while read LINE ; do TTL=`redis-cli -n 15 ttl "$LINE"`; if [ "$TTL" -eq -1 ]; then echo "$LINE"; fi; done;')
|
|
|
|
#end
|
|
|
|
|
|
|
|
it 'indicates stopping status in proctitle' do
|
|
|
|
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] stopping", $0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores process info in redis' do
|
|
|
|
info = Sidekiq.redis { |c| c.hmget(@id, 'busy') }
|
|
|
|
assert_equal ["1"], info
|
|
|
|
expires = Sidekiq.redis { |c| c.pttl(@id) }
|
|
|
|
assert_in_delta 60000, expires, 50
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
{ :concurrency => 3, :queues => ['default'], :tag => 'myapp' }
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
|
2015-10-06 15:43:01 -04:00
|
|
|
end
|