Last active
June 10, 2019 19:48
-
-
Save ndbroadbent/93b56b183141ff395b8ef1497f0f4e4d to your computer and use it in GitHub Desktop.
An RSpec test that ensure that the Sidekiq worker can actually boot without crashing, and runs a real test job from Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# spec/jobs/worker_health_check_job_spec.rb starts a Sidekiq worker process | |
# and checks stdout for some expected strings. This ensures that Sidekiq | |
# can boot without crashing, and run a real job from a Redis queue. | |
class SidekiqHealthCheckJob | |
include Sidekiq::Worker | |
def perform(string) | |
puts "[SidekiqHealthCheckJob] => #{string}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
RSpec.describe SidekiqHealthCheckJob do | |
before(:each) do | |
Sidekiq::Testing.disable! | |
end | |
it 'starts a Sidekiq worker process and performs SidekiqHealthCheckJob' do | |
require 'pty' | |
random_string = SecureRandom.hex(32) | |
jid = SidekiqHealthCheckJob.perform_async(random_string) | |
sidekiq_output = [] | |
match_index = 0 | |
output_expectations = [ | |
/SidekiqHealthCheckJob JID-#{jid} INFO: start/, | |
/\[SidekiqHealthCheckJob\] => #{random_string}/, | |
/SidekiqHealthCheckJob JID-#{jid} INFO: done/, | |
] | |
begin | |
Timeout.timeout(60) do | |
begin | |
PTY.spawn('sidekiq 2>&1') do |reader, _, pid| | |
reader.each_line do |line| | |
sidekiq_output << line | |
next unless line.match?(output_expectations[match_index]) | |
match_index += 1 | |
next unless match_index == output_expectations.size | |
Process.kill(9, pid) | |
end | |
end | |
rescue Errno::EIO | |
nil | |
end | |
expect(match_index).to( | |
eq(output_expectations.size), | |
"Matches: #{match_index}, Output:\n#{sidekiq_output.join('')}" | |
) | |
end | |
rescue Timeout::Error | |
raise Timeout::Error, 'Sidekiq output did not match expectations! ' \ | |
"Output:\n#{sidekiq_output.join('')}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment