How to test signal handling in RSpec, particularly

2019-06-16 05:19发布

问题:

Heroku may send a SIGTERM to your application for various reasons, so I have created a handler to take care of some cleanup in case this happens. Some googling hasn't yielded any answers or examples on how to test this in RSpec. Here's the basic code:

Signal.trap('TERM') do  
    cleanup  
end

def cleanup
    puts "doing some cleanup stuff"
    ...
    exit
end

What's the best way to test that this cleanup method is called when the program receives a SIGTERM?

回答1:

Kill yourself! Send the signal to RSpec with Process.kill 'TERM', 0 and test that the handler is called. It's true that if the signal isn't trapped the test will crash rather than nicely reporting a failure, but at least you'll know there's a problem in your code.

For example:

class SignalHandler
  def self.trap_signals
    Signal.trap('TERM') { term_handler }
  end

  def self.term_handler
    # ...
  end

end

describe SignalHandler do
  describe '#trap_signals' do
    it "traps TERM" do
      # The MRI default TERM handler does not cause RSpec to exit with an error.
      # Use the system default TERM handler instead, which does kill RSpec.
      # If you test a different signal you might not need to do this,
      # or you might need to install a different signal's handler.
      old_signal_handler = Signal.trap 'TERM', 'SYSTEM_DEFAULT'

      SignalHandler.trap_signals
      expect(SignalHandler).to receive(:term_handler).with no_args
      Process.kill 'TERM', 0 # Send the signal to ourself

      # Put the Ruby default signal handler back in case it matters to other tests
      Signal.trap 'TERM', old_signal_handler
    end
  end
end

I merely tested that the handler was called, but you could equally well test a side effect of the handler.