Skip to content

Commit

Permalink
Merge pull request #7774 from drew2a/fix/5923
Browse files Browse the repository at this point in the history
Refactoring for the `enable_fault_handler`
  • Loading branch information
drew2a committed Dec 13, 2023
2 parents 30f11f8 + 267524a commit cea3186
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/tribler/core/check_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def set_process_priority(pid=None, priority_order=1):
logger.exception(e)


def enable_fault_handler(log_dir):
def enable_fault_handler(log_dir) -> bool:
"""
Enables fault handler if the module is available.
"""
Expand All @@ -108,8 +108,11 @@ def enable_fault_handler(log_dir):
log_dir.mkdir(parents=True, exist_ok=True)
crash_file = log_dir / "crash-report.log"
faulthandler.enable(file=open(str(crash_file), "w"), all_threads=True)
except ImportError:
logger.error("Fault Handler module not found.")
except (ImportError, OSError) as e:
logger.exception(e)
return False

return True


def check_and_enable_code_tracing(process_name, log_dir):
Expand Down
20 changes: 13 additions & 7 deletions src/tribler/core/tests/test_check_os.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from logging import Logger
from unittest.mock import MagicMock, Mock, patch

import psutil
Expand All @@ -25,26 +24,33 @@ def test_error_and_exit(mocked_show_system_popup, mocked_sys_exit):
@patch_import(['faulthandler'], strict=True, enable=MagicMock())
@patch('tribler.core.check_os.open', new=MagicMock())
def test_enable_fault_handler():
""" Test that the enable_fault_handler calls faulthandler.enable."""
import faulthandler
enable_fault_handler(log_dir=MagicMock())
assert enable_fault_handler(log_dir=MagicMock())
faulthandler.enable.assert_called_once()


@patch_import(['faulthandler'], strict=True, always_raise_exception_on_import=True)
@patch.object(Logger, 'error')
@patch('tribler.core.check_os.open', new=MagicMock())
def test_enable_fault_handler_import_error(mocked_log_error: MagicMock):
enable_fault_handler(log_dir=MagicMock())
mocked_log_error.assert_called_once()
def test_enable_fault_handler_import_error():
""" Test that the enable_fault_handler does not re-raise an exception derived from `ImportError`"""
assert not enable_fault_handler(log_dir=MagicMock())


@patch('tribler.core.check_os.open', new=MagicMock(side_effect=PermissionError))
def test_enable_fault_handler_os_error():
""" Test that the enable_fault_handler does not re-raise an exception derived from `OSError`"""
assert not enable_fault_handler(log_dir=MagicMock())


@patch_import(['faulthandler'], strict=True, enable=MagicMock())
@patch('tribler.core.check_os.open', new=MagicMock())
def test_enable_fault_handler_log_dir_not_exists():
""" Test that the enable_fault_handler creates the log directory if it does not exist."""
log_dir = MagicMock(exists=MagicMock(return_value=False),
mkdir=MagicMock())

enable_fault_handler(log_dir=log_dir)
assert enable_fault_handler(log_dir=log_dir)
log_dir.mkdir.assert_called_once()


Expand Down
2 changes: 1 addition & 1 deletion src/tribler/core/utilities/tests/test_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ def test_fix_win_long_file_win():
def test_fix_win_long_file_linux():
""" Test that fix_win_long_file works correct on Linux"""
path = Path('/home/user/.Tribler/7.7')
assert Path.fix_win_long_file(path) == '/home/user/.Tribler/7.7'
assert Path.fix_win_long_file(path) == str(path)

0 comments on commit cea3186

Please sign in to comment.