From 2516505f8e0e3a5f82ff792cd0040266c73071c3 Mon Sep 17 00:00:00 2001 From: Sandip Pandey Date: Tue, 10 Oct 2023 13:17:55 +0200 Subject: [PATCH] Mark current process with matching pid to be running --- .../core/utilities/process_manager/process.py | 5 +++- .../process_manager/tests/test_process.py | 29 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/tribler/core/utilities/process_manager/process.py b/src/tribler/core/utilities/process_manager/process.py index 875d5c43754..45f34a41eaa 100644 --- a/src/tribler/core/utilities/process_manager/process.py +++ b/src/tribler/core/utilities/process_manager/process.py @@ -140,7 +140,7 @@ def current_process(cls, kind: ProcessKind, def is_current_process(self) -> bool: """Returns True if the object represents the current process""" - return self.pid == os.getpid() and self.is_running() + return self.pid == os.getpid() @with_retry def become_primary(self) -> bool: @@ -159,6 +159,9 @@ def become_primary(self) -> bool: def is_running(self): """Returns True if the object represents a running process""" + if self.is_current_process(): + return True + if not psutil.pid_exists(self.pid): return False diff --git a/src/tribler/core/utilities/process_manager/tests/test_process.py b/src/tribler/core/utilities/process_manager/tests/test_process.py index 58568bebca6..27babbcda34 100644 --- a/src/tribler/core/utilities/process_manager/tests/test_process.py +++ b/src/tribler/core/utilities/process_manager/tests/test_process.py @@ -42,37 +42,48 @@ def current_process_fixture(process_manager): return process_manager.current_process +@pytest.fixture(name='non_current_process') +def non_current_process_fixture(process_manager: ProcessManager): + process_manager.connection = Mock() + process_manager.current_process.is_current_process = lambda: False + return process_manager.current_process + + +def test_current_process_is_running(current_process): + assert current_process.is_running() is True + + @patch('psutil.pid_exists') -def test_is_running_pid_does_not_exists(pid_exists: Mock, current_process): +def test_is_running_pid_does_not_exists(pid_exists: Mock, non_current_process): pid_exists.return_value = False # if the pid does not exist, the process is not running assert not pid_exists.called - assert current_process.is_running() is False + assert non_current_process.is_running() is False assert pid_exists.called @patch('psutil.Process') -def test_is_running_process_not_running(process_class: Mock, current_process): +def test_is_running_process_not_running(process_class: Mock, non_current_process): process_class.side_effect = psutil.Error # if the instantiation of the Process instance lead to psutil.Error, the process is not running - assert current_process.is_running() is False + assert non_current_process.is_running() is False assert process_class.called @patch('psutil.Process') -def test_is_running_zombie_process(process_class: Mock, current_process): +def test_is_running_zombie_process(process_class: Mock, non_current_process): process_class.return_value.status.return_value = psutil.STATUS_ZOMBIE # if the process is zombie, it is not considered to be running - assert current_process.is_running() is False + assert non_current_process.is_running() is False @patch('psutil.Process') -def test_is_running_incorrect_process_create_time(process_class: Mock, current_process): +def test_is_running_incorrect_process_create_time(process_class: Mock, non_current_process): process = process_class.return_value process.status.return_value = psutil.STATUS_RUNNING - process.create_time.return_value = current_process.started_at + 1 + process.create_time.return_value = non_current_process.started_at + 1 # if the process with the specified pid was created after the specified time, it is a different process - assert current_process.is_running() is False + assert non_current_process.is_running() is False @patch('psutil.Process')