Beispiel #1
0
    def test_lifecycle_none(self):

        process = None

        assert not ProcessMonitor.is_process_alive(process)
        assert not ProcessMonitor.is_supported(process)
        assert not ProcessMonitor._pid(process)
        assert ProcessMonitor.exit_code(process) is None
Beispiel #2
0
    def test_lifecycle_popen(self):

        process = subprocess.Popen(
            ['python', '-c', 'import time; time.sleep(1)'])
        assert ProcessMonitor.is_process_alive(process)
        assert ProcessMonitor._pid(process)
        assert ProcessMonitor.is_supported(process)

        process.communicate()
        assert not ProcessMonitor.is_process_alive(process)
        assert ProcessMonitor.exit_code(process) is not None
Beispiel #3
0
    def test_lifecycle_multiprocessing(self):

        process = Process(target=sleep_1sec)
        assert not ProcessMonitor.is_process_alive(process)
        assert ProcessMonitor.is_supported(process)

        process.start()
        assert ProcessMonitor.is_process_alive(process)
        process.join()

        assert not ProcessMonitor.is_process_alive(process)
        assert ProcessMonitor.exit_code(process) is not None
Beispiel #4
0
    def test_exit_code(self):

        process_psutil = psutil.Popen.__new__(psutil.Popen, None)
        process_subprocess = subprocess.Popen.__new__(subprocess.Popen, None)
        process_multiprocessing = Process.__new__(Process, None)

        process_psutil.poll = Mock()
        process_subprocess.poll = Mock()
        process_multiprocessing._popen = Mock()
        process_multiprocessing._parent_pid = os.getpid()
        process_multiprocessing._name = "test"
        process_multiprocessing._daemonic = False

        process_psutil.returncode = None
        process_subprocess.returncode = None

        assert ProcessMonitor.is_process_alive(process_psutil)
        assert ProcessMonitor.is_process_alive(process_subprocess)
        with patch('multiprocessing.Process.is_alive',
                   side_effect=lambda: False):
            assert not ProcessMonitor.is_process_alive(process_multiprocessing)

        assert ProcessMonitor.exit_code(None) is None
        assert ProcessMonitor.exit_code(process_psutil) is None
        assert ProcessMonitor.exit_code(process_subprocess) is None
        with patch('multiprocessing.Process.exitcode') as exitcode:
            exitcode.__get__ = Mock(return_value=None)
            assert ProcessMonitor.exit_code(process_multiprocessing) is None

        process_psutil.poll = Mock()
        process_psutil.returncode = 0

        process_subprocess.poll = Mock()
        process_subprocess.returncode = 0

        assert not ProcessMonitor.is_process_alive(None)
        assert not ProcessMonitor.is_process_alive(process_psutil)
        assert not ProcessMonitor.is_process_alive(process_subprocess)

        with patch('multiprocessing.Process.exitcode') as exitcode:
            exitcode.__get__ = Mock(return_value=0)
            assert not ProcessMonitor.is_process_alive(process_multiprocessing)

        assert ProcessMonitor.exit_code(process_psutil) == 0
        assert ProcessMonitor.exit_code(process_subprocess) == 0

        with patch('multiprocessing.Process.exitcode') as exitcode:
            exitcode.__get__ = Mock(return_value=0)
            assert ProcessMonitor.exit_code(process_multiprocessing) == 0