blob: 68b18c7dd8abdf58da0af089b0045c27d92b4f41 (
plain)
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
79
80
81
82
83
84
85
86
|
#!/usr/bin/env python
import pexpect
import unittest
import sys, os, time
import PexpectTestCase
class IsAliveTestCase(PexpectTestCase.PexpectTestCase):
def test_expect_wait (self):
"""This tests that calling wait on a finished process works as expected.
"""
p = pexpect.spawn('sleep 3')
if not p.isalive():
self.fail ('Child process is not alive. It should be.')
time.sleep(1)
p.wait()
if p.isalive():
self.fail ('Child process is not dead. It should be.')
p = pexpect.spawn('sleep 3')
if not p.isalive():
self.fail ('Child process is not alive. It should be.')
p.kill(9)
time.sleep(1)
try:
p.wait()
except pexpect.ExceptionPexpect, e:
pass
else:
self.fail ('Should have raised ExceptionPython because you can\'t call wait on a dead process.')
def test_expect_isalive_dead_after_normal_termination (self):
p = pexpect.spawn('ls')
p.expect(pexpect.EOF)
time.sleep(1) # allow kernel status time to catch up with state.
if p.isalive():
self.fail ('Child process is not dead. It should be.')
def test_expect_isalive_dead_after_SIGINT (self):
p = pexpect.spawn('cat', timeout=5)
if not p.isalive():
self.fail ('Child process is not alive. It should be.')
p.terminate()
# Solaris is kind of slow.
# Without this delay then p.expect(...) will not see
# that the process is dead and it will timeout.
time.sleep(1)
p.expect(pexpect.EOF)
if p.isalive():
self.fail ('Child process is not dead. It should be.')
def test_expect_isalive_dead_after_SIGKILL (self):
p = pexpect.spawn('cat', timeout=3)
if not p.isalive():
self.fail ('Child process is not alive. It should be.')
p.kill(9)
# Solaris is kind of slow.
# Without this delay then p.expect(...) will not see
# that the process is dead and it will timeout.
time.sleep(1)
p.expect(pexpect.EOF)
if p.isalive():
self.fail ('Child process is not dead. It should be.')
### Some platforms allow this. Some reset status after call to waitpid.
def test_expect_isalive_consistent_multiple_calls (self):
"""This tests that multiple calls to isalive() return same value.
"""
p = pexpect.spawn('cat')
if not p.isalive():
self.fail ('Child process is not alive. It should be.')
if not p.isalive():
self.fail ('Second call. Child process is not alive. It should be.')
p.kill(9)
p.expect(pexpect.EOF)
if p.isalive():
self.fail ('Child process is not dead. It should be.')
if p.isalive():
self.fail ('Second call. Child process is not dead. It should be.')
if __name__ == '__main__':
unittest.main()
suite = unittest.makeSuite(IsAliveTestCase, 'test')
|