def test_is_running(self): # confirm we're running self.assertTrue(process.is_running(os.getpid())) # fork a new process, SIGSTOP it, and confirm it's not running pid = os.fork() if pid == 0: os.kill(os.getpid(), signal.SIGSTOP) else: # wait for signal to propagate time.sleep(1) # The return value is reliable only on Linux, on other systems just # make sure ProcessNotFound isn't thrown. if sys.platform.startswith('linux'): self.assertFalse(process.is_running(pid)) else: process.is_running(pid) os.kill(pid, signal.SIGKILL) with mock.patch('snakeoil.process.os.kill') as kill: kill.side_effect = OSError(3, 'No such process') self.assertRaises(process.ProcessNotFound, process.is_running, 1234) kill.side_effect = OSError(4, 'Interrupted system call') self.assertRaises(OSError, process.is_running, 1234) with mock.patch('snakeoil.process.open') as open: open.side_effect = OSError(2, 'No such file or directory') self.assertRaises(process.ProcessNotFound, process.is_running, os.getpid()) open.side_effect = OSError(5, 'Input/output error') self.assertRaises(OSError, process.is_running, os.getpid())
def test_is_running(self): # confirm we're running self.assertTrue(process.is_running(os.getpid())) # fork a new process, SIGSTOP it, and confirm it's not running pid = os.fork() if pid == 0: os.kill(os.getpid(), signal.SIGSTOP) else: # wait for signal to propagate time.sleep(1) self.assertFalse(process.is_running(pid)) os.kill(pid, signal.SIGKILL) with mock.patch('snakeoil.process.os.kill') as kill: kill.side_effect = OSError(3, 'No such process') self.assertRaises( process.ProcessNotFound, process.is_running, 1234) kill.side_effect = OSError(4, 'Interrupted system call') self.assertRaises( OSError, process.is_running, 1234) with mock.patch('snakeoil.process.open') as open: open.side_effect = OSError(2, 'No such file or directory') self.assertRaises( process.ProcessNotFound, process.is_running, os.getpid()) open.side_effect = OSError(5, 'Input/output error') self.assertRaises( OSError, process.is_running, os.getpid())
def test_is_running(self): # confirm we're running self.assertTrue(process.is_running(os.getpid())) # fork a new process, SIGSTOP it, and confirm it's not running pid = os.fork() if pid == 0: os.kill(os.getpid(), signal.SIGSTOP) else: # wait for signal to propagate time.sleep(1) # The return value is reliable only on Linux, on other systems just # make sure ProcessNotFound isn't thrown. if sys.platform.startswith('linux'): self.assertFalse(process.is_running(pid)) else: process.is_running(pid) os.kill(pid, signal.SIGKILL) with mock.patch('snakeoil.process.os.kill') as kill: kill.side_effect = OSError(3, 'No such process') self.assertRaises( process.ProcessNotFound, process.is_running, 1234) kill.side_effect = OSError(4, 'Interrupted system call') self.assertRaises( OSError, process.is_running, 1234) with mock.patch('snakeoil.process.open') as open: open.side_effect = OSError(2, 'No such file or directory') self.assertRaises( process.ProcessNotFound, process.is_running, os.getpid()) open.side_effect = OSError(5, 'Input/output error') self.assertRaises( OSError, process.is_running, os.getpid())
def is_alive(self): """Return whether the processor is alive.""" try: if self.pid is None: return False try: if process.is_running(self.pid): self.write("alive", disable_runtime_exceptions=True) if self.expect("yep!", timeout=10): return True except process.ProcessNotFound: # pid doesn't exist self.pid = None return False except (AttributeError, KeyboardInterrupt): # thrown only if failure occurred instantiation. return False