OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 ''' |
| 3 PEXPECT LICENSE |
| 4 |
| 5 This license is approved by the OSI and FSF as GPL-compatible. |
| 6 http://opensource.org/licenses/isc-license.txt |
| 7 |
| 8 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 |
| 20 ''' |
| 21 import pexpect |
| 22 import unittest |
| 23 import signal |
| 24 import sys |
| 25 import time |
| 26 from . import PexpectTestCase |
| 27 |
| 28 |
| 29 class IsAliveTestCase(PexpectTestCase.PexpectTestCase): |
| 30 """Various tests for the running status of processes.""" |
| 31 |
| 32 def test_expect_wait(self): |
| 33 """Ensure consistency in wait() and isalive().""" |
| 34 p = pexpect.spawn('sleep 1') |
| 35 assert p.isalive() |
| 36 assert p.wait() == 0 |
| 37 assert not p.isalive() |
| 38 # In previous versions of ptyprocess/pexpect, calling wait() a second |
| 39 # time would raise an exception, but not since v4.0 |
| 40 assert p.wait() == 0 |
| 41 |
| 42 def test_expect_wait_after_termination(self): |
| 43 """Ensure wait on a process terminated by kill -9.""" |
| 44 p = pexpect.spawn('sleep 3') |
| 45 assert p.isalive() |
| 46 p.kill(9) |
| 47 time.sleep(1) |
| 48 |
| 49 # when terminated, the exitstatus is None, but p.signalstatus |
| 50 # and p.terminated reflects that the kill -9 nature. |
| 51 assert p.wait() is None |
| 52 assert p.signalstatus == 9 |
| 53 assert p.terminated == True |
| 54 assert not p.isalive() |
| 55 |
| 56 def test_signal_wait(self): |
| 57 '''Test calling wait with a process terminated by a signal.''' |
| 58 if not hasattr(signal, 'SIGALRM'): |
| 59 return 'SKIP' |
| 60 p = pexpect.spawn(sys.executable, ['alarm_die.py']) |
| 61 p.wait() |
| 62 assert p.exitstatus is None |
| 63 self.assertEqual(p.signalstatus, signal.SIGALRM) |
| 64 |
| 65 def test_expect_isalive_dead_after_normal_termination (self): |
| 66 p = pexpect.spawn('ls', timeout=15) |
| 67 p.expect(pexpect.EOF) |
| 68 assert not p.isalive() |
| 69 |
| 70 def test_expect_isalive_dead_after_SIGHUP(self): |
| 71 p = pexpect.spawn('cat', timeout=5, ignore_sighup=False) |
| 72 assert p.isalive() |
| 73 force = False |
| 74 if sys.platform.lower().startswith('sunos'): |
| 75 # On Solaris (SmartOs), and only when executed from cron(1), SIGKILL |
| 76 # is required to end the sub-process. This is done using force=True |
| 77 force = True |
| 78 assert p.terminate(force) == True |
| 79 p.expect(pexpect.EOF) |
| 80 assert not p.isalive() |
| 81 |
| 82 def test_expect_isalive_dead_after_SIGINT(self): |
| 83 p = pexpect.spawn('cat', timeout=5) |
| 84 assert p.isalive() |
| 85 force = False |
| 86 if sys.platform.lower().startswith('sunos'): |
| 87 # On Solaris (SmartOs), and only when executed from cron(1), SIGKILL |
| 88 # is required to end the sub-process. This is done using force=True |
| 89 force = True |
| 90 assert p.terminate(force) == True |
| 91 p.expect(pexpect.EOF) |
| 92 assert not p.isalive() |
| 93 |
| 94 def test_expect_isalive_dead_after_SIGKILL(self): |
| 95 p = pexpect.spawn('cat', timeout=5) |
| 96 assert p.isalive() |
| 97 p.kill(9) |
| 98 p.expect(pexpect.EOF) |
| 99 assert not p.isalive() |
| 100 |
| 101 def test_forced_terminate(self): |
| 102 p = pexpect.spawn(sys.executable, ['needs_kill.py']) |
| 103 p.expect('READY') |
| 104 assert p.terminate(force=True) == True |
| 105 p.expect(pexpect.EOF) |
| 106 assert not p.isalive() |
| 107 |
| 108 ### Some platforms allow this. Some reset status after call to waitpid. |
| 109 ### probably not necessary, isalive() returns early when terminate is False. |
| 110 def test_expect_isalive_consistent_multiple_calls (self): |
| 111 '''This tests that multiple calls to isalive() return same value. |
| 112 ''' |
| 113 p = pexpect.spawn('cat') |
| 114 assert p.isalive() |
| 115 assert p.isalive() |
| 116 p.sendeof() |
| 117 p.expect(pexpect.EOF) |
| 118 assert not p.isalive() |
| 119 assert not p.isalive() |
| 120 |
| 121 if __name__ == '__main__': |
| 122 unittest.main() |
| 123 |
| 124 suite = unittest.makeSuite(IsAliveTestCase, 'test') |
| 125 |
OLD | NEW |