Example #1
0
    def test_wait_joins_redirection_thread_if_it_exists(self):
        """Tests wait() joins _listening_thread if it exists."""
        process = Process('cmd')
        process._process = mock.Mock()
        mocked_thread = mock.Mock()
        process._redirection_thread = mocked_thread

        process.wait(0)

        self.assertEqual(mocked_thread.join.called, True)
Example #2
0
    def test_wait_kills_after_timeout(self, *_):
        """Tests that if a TimeoutExpired error is thrown during wait, the
        process is killed."""
        process = Process('cmd')
        process._process = mock.Mock()
        process._process.wait.side_effect = subprocess.TimeoutExpired('', '')

        process.wait(0)

        self.assertEqual(process._kill_process.called, True)
Example #3
0
    def test_wait_raises_if_called_back_to_back(self):
        """Tests that wait raises an exception if it has already been called
        prior."""
        process = Process('cmd')
        process._process = mock.Mock()

        process.wait(0)
        expected_msg = 'Process is already being stopped.'
        with self.assertRaisesRegex(ProcessError, expected_msg):
            process.wait(0)
Example #4
0
    def test_wait_clears_redirection_thread_if_it_exists(self):
        """Tests wait() joins _listening_thread if it exists.

        Threads can only be started once, so after wait has been called, we
        want to make sure we clear the listening thread.
        """
        process = Process('cmd')
        process._process = mock.Mock()
        process._redirection_thread = mock.Mock()

        process.wait(0)

        self.assertEqual(process._redirection_thread, None)
Example #5
0
    def test_stop_calls_wait(self):
        """Tests that stop() also has the functionality of wait()."""
        process = Process('cmd')
        process._process = mock.Mock()
        process.wait = mock.Mock()

        process.stop()

        self.assertEqual(process.wait.called, True)
Example #6
0
    def test_wait_sets_stopped_to_true_before_process_kill(self, *_):
        """Tests that stop() sets the _stopped attribute to True.

        This order is required to prevent the _exec_loop from calling
        _on_terminate_callback when the user has killed the process.
        """
        verifier = mock.Mock()
        verifier.passed = False

        def test_call_order():
            self.assertTrue(process._stopped)
            verifier.passed = True

        process = Process('cmd')
        process._process = mock.Mock()
        process._process.poll.return_value = None
        process._process.wait.side_effect = subprocess.TimeoutExpired('', '')
        process._kill_process = test_call_order

        process.wait()

        self.assertEqual(verifier.passed, True)