/tests/app/test_daemon.py

https://bitbucket.org/prologic/circuits/ · Python · 54 lines · 35 code · 18 blank · 1 comment · 7 complexity · 1ae4211fe54e0d86b41302c7f5887aad MD5 · raw file

  1. #!/usr/bin/env python
  2. import pytest
  3. if pytest.PLATFORM == "win32":
  4. pytest.skip("Unsupported Platform")
  5. import sys
  6. from os import kill
  7. from time import sleep
  8. from errno import ESRCH
  9. from signal import SIGTERM
  10. from subprocess import Popen
  11. from . import app
  12. def is_running(pid):
  13. try:
  14. kill(pid, 0)
  15. except OSError as error:
  16. if error.errno == ESRCH:
  17. return False
  18. return True
  19. def wait(pid, timeout=3):
  20. count = timeout
  21. while is_running(pid) and count:
  22. sleep(1)
  23. def test(tmpdir):
  24. tmpdir.ensure("app.pid")
  25. pid_path = tmpdir.join("app.pid")
  26. args = [sys.executable, app.__file__, str(pid_path)]
  27. Popen(args, env={'PYTHONPATH': ':'.join(sys.path)}).wait()
  28. sleep(1)
  29. assert pid_path.check(exists=True, file=True)
  30. pid = None
  31. with pid_path.open() as f:
  32. pid = int(f.read().strip())
  33. assert isinstance(pid, int)
  34. assert pid > 0
  35. kill(pid, SIGTERM)
  36. wait(pid)