diff --git a/tests/test_parametric_printer.py b/tests/test_parametric_printer.py index 37af702..c228921 100644 --- a/tests/test_parametric_printer.py +++ b/tests/test_parametric_printer.py @@ -24,6 +24,7 @@ class TestCaseParametricPrinter(PexpectTestCase.PexpectTestCase): self._test(['a'], range(1,200), [1], [0]) + self.p.terminate() def test_random(self): @@ -45,6 +46,8 @@ class TestCaseParametricPrinter(PexpectTestCase.PexpectTestCase): self._test(['a', 'b', 'c'], [16], [16], [-1, 0, 1]) self._test(['a', 'b', 'c'], [16, 32, 64], [16, 32, 64], [-1, 0]) + self.p.terminate() + @unittest.skipIf(wexpect.spawn_class_name == 'legacy_wexpect', "legacy has bug around refreshing long consoles") def test_long_console(self): @@ -64,6 +67,8 @@ class TestCaseParametricPrinter(PexpectTestCase.PexpectTestCase): self._test(['a', 'b', 'c', 'd', 'e', 'f'], [8, 16, 32, 64], [64, 128, 256], [-1, 0]) + self.p.terminate() + def _test(self, character_list, character_count_list, line_count_list, speed_ms_list): # print(f'character_list: {character_list} character_count_list: {character_count_list} line_count_list: {line_count_list} speed_ms_list: {speed_ms_list}') diff --git a/tests/test_readline.py b/tests/test_readline.py index 8d498d4..85432ef 100644 --- a/tests/test_readline.py +++ b/tests/test_readline.py @@ -19,7 +19,7 @@ class ReadLineTestCase(PexpectTestCase.PexpectTestCase): fooPath = python_executable + ' ' + child_script prompt = ': ' num = 5 - + # Start the child process p = wexpect.spawn(fooPath) # Wait for prompt @@ -28,27 +28,39 @@ class ReadLineTestCase(PexpectTestCase.PexpectTestCase): p.expect('Bye!\r\n') expected_lines = p.before.splitlines(True) # Keep the line end expected_lines += [p.match.group()] - + + # Termination of the SpawnSocket is slow. We have to wait to prevent the failure of the next test. + if wexpect.spawn_class_name == 'SpawnSocket': + p.wait() + # Start the child process p = wexpect.spawn(fooPath) # Wait for prompt p.expect(prompt) - + p.sendline(str(num)) for i in range(num +2): # +1 the line of sendline +1: Bye line = p.readline() self.assertEqual(expected_lines[i], line) - + + # Termination of the SpawnSocket is slow. We have to wait to prevent the failure of the next test. + if wexpect.spawn_class_name == 'SpawnSocket': + p.wait() + # Start the child process p = wexpect.spawn(fooPath) # Wait for prompt p.expect(prompt) - + p.sendline(str(num)) readlines_lines = p.readlines() self.assertEqual(expected_lines, readlines_lines) - - + + # Termination of the SpawnSocket is slow. We have to wait to prevent the failure of the next test. + if wexpect.spawn_class_name == 'SpawnSocket': + p.wait() + + if __name__ == '__main__': unittest.main() diff --git a/wexpect/host.py b/wexpect/host.py index f04f0b8..0bd89f7 100644 --- a/wexpect/host.py +++ b/wexpect/host.py @@ -425,13 +425,12 @@ class SpawnBase: return True self.kill() - time.sleep(self.delayafterterminate) - if not self.isalive(): + if not self.isalive(timeout = self.delayafterterminate): return True return False - def isalive(self, trust_console=True): + def isalive(self, trust_console=True, timeout=0): """True if the child is still alive, false otherwise""" if trust_console: if self.flag_eof: @@ -445,8 +444,9 @@ class SpawnBase: return False try: - self.exitstatus = self.child_process.wait(timeout=0) + self.exitstatus = self.child_process.wait(timeout=timeout) logger.info(f'exitstatus: {self.exitstatus}') + return False except psutil.TimeoutExpired: return True @@ -890,7 +890,7 @@ class SpawnPipe(SpawnBase): # Sets delay in terminate() method to allow kernel time to update process status. Time in # seconds. - self.delayafterterminate = 1 + self.delayafterterminate = 2 def connect_to_child(self): pipe_name = 'wexpect_{}'.format(self.console_pid) @@ -1013,7 +1013,7 @@ class SpawnSocket(SpawnBase): # Sets delay in terminate() method to allow kernel time to update process status. Time in # seconds. - self.delayafterterminate = 1 + self.delayafterterminate = 2 def _send_impl(self, s): """This sends a string to the child process. This returns the number of