def test_stop_sets_stopped_to_true(self): """Tests that stop() sets the _stopped attribute to True.""" process = Process('cmd') process._process = mock.Mock() process.stop() self.assertTrue(process._stopped)
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)
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)
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)
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)
def test_sends_signal(self, mock_os, *_): """Tests that signal is sent to process..""" process = Process('cmd') mock_process = mock.Mock() mock_process.pid = -1 process._process = mock_process process.signal(51641) mock_os.assert_called_with(-1, 51641)
def test_signal_raises_error_on_windows(self, *_): """Tests that signaling is unsupported in windows with appropriate error msg.""" process = Process('cmd') mock_inner_process = mock.Mock() mock_inner_process.pid = -1 process._process = mock_inner_process with mock.patch('acts.libs.proc.process._on_windows', True): with self.assertRaises(ProcessError): process.signal(51641)
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)
def test_redirect_output_feeds_all_lines_to_on_output_callback(self): """Tests that _redirect_output loops until all lines are parsed.""" received_list = [] def appender(line): received_list.append(line) process = Process('cmd') process.set_on_output_callback(appender) process._process = mock.Mock() process._process.stdout.readline.side_effect = [b'a\n', b'b\n', b''] process._redirect_output() self.assertEqual(received_list[0], 'a') self.assertEqual(received_list[1], 'b') self.assertEqual(len(received_list), 2)
def test_stop_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._kill_process = test_call_order process._process.wait.side_effect = subprocess.TimeoutExpired('', '') process.stop() self.assertEqual(verifier.passed, True)