Skip to content Skip to sidebar Skip to footer

(unit) Test Python Signal Handler

I have a simple Python service, where there is a loop that performs some action infinitely. On various signals, sys.exit(0) is called, which causes SystemExit to be raised and then

Solution 1:

You can trigger a SIGINT (or any signal) from another thread after some delay, which is received in the main thread. You can then assert on its effects just as in any other test, as below.

import os
import signal
import time
import threading
import unittest
from unittest.mock import (
    Mock,
    patch,
)

import service

classTestService(unittest.TestCase):

    @patch('service.print')deftest_signal_handling(self, mock_print):

        pid = os.getpid()

        deftrigger_signal():
            whilelen(mock_print.mock_calls) < 1:
                time.sleep(0.2)
            os.kill(pid, signal.SIGINT)

        thread = threading.Thread(target=trigger_signal)
        thread.daemon = True
        thread.start()

        service.main()

        self.assertEqual(mock_print.mock_calls[1][1][0], 'Some cleanup')


if __name__ == '__main__':
    unittest.main()

Solution 2:

Let's refactor that to make it easier to test:

defloop():
    try:
        print("Some action here")
    except:
        # clean up and re-raiseprint("Some cleanup")
        raisedefmain():

    defsignal_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)

    whileTrue:
        try:
            loop_body()
            time.sleep(10)
        except SystemExit:
            breakif __name__ == '__main__':
    main()

This doesn't allow easy testing of the signal handling code though. However, that amount is so small, rarely changed and strongly depends on the environment, that it is possible and perhaps even better to test manually.

For clarity, it could be useful to use a context handler, which is usually a good idea when you have setup/shutdown code. You don't mention the setup code, but my Crystall Ball (tm) tells me it exists. It could then be called like this:

try:
    with my_service() as service:
        whileTrue:
            service.run()
            sleep(10)
except SystemExit:
    # perform graceful shutdown on signalpass

I'll leave the implementation of that context manager to you, but check out contextlib, which makes it easy and fun.

Post a Comment for "(unit) Test Python Signal Handler"