[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.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}')

View File

@ -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()

View File

@ -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