Created
May 5, 2017 14:59
-
-
Save keiththomps/af14e5f77fecb01583f29051c551ac6c to your computer and use it in GitHub Desktop.
Example signal handling test
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
require "spec_helper" | |
class MyClass | |
def self.run(command) | |
Signal.trap("TERM") { trap_term } | |
`#{command}` | |
end | |
def self.trap_term | |
puts "terminated" | |
exit 1 | |
end | |
end | |
RSpec.describe "testing signal handling" do | |
it "allows you to ensure that the code was run" do | |
output_reader, output_writer = IO.pipe | |
pid = fork do | |
$stdout = $stderr = output_writer | |
MyClass.run("sleep 2") | |
end | |
Process.kill("TERM", pid) | |
_, status = Process.waitpid2(pid) | |
output_writer.close | |
output = output_reader.read | |
expect(status.success?).to eql(false) # because of exit 1, see Process::Status | |
expect(output).to eql("terminated\n") | |
output_reader.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment