summaryrefslogtreecommitdiff
path: root/pexpect/tests/test_isalive.py
blob: 5b7e91bbd94ebe42a0a0b4a9ec9875bfe5fdb564 (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
#!/usr/bin/env python
import pexpect
import unittest
import sys, os, time

class IsAliveTestCase(unittest.TestCase):
        
    def test_expect_isalive1 (self):
        p = pexpect.spawn('ls')
        p.expect(pexpect.EOF)
        if p.isalive():
            self.fail ('Child process is not dead. It should be.')

    def test_expect_isalive2 (self):
        p = pexpect.spawn('cat', timeout=5)
        if not p.isalive():
            self.fail ('Child process is not alive. It should be.')
        p.kill(1)
	# 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_isalive3 (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():
#            pid, sts = os.waitpid(p.pid, 0)#, os.WNOHANG)
#            print 'p.pid, pid, sts:', p.pid, pid, sts
#            pp = pexpect.spawn('ps -p %d' % p.pid)
#            pp.expect (pexpect.EOF)
#            print pp.before
            self.fail ('Child process is not dead. It should be.')

### Some platforms allow this. Some reset status after call to waitpid.
    def OFF_test_expect_isalive4 (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')