PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/core/test_signals.py

https://bitbucket.org/prologic/circuits/
Python | 69 lines | 46 code | 22 blank | 1 comment | 5 complexity | 92d22f08dfc7996abe1d59468670c472 MD5 | raw file
 1#!/usr/bin/env python
 2
 3
 4import pytest
 5
 6
 7import os
 8import sys
 9from time import sleep
10from errno import ESRCH
11from signal import SIGTERM
12from os import kill, remove
13from subprocess import Popen
14
15
16from . import signalapp
17
18
19def is_running(pid):
20    try:
21        kill(pid, 0)
22    except OSError as error:
23        if error.errno == ESRCH:
24            return False
25    return True
26
27
28def wait(pid, timeout=3):
29    count = timeout
30    while is_running(pid) and count:
31        sleep(1)
32
33
34def test(tmpdir):
35    if not os.name == "posix":
36        pytest.skip("Cannot run test on a non-POSIX platform.")
37
38    tmpdir.ensure(".pid")
39    tmpdir.ensure(".signal")
40    pidfile = str(tmpdir.join(".pid"))
41    signalfile = str(tmpdir.join(".signal"))
42
43    args = [sys.executable, signalapp.__file__, pidfile, signalfile]
44    cmd = " ".join(args)
45    p = Popen(cmd, shell=True, env={'PYTHONPATH': ':'.join(sys.path)})
46    status = p.wait()
47
48    assert status == 0
49
50    sleep(1)
51
52    assert os.path.exists(pidfile)
53    assert os.path.isfile(pidfile)
54
55    f = open(pidfile, "r")
56    pid = int(f.read().strip())
57    f.close()
58
59    kill(pid, SIGTERM)
60    wait(pid)
61
62    f = open(signalfile, "r")
63    signal = f.read().strip()
64    f.close()
65
66    assert signal == str(SIGTERM)
67
68    remove(pidfile)
69    remove(signalfile)