-
Notifications
You must be signed in to change notification settings - Fork 129
[Core] Fixed Pickle file load error #1909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
57334b5
4d2d090
dbd06b1
eedc2d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import tempfile | ||
| from unittest.mock import patch | ||
| from port_ocean.utils.ipc import FileIPC | ||
|
|
||
|
|
||
| class TestFileIPCErrorHandling: | ||
| def test_save_and_load_normal_operation(self) -> None: | ||
| """Test that normal save and load operations work correctly.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| # Create a simple IPC instance and manually set file path to temp dir | ||
| ipc = FileIPC("test_process", "test_name", default_return="default") | ||
| ipc.file_path = f"{temp_dir}/test_name.pkl" | ||
|
|
||
| # Save data | ||
| test_data = {"key": "value", "number": 42} | ||
| ipc.save(test_data) | ||
|
|
||
| # Load data | ||
| loaded_data = ipc.load() | ||
| assert loaded_data == test_data | ||
|
|
||
| def test_load_missing_file_returns_default(self) -> None: | ||
| """Test that loading a non-existent file returns the default value.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with patch("port_ocean.utils.ipc.os.makedirs"): | ||
| ipc = FileIPC( | ||
| "test_process", "test_name", default_return="default_value" | ||
| ) | ||
| ipc.file_path = f"{temp_dir}/nonexistent.pkl" | ||
|
|
||
| result = ipc.load() | ||
| assert result == "default_value" | ||
|
|
||
| def test_load_corrupted_pickle_returns_default(self) -> None: | ||
| """Test that loading a corrupted pickle file returns default value and logs warning.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with patch("port_ocean.utils.ipc.os.makedirs"): | ||
| ipc = FileIPC("test_process", "test_name", default_return="fallback") | ||
| ipc.file_path = f"{temp_dir}/corrupted.pkl" | ||
|
|
||
| # Create a corrupted pickle file | ||
| with open(ipc.file_path, "wb") as f: | ||
| f.write(b"corrupted pickle data") | ||
|
|
||
| with patch("port_ocean.utils.ipc.logger.warning") as mock_logger: | ||
| result = ipc.load() | ||
|
|
||
| assert result == "fallback" | ||
| mock_logger.assert_called_once() | ||
| assert "Failed to load IPC data" in str(mock_logger.call_args) | ||
|
|
||
| def test_load_truncated_pickle_returns_default(self) -> None: | ||
| """Test that loading a truncated pickle file (EOFError) returns default value.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with patch("port_ocean.utils.ipc.os.makedirs"): | ||
| ipc = FileIPC("test_process", "test_name", default_return=[]) | ||
| ipc.file_path = f"{temp_dir}/truncated.pkl" | ||
|
|
||
| # Create a truncated pickle file (empty file) | ||
| with open(ipc.file_path, "wb"): | ||
| pass # Create empty file | ||
|
|
||
| with patch("port_ocean.utils.ipc.logger.warning") as mock_logger: | ||
| result = ipc.load() | ||
|
|
||
| assert result == [] | ||
| mock_logger.assert_called_once() | ||
|
|
||
| def test_load_type_error_during_unpickling_returns_default(self) -> None: | ||
| """Test that TypeError during unpickling (e.g., constructor mismatch) returns default value.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with patch("port_ocean.utils.ipc.os.makedirs"): | ||
| ipc = FileIPC( | ||
| "test_process", "test_name", default_return="type_error_fallback" | ||
| ) | ||
| ipc.file_path = f"{temp_dir}/type_error.pkl" | ||
|
|
||
| # Create a dummy file so existence check passes | ||
| with open(ipc.file_path, "wb") as f: | ||
| f.write(b"dummy content") | ||
|
|
||
| # Mock pickle.load to raise TypeError (simulating constructor signature mismatch) | ||
| with patch( | ||
| "pickle.load", | ||
| side_effect=TypeError( | ||
| "KindNotImplementedException.__init__() missing 1 required positional argument: 'available_kinds'" | ||
| ), | ||
| ): | ||
| with patch("port_ocean.utils.ipc.logger.warning") as mock_logger: | ||
| result = ipc.load() | ||
|
|
||
| assert result == "type_error_fallback" | ||
| mock_logger.assert_called_once() | ||
| assert "KindNotImplementedException" in str( | ||
| mock_logger.call_args | ||
| ) | ||
|
|
||
| def test_load_attribute_error_during_unpickling_returns_default(self) -> None: | ||
| """Test that AttributeError during unpickling returns default value.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with patch("port_ocean.utils.ipc.os.makedirs"): | ||
| ipc = FileIPC( | ||
| "test_process", "test_name", default_return="attr_error_fallback" | ||
| ) | ||
| ipc.file_path = f"{temp_dir}/attr_error.pkl" | ||
|
|
||
| # Create a dummy file so existence check passes | ||
| with open(ipc.file_path, "wb") as f: | ||
| f.write(b"dummy content") | ||
|
|
||
| # Mock pickle.load to raise AttributeError | ||
| with patch( | ||
| "pickle.load", | ||
| side_effect=AttributeError( | ||
| "module 'some_module' has no attribute 'SomeClass'" | ||
| ), | ||
| ): | ||
| with patch("port_ocean.utils.ipc.logger.warning") as mock_logger: | ||
| result = ipc.load() | ||
|
|
||
| assert result == "attr_error_fallback" | ||
| mock_logger.assert_called_once() | ||
|
|
||
| def test_load_import_error_during_unpickling_returns_default(self) -> None: | ||
| """Test that ImportError during unpickling returns default value.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with patch("port_ocean.utils.ipc.os.makedirs"): | ||
| ipc = FileIPC( | ||
| "test_process", "test_name", default_return="import_error_fallback" | ||
| ) | ||
| ipc.file_path = f"{temp_dir}/import_error.pkl" | ||
|
|
||
| # Create a dummy file so existence check passes | ||
| with open(ipc.file_path, "wb") as f: | ||
| f.write(b"dummy content") | ||
|
|
||
| # Mock pickle.load to raise ImportError | ||
| with patch( | ||
| "pickle.load", | ||
| side_effect=ImportError("No module named 'missing_module'"), | ||
| ): | ||
| with patch("port_ocean.utils.ipc.logger.warning") as mock_logger: | ||
| result = ipc.load() | ||
|
|
||
| assert result == "import_error_fallback" | ||
| mock_logger.assert_called_once() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import pickle | ||
| import os | ||
| from typing import Any | ||
| from loguru import logger | ||
|
|
||
|
|
||
| class FileIPC: | ||
|
|
@@ -22,8 +23,22 @@ def save(self, object: Any) -> None: | |
| def load(self) -> Any: | ||
| if not os.path.exists(self.file_path): | ||
| return self.default_return | ||
| with open(self.file_path, "rb") as f: | ||
| return pickle.load(f) | ||
|
|
||
| try: | ||
| with open(self.file_path, "rb") as f: | ||
| return pickle.load(f) | ||
| except ( | ||
| pickle.PickleError, | ||
| EOFError, | ||
| OSError, | ||
| TypeError, | ||
| AttributeError, | ||
| ImportError, | ||
| ) as e: | ||
| logger.warning( | ||
| f"Failed to load IPC data from {self.file_path}: {str(e)}. Returning default value." | ||
| ) | ||
| return self.default_return | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's raise a more indicative error and not return the self.default_return here |
||
|
|
||
| def delete(self) -> None: | ||
| if os.path.exists(self.file_path): | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||
| [tool.poetry] | ||||||
| name = "port-ocean" | ||||||
| version = "0.27.0" | ||||||
| version = "0.27.1" | ||||||
|
||||||
| version = "0.27.1" | |
| version = "0.25.4" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version number in CHANGELOG.md (0.27.1) is inconsistent with the version mentioned in the PR description (0.25.4). This version mismatch could cause confusion and should be aligned with the actual version being released.