/tests/core/test_signals.py

https://bitbucket.org/prologic/circuits/ · Python · 69 lines · 46 code · 22 blank · 1 comment · 6 complexity · 92d22f08dfc7996abe1d59468670c472 MD5 · raw file

  1. #!/usr/bin/env python
  2. import pytest
  3. import os
  4. import sys
  5. from time import sleep
  6. from errno import ESRCH
  7. from signal import SIGTERM
  8. from os import kill, remove
  9. from subprocess import Popen
  10. from . import signalapp
  11. def is_running(pid):
  12. try:
  13. kill(pid, 0)
  14. except OSError as error:
  15. if error.errno == ESRCH:
  16. return False
  17. return True
  18. def wait(pid, timeout=3):
  19. count = timeout
  20. while is_running(pid) and count:
  21. sleep(1)
  22. def test(tmpdir):
  23. if not os.name == "posix":
  24. pytest.skip("Cannot run test on a non-POSIX platform.")
  25. tmpdir.ensure(".pid")
  26. tmpdir.ensure(".signal")
  27. pidfile = str(tmpdir.join(".pid"))
  28. signalfile = str(tmpdir.join(".signal"))
  29. args = [sys.executable, signalapp.__file__, pidfile, signalfile]
  30. cmd = " ".join(args)
  31. p = Popen(cmd, shell=True, env={'PYTHONPATH': ':'.join(sys.path)})
  32. status = p.wait()
  33. assert status == 0
  34. sleep(1)
  35. assert os.path.exists(pidfile)
  36. assert os.path.isfile(pidfile)
  37. f = open(pidfile, "r")
  38. pid = int(f.read().strip())
  39. f.close()
  40. kill(pid, SIGTERM)
  41. wait(pid)
  42. f = open(signalfile, "r")
  43. signal = f.read().strip()
  44. f.close()
  45. assert signal == str(SIGTERM)
  46. remove(pidfile)
  47. remove(signalfile)