From 9ce23fe55684845b8b695b28c7bb77c0492140b8 Mon Sep 17 00:00:00 2001 From: Anastasiya Semenkevich Date: Tue, 11 Jun 2024 17:04:06 +0300 Subject: [PATCH] chore: squeeze 2 kill methods to 1 --- test/e2e/driver/aut.py | 9 +------- test/e2e/driver/server.py | 2 +- test/e2e/scripts/utils/local_system.py | 29 +++++++------------------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/test/e2e/driver/aut.py b/test/e2e/driver/aut.py index 6a9aa2af20..f2948f7dfa 100644 --- a/test/e2e/driver/aut.py +++ b/test/e2e/driver/aut.py @@ -73,13 +73,6 @@ class AUT: squish.currentApplicationContext().detach() self.ctx = None - def kill_process(self): - if self.pid is None: - LOG.warning('No PID available for AUT.') - return - local_system.kill_process_with_retries(self.pid) - self.pid = None - @allure.step('Attach Squish to Test Application') def attach(self, timeout_sec: int = configs.timeouts.PROCESS_TIMEOUT_SEC): LOG.info('Attaching to AUT: localhost:%d', self.port) @@ -123,7 +116,7 @@ class AUT: def stop(self): LOG.info('Stopping AUT: %s', self.path) self.detach_context() - self.kill_process() + local_system.kill_process(self.pid) @allure.step("Start and attach AUT") def launch(self) -> 'AUT': diff --git a/test/e2e/driver/server.py b/test/e2e/driver/server.py index fb5d195c3d..ea8ddbcebc 100644 --- a/test/e2e/driver/server.py +++ b/test/e2e/driver/server.py @@ -42,7 +42,7 @@ class SquishServer: if cls.pid is None: return LOG.info('Stopping Squish Server with PID: %d', cls.pid) - local_system.kill_process_with_retries(cls.pid) + local_system.kill_process(cls.pid) cls.pid = None cls.port = None diff --git a/test/e2e/scripts/utils/local_system.py b/test/e2e/scripts/utils/local_system.py index ae5d613d2d..882597e800 100644 --- a/test/e2e/scripts/utils/local_system.py +++ b/test/e2e/scripts/utils/local_system.py @@ -36,34 +36,19 @@ def find_free_port(start: int, step: int): def kill_process(pid, sig: signal.Signals = signal.SIGKILL): LOG.debug('Sending %s to %d process', sig.name, pid) try: - os.kill(pid, sig) + p = psutil.Process(pid) + except ProcessLookupError as err: LOG.error('Failed to find process %d: %s', pid, err) raise err - - -@allure.step('Kill process with retries') -def kill_process_with_retries(pid, sig: signal.Signals = signal.SIGKILL, attempts: int = 3): - LOG.debug('Killing process: %d', pid) - try: - p = psutil.Process(pid) - except psutil.NoSuchProcess: - LOG.warning('Process %d already gone.', pid) - return - - p.send_signal(sig) - - while attempts > 0: - attempts -= 1 + for i in range(2): try: LOG.warning('Waiting for process to exit: %d', pid) - p.wait() - except TimeoutError as err: p.kill() - else: - return - - raise RuntimeError('Failed to kill process: %d' % pid) + if not p.is_running(): + break + except RuntimeError: + raise ('Failed to kill process: %d' % pid) @allure.step('System execute command')