mperham--sidekiq/test/launcher_test.rb

91 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require_relative "helper"
require "sidekiq/launcher"
2015-10-06 19:43:01 +00:00
2019-03-01 21:25:56 +00:00
describe Sidekiq::Launcher do
before do
@config = reset!
2022-09-01 22:47:24 +00:00
@config.default_capsule.concurrency = 3
@config[:tag] = "myapp"
2019-03-01 21:25:56 +00:00
end
2015-10-06 19:43:01 +00:00
describe "memory collection" do
it "works in any test environment" do
kb = Sidekiq::Launcher::MEMORY_GRABBER.call($$)
refute_nil kb
assert kb > 0
end
end
2022-09-01 22:47:24 +00:00
it "starts and stops" do
l = Sidekiq::Launcher.new(@config)
l.run(async_beat: false)
l.stop
2022-09-01 22:47:24 +00:00
end
describe "heartbeat" do
2015-10-06 19:43:01 +00:00
before do
@launcher = Sidekiq::Launcher.new(@config)
@id = @launcher.identity
2019-03-01 21:25:56 +00:00
Sidekiq::Processor::WORK_STATE.set("a", {"b" => 1})
2019-03-01 21:25:56 +00:00
@proctitle = $0
2015-10-06 19:43:01 +00:00
end
after do
Sidekiq::Processor::WORK_STATE.clear
$0 = @proctitle
2015-10-06 19:43:01 +00:00
end
it "stores process info in redis" do
@launcher.beat
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy]", $0
workers, rtt = @config.redis { |c| c.hmget(@id, "busy", "rtt_us") }
2015-10-06 19:43:01 +00:00
assert_equal "1", workers
refute_nil rtt
assert_in_delta 1000, rtt.to_i, 1000
2015-10-06 19:43:01 +00:00
expires = @config.redis { |c| c.pttl(@id) }
assert_in_delta 60000, expires, 500
end
it "fires start heartbeat event only once" do
cnt = 0
@config.on(:heartbeat) do
cnt += 1
2015-10-06 19:43:01 +00:00
end
assert_equal 0, cnt
@launcher.heartbeat
assert_equal 1, cnt
@launcher.heartbeat
assert_equal 1, cnt
end
2015-10-06 19:43:01 +00:00
it "quiets" do
@launcher.quiet
@launcher.beat
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] stopping", $0
2019-03-01 21:25:56 +00:00
@launcher.beat
info = @config.redis { |c| c.hmget(@id, "busy") }
assert_equal ["1"], info
2019-03-01 21:25:56 +00:00
expires = @config.redis { |c| c.pttl(@id) }
assert_in_delta 60000, expires, 50
2019-03-01 21:25:56 +00:00
end
it "allows arbitrary proctitle extensions" do
Sidekiq::Launcher::PROCTITLES << proc { "xyz" }
@launcher.beat
Sidekiq::Launcher::PROCTITLES.pop
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] xyz", $0
2019-03-01 21:25:56 +00:00
end
end
2015-10-06 19:43:01 +00:00
end