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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/usr/bin/env python
'''
PEXPECT LICENSE
This license is approved by the OSI and FSF as GPL-compatible.
http://opensource.org/licenses/isc-license.txt
Copyright (c) 2012, Noah Spurrier <noah@noah.org>
PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
from __future__ import with_statement # bring 'with' stmt to py25
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 as 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')
|