Skip to content

Instantly share code, notes, and snippets.

@keiththomps
Created May 5, 2017 14:59
Show Gist options
  • Save keiththomps/af14e5f77fecb01583f29051c551ac6c to your computer and use it in GitHub Desktop.
Save keiththomps/af14e5f77fecb01583f29051c551ac6c to your computer and use it in GitHub Desktop.
Example signal handling test
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