[ADD] improove terminate function, reduce wait. [FIX] add fixes for testcases, to pass Socket tests.

This commit is contained in:
Benedek Racz 2020-04-05 13:26:51 +02:00
parent 215f688562
commit f53331ed61
3 changed files with 30 additions and 13 deletions

View File

@ -24,6 +24,7 @@ class TestCaseParametricPrinter(PexpectTestCase.PexpectTestCase):
self._test(['a'], range(1,200), [1], [0]) self._test(['a'], range(1,200), [1], [0])
self.p.terminate()
def test_random(self): 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], [16], [-1, 0, 1])
self._test(['a', 'b', 'c'], [16, 32, 64], [16, 32, 64], [-1, 0]) 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") @unittest.skipIf(wexpect.spawn_class_name == 'legacy_wexpect', "legacy has bug around refreshing long consoles")
def test_long_console(self): 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._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): 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}') # print(f'character_list: {character_list} character_count_list: {character_count_list} line_count_list: {line_count_list} speed_ms_list: {speed_ms_list}')

View File

@ -29,6 +29,10 @@ class ReadLineTestCase(PexpectTestCase.PexpectTestCase):
expected_lines = p.before.splitlines(True) # Keep the line end expected_lines = p.before.splitlines(True) # Keep the line end
expected_lines += [p.match.group()] 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 # Start the child process
p = wexpect.spawn(fooPath) p = wexpect.spawn(fooPath)
# Wait for prompt # Wait for prompt
@ -39,6 +43,10 @@ class ReadLineTestCase(PexpectTestCase.PexpectTestCase):
line = p.readline() line = p.readline()
self.assertEqual(expected_lines[i], line) 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 # Start the child process
p = wexpect.spawn(fooPath) p = wexpect.spawn(fooPath)
# Wait for prompt # Wait for prompt
@ -48,6 +56,10 @@ class ReadLineTestCase(PexpectTestCase.PexpectTestCase):
readlines_lines = p.readlines() readlines_lines = p.readlines()
self.assertEqual(expected_lines, readlines_lines) 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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -425,13 +425,12 @@ class SpawnBase:
return True return True
self.kill() self.kill()
time.sleep(self.delayafterterminate) if not self.isalive(timeout = self.delayafterterminate):
if not self.isalive():
return True return True
return False 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""" """True if the child is still alive, false otherwise"""
if trust_console: if trust_console:
if self.flag_eof: if self.flag_eof:
@ -445,8 +444,9 @@ class SpawnBase:
return False return False
try: try:
self.exitstatus = self.child_process.wait(timeout=0) self.exitstatus = self.child_process.wait(timeout=timeout)
logger.info(f'exitstatus: {self.exitstatus}') logger.info(f'exitstatus: {self.exitstatus}')
return False
except psutil.TimeoutExpired: except psutil.TimeoutExpired:
return True return True
@ -890,7 +890,7 @@ class SpawnPipe(SpawnBase):
# Sets delay in terminate() method to allow kernel time to update process status. Time in # Sets delay in terminate() method to allow kernel time to update process status. Time in
# seconds. # seconds.
self.delayafterterminate = 1 self.delayafterterminate = 2
def connect_to_child(self): def connect_to_child(self):
pipe_name = 'wexpect_{}'.format(self.console_pid) 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 # Sets delay in terminate() method to allow kernel time to update process status. Time in
# seconds. # seconds.
self.delayafterterminate = 1 self.delayafterterminate = 2
def _send_impl(self, s): def _send_impl(self, s):
"""This sends a string to the child process. This returns the number of """This sends a string to the child process. This returns the number of