Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/crawlee/_service_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def __init__(
self._event_manager = event_manager
self._storage_client = storage_client

def reset(self) -> None:
"""Reset the service locator."""
self._configuration = None
self._event_manager = None
self._storage_client = None

def get_configuration(self) -> Configuration:
"""Get the configuration."""
if self._configuration is None:
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def _prepare_test_env() -> None:
monkeypatch.setenv('CRAWLEE_STORAGE_DIR', str(tmp_path))

# Reset the services in the service locator.
service_locator._configuration = None
service_locator._event_manager = None
service_locator._storage_client = None
service_locator.reset()
service_locator.storage_instance_manager.clear_cache()

# Verify that the test environment was set up correctly.
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_service_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ def test_storage_client_conflict() -> None:

with pytest.raises(ServiceConflictError, match=r'StorageClient is already in use.'):
service_locator.set_storage_client(custom_storage_client)


def test_storage_client_overwrite_possible_after_reset() -> None:
service_locator.set_event_manager(LocalEventManager())
service_locator.set_configuration(Configuration())
service_locator.set_storage_client(MemoryStorageClient())

service_locator.reset()

service_locator.set_event_manager(LocalEventManager())
service_locator.set_configuration(Configuration())
service_locator.set_storage_client(MemoryStorageClient())
Loading