title: (Unit) Test python signal handler
body: <p>I have a simple Python service, where there is a loop that performs some action infinitely. On various signals, <code>sys.exit(0)</code> is called, which causes <code>SystemExit</code> to be raised and then some cleanup should happen if it can.</p>
  
  <p>In a test, i.e. standard <code>unittest.TestCase</code>, I would like to test that this cleanup happens and the loop exits. However, I'm stuck on even getting the signal to be triggered / <code>SystemExit</code> to be raised.</p>
  
  <pre><code># service.py
  import signal
  import sys
  import time
  
  def main():
  
      def signal_handler(signalnum, _):
          # How to get this to block to run in a test?
          sys.exit(0)
  
      signal.signal(signal.SIGINT, signal_handler)
      signal.signal(signal.SIGTERM, signal_handler)
  
      while True:
          try:
              print("Some action here")
              time.sleep(10)
          except SystemExit:
              # How to get this to block to run in a test?
              print("Some cleanup")
              break
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>How can the code enter the <code>SystemExit</code> handler / signal handler in the test environment? An alternative pattern would also be welcome.</p>