PageRenderTime 68ms CodeModel.GetById 54ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/app/test_daemon.py

https://bitbucket.org/prologic/circuits/
Python | 54 lines | 35 code | 18 blank | 1 comment | 4 complexity | 1ae4211fe54e0d86b41302c7f5887aad MD5 | raw file
 1#!/usr/bin/env python
 2
 3
 4import pytest
 5if pytest.PLATFORM == "win32":
 6    pytest.skip("Unsupported Platform")
 7
 8
 9import sys
10from os import kill
11from time import sleep
12from errno import ESRCH
13from signal import SIGTERM
14from subprocess import Popen
15
16
17from . import app
18
19
20def is_running(pid):
21    try:
22        kill(pid, 0)
23    except OSError as error:
24        if error.errno == ESRCH:
25            return False
26    return True
27
28
29def wait(pid, timeout=3):
30    count = timeout
31    while is_running(pid) and count:
32        sleep(1)
33
34
35def test(tmpdir):
36    tmpdir.ensure("app.pid")
37    pid_path = tmpdir.join("app.pid")
38
39    args = [sys.executable, app.__file__, str(pid_path)]
40    Popen(args, env={'PYTHONPATH': ':'.join(sys.path)}).wait()
41
42    sleep(1)
43
44    assert pid_path.check(exists=True, file=True)
45
46    pid = None
47    with pid_path.open() as f:
48        pid = int(f.read().strip())
49
50    assert isinstance(pid, int)
51    assert pid > 0
52
53    kill(pid, SIGTERM)
54    wait(pid)