How to test signal handling in RSpec, particularly handling of SIGTERM?

别说谁变了你拦得住时间么 提交于 2019-12-08 16:03:17

问题


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.



来源:https://stackoverflow.com/questions/12571582/how-to-test-signal-handling-in-rspec-particularly-handling-of-sigterm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!