forked from dimier/python-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_daemon.py
More file actions
78 lines (62 loc) · 2.12 KB
/
test_daemon.py
File metadata and controls
78 lines (62 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import sys
import time
import unittest
from daemon import Daemon
class TDaemon(Daemon):
def __init__(self, *args, **kwargs):
super(TDaemon, self).__init__(*args, **kwargs)
testoutput = open('testing_daemon', 'w')
testoutput.write('inited')
testoutput.close()
def run(self):
time.sleep(0.3)
testoutput = open('testing_daemon', 'w')
testoutput.write('finished')
testoutput.close()
def control_daemon(action):
os.system(" ".join((sys.executable, __file__, action)))
class TestDaemon(unittest.TestCase):
testoutput = None
def setUp(self):
control_daemon('start')
time.sleep(0.1)
self.testoutput = open('testing_daemon')
def test_daemon_can_start(self):
assert os.path.exists('testing_daemon.pid')
assert self.testoutput.read() == 'inited'
def test_daemon_can_stop(self):
control_daemon('stop')
time.sleep(0.1)
assert os.path.exists('testing_daemon.pid') is False
assert self.testoutput.read() == 'inited'
def test_daemon_can_finish(self):
time.sleep(0.4)
assert os.path.exists('testing_daemon.pid') is False
assert self.testoutput.read() == 'finished'
def test_daemon_can_restart(self):
assert os.path.exists('testing_daemon.pid')
pidfile = open('testing_daemon.pid')
pid1 = pidfile.read()
pidfile.close()
control_daemon('restart')
time.sleep(0.1)
assert os.path.exists('testing_daemon.pid')
pidfile = open('testing_daemon.pid')
pid2 = pidfile.read()
pidfile.close()
assert pid1 != pid2
def tearDown(self):
self.testoutput.close()
if os.path.exists('testing_daemon.pid'):
control_daemon('stop')
time.sleep(0.05)
os.system('rm testing_daemon*')
if __name__ == '__main__':
if len(sys.argv) == 1:
unittest.main()
elif len(sys.argv) == 2:
arg = sys.argv[1]
if arg in ('start', 'stop', 'restart'):
d = TDaemon('testing_daemon.pid', verbose=0)
getattr(d, arg)()