bec-lib 4.5.0__tar.gz → 4.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bec_lib-4.5.0 → bec_lib-4.6.0}/PKG-INFO +1 -1
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/redis_connector/managed_redis_connection.py +1 -1
- {bec_lib-4.5.0 → bec_lib-4.6.0}/pyproject.toml +1 -1
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_hli_connector_fakeredis.py +2 -3
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_hli_redis_connector.py +5 -20
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_managed_redis_connection.py +47 -63
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_messaging_service.py +31 -7
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_redis_connector_fakeredis.py +73 -69
- {bec_lib-4.5.0 → bec_lib-4.6.0}/.gitignore +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/README.md +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/_print_versions.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/acl_login.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/actors.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/alarm_handler.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/atlas_models.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/bec_errors.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/bec_service.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/bec_yaml_loader.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/bl_state_manager.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/bl_states.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/builtin_actor_hli.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/callback_handler.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/channel_monitor.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/client.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/codecs.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/config_helper.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/config_values.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/configs/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/configs/demo_config.yaml +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/configs/openapi_schema.json +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/connector.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/dap_plugin_objects.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/dap_plugins.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/device.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/devicemanager.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/endpoints.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/file_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/live_scan_data.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/lmfit_serializer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/logger.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/macro_update_handler.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/messages.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/messaging_hooks.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/messaging_services.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/metadata_schema.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/numpy_encoder.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/observer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/path_optimization.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/pdf_writer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/plugin_helper.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/position_generators.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/procedures/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/procedures/helper.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/procedures/hli.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/procedures/status.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/queue_items.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/redis_connector/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/redis_connector/constants.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/redis_connector/hli.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/redis_connector/streams.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/redis_connector/validation.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/request_context.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/request_items.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_args.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_data_container.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_history.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_input_validator.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_items.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_manager.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_number_container.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_preview.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_repeat.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scan_report.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scans.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/scibec_validator.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/script_executor.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/serialization.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/serialization_registry.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/service_config.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/signature_serializer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/tests/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/tests/fixtures.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/tests/test_config.yaml +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/tests/test_service_config.yaml +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/tests/utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/user_macros.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/copier_jinja_filters.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/deprecation.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/import_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/json_extended.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/_constants.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/_util.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/create/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/create/device.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/create/scan.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/create/templates/scan.py.jinja +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/plugin_manager/main.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/proxy.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/pydantic_pretty_print.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/rpc_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/scan_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/threading_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/bec_lib/utils/user_acls_test.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/__init__.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/_additional_for_signature_serializer_test.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/conftest.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_acl_login.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_alarm_handler.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_atlas_models.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_beamline_states.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_bec_logger.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_bec_messages.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_bec_service.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_callback_handler.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_channel_monitor.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_client.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_config_helper.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_core_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_dap_plugins.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_data_container.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_deprecation.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_device_hashing.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_device_manager.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_devices.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_file_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_import_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_jinja_plugins.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_lmfit_serializer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_managed_config_values.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_messaging_hooks.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_metadata_schema.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_observer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_path_optimization.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_pdf_writer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_plugin_helper.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_plugin_manager.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_plugin_system.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_position_generators.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_procedure_submission.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_pydantic_pretty_print.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_queue_items.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_roi_endpoints.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_roi_messages.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_context.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_data.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_history.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_items.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_manager.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_object.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_preview.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_repeat.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scan_report.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_scibec_validator.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_script_executor.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_serializer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_service_config.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_signature_serializer.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_signature_serializer_with_future_import.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_stream_subs.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_user_macros.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_utils.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/tests/test_yaml_loader.py +0 -0
- {bec_lib-4.5.0 → bec_lib-4.6.0}/util_scripts/init_config.py +0 -0
|
@@ -716,7 +716,7 @@ class ManagedRedisConnection:
|
|
|
716
716
|
except redis.exceptions.ConnectionError:
|
|
717
717
|
if not error:
|
|
718
718
|
error = True
|
|
719
|
-
|
|
719
|
+
logger.error(self.connection_error_str)
|
|
720
720
|
self._stop_events_listener_thread.wait(timeout=1)
|
|
721
721
|
# pylint: disable=broad-except
|
|
722
722
|
except Exception:
|
|
@@ -12,7 +12,6 @@ from bec_lib.redis_connector.constants import (
|
|
|
12
12
|
IncompatibleMessageForEndpoint,
|
|
13
13
|
IncompatibleRedisOperation,
|
|
14
14
|
)
|
|
15
|
-
from bec_lib.redis_connector.managed_redis_connection import ManagedRedisConnection
|
|
16
15
|
from bec_lib.serialization import MsgpackSerialization
|
|
17
16
|
|
|
18
17
|
from .test_managed_redis_connection import TestMessage
|
|
@@ -34,9 +33,9 @@ def fake_redis_server(host, port, **kwargs):
|
|
|
34
33
|
|
|
35
34
|
|
|
36
35
|
@pytest.fixture
|
|
37
|
-
def connected_connector():
|
|
38
|
-
ManagedRedisConnection.RETRY_ON_TIMEOUT = 0
|
|
36
|
+
def connected_connector(monkeypatch):
|
|
39
37
|
connector = RedisConnector("localhost:1", redis_cls=fake_redis_server)
|
|
38
|
+
monkeypatch.setattr(connector._managed_connection, "RETRY_ON_TIMEOUT", 0)
|
|
40
39
|
connector._managed_connection.flushall()
|
|
41
40
|
try:
|
|
42
41
|
yield connector
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
from typing import Any, ClassVar, Optional
|
|
2
1
|
from unittest import mock
|
|
3
2
|
|
|
4
3
|
import pytest
|
|
5
4
|
|
|
6
|
-
import bec_lib.messages as bec_messages
|
|
7
5
|
from bec_lib import messages
|
|
8
6
|
from bec_lib.alarm_handler import Alarms
|
|
9
|
-
from bec_lib.endpoints import
|
|
10
|
-
from bec_lib.messages import AlarmMessage
|
|
7
|
+
from bec_lib.endpoints import MessageEndpoints
|
|
8
|
+
from bec_lib.messages import AlarmMessage
|
|
11
9
|
from bec_lib.messaging_hooks import MessagingEvent
|
|
12
10
|
from bec_lib.redis_connector import IncompatibleRedisOperation, RedisConnector
|
|
13
|
-
from bec_lib.redis_connector.constants import WrongArguments
|
|
14
|
-
from bec_lib.redis_connector.validation import validate_endpoint
|
|
15
11
|
from bec_lib.serialization import MsgpackSerialization
|
|
16
12
|
|
|
17
13
|
# pylint: disable=protected-access
|
|
@@ -20,17 +16,6 @@ from bec_lib.serialization import MsgpackSerialization
|
|
|
20
16
|
# pylint: disable=redefined-outer-name
|
|
21
17
|
|
|
22
18
|
|
|
23
|
-
class TestMessage(BECMessage):
|
|
24
|
-
__test__: ClassVar[bool] = False # just for pytest to ignore this class
|
|
25
|
-
msg_type: ClassVar[str] = "test_message"
|
|
26
|
-
msg: Optional[str] = None
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
# register at BEC messages module level, to be able to
|
|
30
|
-
# find it when using "loads()"
|
|
31
|
-
bec_messages.TestMessage = TestMessage
|
|
32
|
-
|
|
33
|
-
|
|
34
19
|
@pytest.fixture
|
|
35
20
|
def hli_connector():
|
|
36
21
|
_connector = RedisConnector("localhost:1", redis_cls=mock.MagicMock)
|
|
@@ -110,7 +95,7 @@ def test_redis_connector_raise_alarm(
|
|
|
110
95
|
|
|
111
96
|
def test_redis_connector_send_converts_ep(hli_connector: RedisConnector):
|
|
112
97
|
topic = MessageEndpoints.scan_segment()
|
|
113
|
-
msg =
|
|
98
|
+
msg = messages.ScanMessage(point_id=1, scan_id="scan_id", data={})
|
|
114
99
|
hli_connector.send(topic, msg)
|
|
115
100
|
hli_connector._redis_conn.publish.assert_called_once_with(
|
|
116
101
|
topic.endpoint, MsgpackSerialization.dumps(msg)
|
|
@@ -131,12 +116,12 @@ def test_redis_connector_keys(hli_connector, pattern):
|
|
|
131
116
|
|
|
132
117
|
|
|
133
118
|
def test_send_raises_on_invalid_message_type(hli_connector):
|
|
134
|
-
correct_msg =
|
|
119
|
+
correct_msg = messages.DeviceMessage(
|
|
135
120
|
signals={"samx": {"value": 1, "timestamp": 1}}, metadata={}
|
|
136
121
|
)
|
|
137
122
|
hli_connector.set_and_publish(MessageEndpoints.device_read("samx"), correct_msg)
|
|
138
123
|
with pytest.raises(TypeError) as excinfo:
|
|
139
|
-
msg =
|
|
124
|
+
msg = messages.ScanMessage(point_id=1, scan_id="scan_id", data={}, metadata={})
|
|
140
125
|
hli_connector.set_and_publish(MessageEndpoints.device_read("samx"), msg)
|
|
141
126
|
assert "Message type <class 'bec_lib.messages.ScanMessage'> is not compatible " in str(
|
|
142
127
|
excinfo.value
|
|
@@ -10,11 +10,11 @@ from louie.saferef import safe_ref
|
|
|
10
10
|
from redis import Redis
|
|
11
11
|
from redis.exceptions import RedisError
|
|
12
12
|
|
|
13
|
-
import bec_lib.messages as bec_messages
|
|
14
13
|
from bec_lib import messages
|
|
15
14
|
from bec_lib.endpoints import EndpointInfo, MessageEndpoints
|
|
15
|
+
from bec_lib.logger import bec_logger
|
|
16
16
|
from bec_lib.messages import BECMessage, BECStatus, BundleMessage, ClientInfoMessage
|
|
17
|
-
from bec_lib.redis_connector import IncompatibleRedisOperation
|
|
17
|
+
from bec_lib.redis_connector import IncompatibleRedisOperation, managed_redis_connection
|
|
18
18
|
from bec_lib.redis_connector.constants import WrongArguments
|
|
19
19
|
from bec_lib.redis_connector.managed_redis_connection import ManagedRedisConnection
|
|
20
20
|
from bec_lib.redis_connector.streams import StreamSubInfo
|
|
@@ -33,9 +33,11 @@ class TestMessage(BECMessage):
|
|
|
33
33
|
msg: Optional[str] = None
|
|
34
34
|
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
@pytest.fixture
|
|
37
|
+
def connector_logger():
|
|
38
|
+
# Rebind the module's logger so other services can keep using the shared logger.
|
|
39
|
+
with mock.patch.object(managed_redis_connection, "logger") as logger:
|
|
40
|
+
yield logger
|
|
39
41
|
|
|
40
42
|
|
|
41
43
|
@pytest.fixture
|
|
@@ -68,7 +70,7 @@ def legacy_object_array_payload():
|
|
|
68
70
|
|
|
69
71
|
@pytest.mark.parametrize("from_start", [True, False])
|
|
70
72
|
def test_stream_listener_skips_rejected_object_arrays(
|
|
71
|
-
connector, legacy_object_array_payload, from_start
|
|
73
|
+
connector, connector_logger, legacy_object_array_payload, from_start
|
|
72
74
|
):
|
|
73
75
|
topic = MessageEndpoints.processed_data("legacy").endpoint
|
|
74
76
|
other_topic = MessageEndpoints.processed_data("valid").endpoint
|
|
@@ -91,11 +93,8 @@ def test_stream_listener_skips_rejected_object_arrays(
|
|
|
91
93
|
connector._redis_conn.xread.return_value = [] if from_start else response
|
|
92
94
|
connector._redis_conn.xrange.side_effect = lambda topic, *_: dict(response)[topic.encode()]
|
|
93
95
|
|
|
94
|
-
with (
|
|
95
|
-
|
|
96
|
-
connector._stop_stream_events_listener_thread, "is_set", side_effect=[False, True]
|
|
97
|
-
),
|
|
98
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.error") as log_error,
|
|
96
|
+
with mock.patch.object(
|
|
97
|
+
connector._stop_stream_events_listener_thread, "is_set", side_effect=[False, True]
|
|
99
98
|
):
|
|
100
99
|
connector._get_stream_messages_loop()
|
|
101
100
|
|
|
@@ -106,11 +105,11 @@ def test_stream_listener_skips_rejected_object_arrays(
|
|
|
106
105
|
message = connector._message_callbacks_queue.get_nowait()
|
|
107
106
|
assert message.msg == {"data": valid}
|
|
108
107
|
assert list(message.callbacks) == [(subscription.cb_ref, {})]
|
|
109
|
-
assert
|
|
108
|
+
assert connector_logger.error.call_count == 2
|
|
110
109
|
|
|
111
110
|
|
|
112
111
|
def test_direct_stream_listener_skips_rejected_object_arrays(
|
|
113
|
-
connector, legacy_object_array_payload
|
|
112
|
+
connector, connector_logger, legacy_object_array_payload
|
|
114
113
|
):
|
|
115
114
|
topic = MessageEndpoints.processed_data("legacy").endpoint
|
|
116
115
|
valid = messages.RawMessage(data="valid")
|
|
@@ -121,8 +120,7 @@ def test_direct_stream_listener_skips_rejected_object_arrays(
|
|
|
121
120
|
stop_event = mock.Mock()
|
|
122
121
|
stop_event.is_set.side_effect = [False, False, True]
|
|
123
122
|
callback = mock.Mock()
|
|
124
|
-
|
|
125
|
-
connector._direct_stream_listener(topic, stop_event, callback, {})
|
|
123
|
+
connector._direct_stream_listener(topic, stop_event, callback, {})
|
|
126
124
|
|
|
127
125
|
assert connector._redis_conn.xrevrange.call_args_list == [
|
|
128
126
|
mock.call(topic, "+", "-", count=1),
|
|
@@ -132,7 +130,7 @@ def test_direct_stream_listener_skips_rejected_object_arrays(
|
|
|
132
130
|
message = connector._message_callbacks_queue.get_nowait()
|
|
133
131
|
assert message.msg == {"data": valid}
|
|
134
132
|
assert list(message.callbacks) == [(callback, {})]
|
|
135
|
-
|
|
133
|
+
connector_logger.error.assert_called_once()
|
|
136
134
|
|
|
137
135
|
|
|
138
136
|
def test_redis_connector_send_client_info(connector: ManagedRedisConnection):
|
|
@@ -525,7 +523,7 @@ def test_blocking_list_pop(connector: ManagedRedisConnection):
|
|
|
525
523
|
),
|
|
526
524
|
)
|
|
527
525
|
def test_user_pw_restored_on_auth_fail(connector: ManagedRedisConnection, test_dict):
|
|
528
|
-
connector._redis_conn.connection_pool.connection_kwargs = test_dict
|
|
526
|
+
connector._redis_conn.connection_pool.connection_kwargs = test_dict.copy()
|
|
529
527
|
connector._redis_conn.auth.side_effect = RedisError
|
|
530
528
|
with pytest.raises(RedisError):
|
|
531
529
|
connector.authenticate(username="user", password="pass")
|
|
@@ -543,7 +541,9 @@ def test_try_read_streams_returns_error_message_for_no_permission(
|
|
|
543
541
|
assert error == "Permission denied for stream topics: {'topic'}"
|
|
544
542
|
|
|
545
543
|
|
|
546
|
-
def test_stream_connection_logs_once_and_logs_recovery(
|
|
544
|
+
def test_stream_connection_logs_once_and_logs_recovery(
|
|
545
|
+
connector: ManagedRedisConnection, connector_logger
|
|
546
|
+
):
|
|
547
547
|
stream_subs = mock.MagicMock()
|
|
548
548
|
stream_subs.lock = contextlib.nullcontext()
|
|
549
549
|
stream_subs.gc_cb_refs = mock.MagicMock()
|
|
@@ -564,20 +564,18 @@ def test_stream_connection_logs_once_and_logs_recovery(connector: ManagedRedisCo
|
|
|
564
564
|
[],
|
|
565
565
|
]
|
|
566
566
|
|
|
567
|
-
|
|
568
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.error") as log_error,
|
|
569
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.info") as log_info,
|
|
570
|
-
):
|
|
571
|
-
connector._get_stream_messages_loop()
|
|
567
|
+
connector._get_stream_messages_loop()
|
|
572
568
|
|
|
573
|
-
|
|
574
|
-
|
|
569
|
+
connector_logger.error.assert_called_once_with(connector.connection_error_str)
|
|
570
|
+
connector_logger.info.assert_called_once_with(
|
|
575
571
|
f"{connector.name} reconnected to redis ({connector.host}:{connector.port})."
|
|
576
572
|
)
|
|
577
573
|
assert connector._stop_stream_events_listener_thread.wait.call_count == 2
|
|
578
574
|
|
|
579
575
|
|
|
580
|
-
def test_stream_connection_logs_permission_error_and_recovery(
|
|
576
|
+
def test_stream_connection_logs_permission_error_and_recovery(
|
|
577
|
+
connector: ManagedRedisConnection, connector_logger
|
|
578
|
+
):
|
|
581
579
|
stream_subs = mock.MagicMock()
|
|
582
580
|
stream_subs.lock = contextlib.nullcontext()
|
|
583
581
|
stream_subs.gc_cb_refs = mock.MagicMock()
|
|
@@ -592,14 +590,10 @@ def test_stream_connection_logs_permission_error_and_recovery(connector: Managed
|
|
|
592
590
|
connector._stop_stream_events_listener_thread.wait = mock.MagicMock()
|
|
593
591
|
connector._redis_conn.xread.side_effect = [redis.exceptions.NoPermissionError("denied"), []]
|
|
594
592
|
|
|
595
|
-
|
|
596
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.error") as log_error,
|
|
597
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.info") as log_info,
|
|
598
|
-
):
|
|
599
|
-
connector._get_stream_messages_loop()
|
|
593
|
+
connector._get_stream_messages_loop()
|
|
600
594
|
|
|
601
|
-
|
|
602
|
-
|
|
595
|
+
connector_logger.error.assert_called_once_with("Permission denied for stream topics: {'topic'}")
|
|
596
|
+
connector_logger.info.assert_called_once_with(
|
|
603
597
|
f"{connector.name} reconnected to redis ({connector.host}:{connector.port})."
|
|
604
598
|
)
|
|
605
599
|
assert connector._stop_stream_events_listener_thread.wait.call_count == 1
|
|
@@ -609,7 +603,7 @@ def test_stream_connection_logs_permission_error_and_recovery(connector: Managed
|
|
|
609
603
|
"error_type", [redis.exceptions.NoPermissionError, redis.exceptions.ConnectionError]
|
|
610
604
|
)
|
|
611
605
|
def test_stream_connection_logs_errors_from_both_read_paths(
|
|
612
|
-
connector: ManagedRedisConnection, error_type
|
|
606
|
+
connector: ManagedRedisConnection, connector_logger, error_type
|
|
613
607
|
):
|
|
614
608
|
def callback():
|
|
615
609
|
pass
|
|
@@ -619,21 +613,17 @@ def test_stream_connection_logs_errors_from_both_read_paths(
|
|
|
619
613
|
connector._redis_conn.xrange.side_effect = [error_type("denied"), error_type("denied"), []]
|
|
620
614
|
connector._redis_conn.xread.side_effect = [error_type("denied"), error_type("denied"), [], []]
|
|
621
615
|
|
|
622
|
-
with (
|
|
623
|
-
mock.patch.object(connector, "_stop_stream_events_listener_thread") as stop_event,
|
|
624
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.error") as log_error,
|
|
625
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.info") as log_info,
|
|
626
|
-
):
|
|
616
|
+
with mock.patch.object(connector, "_stop_stream_events_listener_thread") as stop_event:
|
|
627
617
|
stop_event.is_set.side_effect = [False, False, False, False, True]
|
|
628
618
|
connector._get_stream_messages_loop()
|
|
629
619
|
|
|
630
620
|
if error_type is redis.exceptions.NoPermissionError:
|
|
631
|
-
assert
|
|
632
|
-
|
|
633
|
-
|
|
621
|
+
assert connector_logger.error.call_count == 2
|
|
622
|
+
connector_logger.error.assert_any_call("Permission denied for stream topics: {'history'}")
|
|
623
|
+
connector_logger.error.assert_any_call("Permission denied for stream topics: {'live'}")
|
|
634
624
|
else:
|
|
635
|
-
|
|
636
|
-
|
|
625
|
+
connector_logger.error.assert_called_once_with(connector.connection_error_str)
|
|
626
|
+
connector_logger.info.assert_called_once_with(
|
|
637
627
|
f"{connector.name} reconnected to redis ({connector.host}:{connector.port})."
|
|
638
628
|
)
|
|
639
629
|
assert stop_event.wait.call_count == 2
|
|
@@ -644,7 +634,7 @@ def test_stream_connection_logs_errors_from_both_read_paths(
|
|
|
644
634
|
@pytest.mark.parametrize("from_start", [False, True])
|
|
645
635
|
@pytest.mark.parametrize("remove_subscription", ["unregister", "gc"])
|
|
646
636
|
def test_stream_connection_waits_for_read_before_logging_recovery(
|
|
647
|
-
connector: ManagedRedisConnection, from_start, remove_subscription
|
|
637
|
+
connector: ManagedRedisConnection, connector_logger, from_start, remove_subscription
|
|
648
638
|
):
|
|
649
639
|
def callback():
|
|
650
640
|
pass
|
|
@@ -670,30 +660,30 @@ def test_stream_connection_waits_for_read_before_logging_recovery(
|
|
|
670
660
|
else:
|
|
671
661
|
callback = None
|
|
672
662
|
elif iteration == 3:
|
|
673
|
-
|
|
663
|
+
# Other BEC components can log while the stream is still disconnected.
|
|
664
|
+
bec_logger.logger.info("Unrelated shutdown message")
|
|
665
|
+
connector_logger.info.assert_not_called()
|
|
674
666
|
assert not connector._stream_subs.all_topics
|
|
675
667
|
connector._stream_subs.add(
|
|
676
668
|
False, "0-0", "topic", StreamSubInfo(safe_ref(replacement_callback), {})
|
|
677
669
|
)
|
|
678
670
|
return iteration == 5
|
|
679
671
|
|
|
680
|
-
with (
|
|
681
|
-
mock.patch.object(connector, "_stop_stream_events_listener_thread") as stop_event,
|
|
682
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.error") as log_error,
|
|
683
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.info") as log_info,
|
|
684
|
-
):
|
|
672
|
+
with mock.patch.object(connector, "_stop_stream_events_listener_thread") as stop_event:
|
|
685
673
|
stop_event.is_set.side_effect = advance_loop
|
|
686
674
|
connector._get_stream_messages_loop()
|
|
687
675
|
|
|
688
|
-
|
|
689
|
-
|
|
676
|
+
connector_logger.error.assert_called_once_with(connector.connection_error_str)
|
|
677
|
+
connector_logger.info.assert_called_once_with(
|
|
690
678
|
f"{connector.name} reconnected to redis ({connector.host}:{connector.port})."
|
|
691
679
|
)
|
|
692
680
|
assert connector._redis_conn.xrange.call_count == int(from_start)
|
|
693
681
|
assert connector._redis_conn.xread.call_count == (2 if from_start else 3)
|
|
694
682
|
|
|
695
683
|
|
|
696
|
-
def test_pubsub_connection_logs_once_and_logs_recovery(
|
|
684
|
+
def test_pubsub_connection_logs_once_and_logs_recovery(
|
|
685
|
+
connector: ManagedRedisConnection, connector_logger
|
|
686
|
+
):
|
|
697
687
|
connector._garbage_collect_cb_refs = mock.MagicMock()
|
|
698
688
|
connector._stop_events_listener_thread = mock.MagicMock()
|
|
699
689
|
connector._stop_events_listener_thread.is_set.side_effect = [False, False, False, True]
|
|
@@ -704,16 +694,10 @@ def test_pubsub_connection_logs_once_and_logs_recovery(connector: ManagedRedisCo
|
|
|
704
694
|
None,
|
|
705
695
|
]
|
|
706
696
|
|
|
707
|
-
|
|
708
|
-
mock.patch(
|
|
709
|
-
"bec_lib.redis_connector.managed_redis_connection.bec_logger.logger.error"
|
|
710
|
-
) as log_error,
|
|
711
|
-
mock.patch("bec_lib.redis_connector.managed_redis_connection.logger.info") as log_info,
|
|
712
|
-
):
|
|
713
|
-
connector._get_messages_loop()
|
|
697
|
+
connector._get_messages_loop()
|
|
714
698
|
|
|
715
|
-
|
|
716
|
-
|
|
699
|
+
connector_logger.error.assert_called_once_with(connector.connection_error_str)
|
|
700
|
+
connector_logger.info.assert_called_once_with(
|
|
717
701
|
f"{connector.name} reconnected to redis ({connector.host}:{connector.port})."
|
|
718
702
|
)
|
|
719
703
|
assert connector._stop_events_listener_thread.wait.call_count == 2
|
|
@@ -979,8 +979,9 @@ def test_set_auto_notifications_persists_notification_config(scilog_service, con
|
|
|
979
979
|
|
|
980
980
|
|
|
981
981
|
def test_set_auto_notifications_merges_with_existing_routes(scilog_service, connected_connector):
|
|
982
|
-
|
|
983
|
-
|
|
982
|
+
# Seed Redis without queuing an older config update on the subscriber thread.
|
|
983
|
+
connected_connector._managed_connection.set( # pylint: disable=protected-access
|
|
984
|
+
MessageEndpoints.notification_config().endpoint,
|
|
984
985
|
messages.NotificationConfigMessage(
|
|
985
986
|
routes={
|
|
986
987
|
"new_scan": [
|
|
@@ -1009,8 +1010,8 @@ def test_set_auto_notifications_merges_with_existing_routes(scilog_service, conn
|
|
|
1009
1010
|
def test_set_auto_notifications_disable_removes_only_matching_service_scope(
|
|
1010
1011
|
scilog_service, connected_connector
|
|
1011
1012
|
):
|
|
1012
|
-
connected_connector.
|
|
1013
|
-
MessageEndpoints.notification_config(),
|
|
1013
|
+
connected_connector._managed_connection.set( # pylint: disable=protected-access
|
|
1014
|
+
MessageEndpoints.notification_config().endpoint,
|
|
1014
1015
|
messages.NotificationConfigMessage(
|
|
1015
1016
|
routes={
|
|
1016
1017
|
"new_scan": [
|
|
@@ -1031,7 +1032,30 @@ def test_set_auto_notifications_disable_removes_only_matching_service_scope(
|
|
|
1031
1032
|
]
|
|
1032
1033
|
}
|
|
1033
1034
|
)
|
|
1034
|
-
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
def test_set_auto_notifications_disable_removes_cached_scope(scilog_service):
|
|
1038
|
+
scilog_service._on_notification_config_change_msg( # pylint: disable=protected-access
|
|
1039
|
+
{
|
|
1040
|
+
"data": messages.NotificationConfigMessage(
|
|
1041
|
+
routes={
|
|
1042
|
+
"new_scan": [
|
|
1043
|
+
messages.NotificationServiceTarget(
|
|
1044
|
+
service_name="scilog", scope=["default", "secondary"]
|
|
1045
|
+
)
|
|
1046
|
+
]
|
|
1047
|
+
}
|
|
1048
|
+
)
|
|
1049
|
+
}
|
|
1050
|
+
)
|
|
1051
|
+
|
|
1052
|
+
# Check the synchronous cache update without an asynchronous notification overwriting it.
|
|
1053
|
+
with mock.patch.object(scilog_service, "_sync_auto_notifications_config"):
|
|
1054
|
+
scilog_service.set_auto_notifications(MessagingEvent.SCAN, enabled=False, scopes="default")
|
|
1055
|
+
|
|
1056
|
+
assert scilog_service._auto_notifications == { # pylint: disable=protected-access
|
|
1057
|
+
"new_scan": ["secondary"]
|
|
1058
|
+
}
|
|
1035
1059
|
|
|
1036
1060
|
|
|
1037
1061
|
def test_set_auto_notifications_uses_default_scope_when_scopes_omitted(
|
|
@@ -1087,8 +1111,8 @@ def test_messaging_service_tracks_external_notification_config_updates(
|
|
|
1087
1111
|
|
|
1088
1112
|
|
|
1089
1113
|
def test_messaging_service_loads_notification_config_on_init(connected_connector):
|
|
1090
|
-
connected_connector.
|
|
1091
|
-
MessageEndpoints.notification_config(),
|
|
1114
|
+
connected_connector._managed_connection.set( # pylint: disable=protected-access
|
|
1115
|
+
MessageEndpoints.notification_config().endpoint,
|
|
1092
1116
|
messages.NotificationConfigMessage(
|
|
1093
1117
|
routes={
|
|
1094
1118
|
"new_scan": [
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import gc
|
|
2
|
+
import queue
|
|
2
3
|
import threading
|
|
3
|
-
import time
|
|
4
4
|
from typing import Generator
|
|
5
5
|
from unittest import mock
|
|
6
6
|
|
|
@@ -9,7 +9,6 @@ import pytest
|
|
|
9
9
|
import redis
|
|
10
10
|
from redis.client import Pipeline
|
|
11
11
|
|
|
12
|
-
import bec_lib.messages as bec_messages
|
|
13
12
|
from bec_lib import messages
|
|
14
13
|
from bec_lib.endpoints import EndpointInfo, EndpointType, MessageEndpoints, MessageOp
|
|
15
14
|
from bec_lib.messages import ProcedureExecutionMessage
|
|
@@ -40,9 +39,10 @@ def fake_redis_server(host, port, **kwargs):
|
|
|
40
39
|
|
|
41
40
|
|
|
42
41
|
@pytest.fixture
|
|
43
|
-
def connected_connector():
|
|
44
|
-
|
|
42
|
+
def connected_connector(monkeypatch):
|
|
43
|
+
monkeypatch.setattr(messages, "TestMessage", TestMessage, raising=False)
|
|
45
44
|
connector = ManagedRedisConnection("localhost:1", redis_cls=fake_redis_server)
|
|
45
|
+
monkeypatch.setattr(connector, "RETRY_ON_TIMEOUT", 0)
|
|
46
46
|
connector.flushall()
|
|
47
47
|
try:
|
|
48
48
|
yield connector
|
|
@@ -235,11 +235,10 @@ def test_redis_connector_unregister_cb_not_topic(connected_connector):
|
|
|
235
235
|
|
|
236
236
|
|
|
237
237
|
def test_redis_connector_unregister_topic_keeps_others_alive(connected_connector):
|
|
238
|
-
def send_msgs_and_poll(
|
|
238
|
+
def send_msgs_and_poll():
|
|
239
239
|
connector.send(topic1.endpoint, TestMessage())
|
|
240
240
|
connector.send(topic2.endpoint, TestMessage())
|
|
241
|
-
|
|
242
|
-
connector.poll_messages(timeout=timeout)
|
|
241
|
+
connector.poll_messages(timeout=1)
|
|
243
242
|
|
|
244
243
|
connector = connected_connector
|
|
245
244
|
|
|
@@ -266,7 +265,7 @@ def test_redis_connector_unregister_topic_keeps_others_alive(connected_connector
|
|
|
266
265
|
# unregistering the last callback for a topic should remove the topic from the list
|
|
267
266
|
connector.unregister(topic2, cb=received_event2)
|
|
268
267
|
with pytest.raises(TimeoutError):
|
|
269
|
-
send_msgs_and_poll(
|
|
268
|
+
send_msgs_and_poll()
|
|
270
269
|
assert received_event1.call_count == 1
|
|
271
270
|
assert received_event2.call_count == 2
|
|
272
271
|
|
|
@@ -441,16 +440,18 @@ def test_redis_connector_stream_cb_swapped_during_blocking_read(connected_connec
|
|
|
441
440
|
def blocking_read(topics_ids, from_start=False):
|
|
442
441
|
if not from_start:
|
|
443
442
|
in_read.set()
|
|
444
|
-
proceed.wait(
|
|
443
|
+
proceed.wait()
|
|
445
444
|
return orig_read(topics_ids, from_start)
|
|
446
445
|
|
|
447
446
|
with mock.patch.object(connector, "_try_read_streams", blocking_read):
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
447
|
+
try:
|
|
448
|
+
connector.register(TestStreamEndpoint, cb=cb_mock1, start_thread=False)
|
|
449
|
+
assert in_read.wait(timeout=2)
|
|
450
|
+
connector.unregister("test", cb=cb_mock1)
|
|
451
|
+
connector.register(TestStreamEndpoint, cb=cb_mock2, start_thread=False)
|
|
452
|
+
connector.xadd("test", {"data": 1})
|
|
453
|
+
finally:
|
|
454
|
+
proceed.set()
|
|
454
455
|
# raises TimeoutError if the message was dropped instead of delivered to cb_mock2
|
|
455
456
|
connector.poll_messages(timeout=2)
|
|
456
457
|
cb_mock1.assert_not_called()
|
|
@@ -549,31 +550,36 @@ def test_redis_connector_register_stream_from_start(connected_connector):
|
|
|
549
550
|
def test_redis_connector_register_stream_newest_only(connected_connector):
|
|
550
551
|
connector = connected_connector
|
|
551
552
|
endpoint = TestStreamEndpoint
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
#
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
553
|
+
received = queue.Queue()
|
|
554
|
+
burst_published = threading.Event()
|
|
555
|
+
read = connector._redis_conn.xrevrange
|
|
556
|
+
|
|
557
|
+
def read_after_burst(topic, max_id, min_id, count):
|
|
558
|
+
if min_id != "-":
|
|
559
|
+
burst_published.wait()
|
|
560
|
+
return read(topic, max_id, min_id, count)
|
|
561
|
+
|
|
562
|
+
def callback(message):
|
|
563
|
+
received.put(message)
|
|
564
|
+
|
|
565
|
+
# Gate the reader itself. Callbacks run on a separate dispatcher thread.
|
|
566
|
+
with mock.patch.object(connector._redis_conn, "xrevrange", side_effect=read_after_burst):
|
|
567
|
+
try:
|
|
568
|
+
connector.register(endpoint, cb=callback, newest_only=True)
|
|
569
|
+
listener = next(
|
|
570
|
+
iter(connector._stream_subs._direct_read_subs[endpoint.endpoint])
|
|
571
|
+
).thread
|
|
572
|
+
connector.xadd(endpoint.endpoint, {"data": 0})
|
|
573
|
+
assert received.get(timeout=2) == {"data": 0}
|
|
574
|
+
|
|
575
|
+
for value in range(1, 4):
|
|
576
|
+
connector.xadd(endpoint.endpoint, {"data": value})
|
|
577
|
+
finally:
|
|
578
|
+
burst_published.set()
|
|
579
|
+
|
|
580
|
+
assert received.get(timeout=2) == {"data": 3}
|
|
581
|
+
connector.unregister(endpoint, cb=callback)
|
|
582
|
+
assert not listener.is_alive()
|
|
577
583
|
|
|
578
584
|
|
|
579
585
|
@pytest.mark.timeout(5)
|
|
@@ -581,27 +587,27 @@ def test_redis_connector_register_stream_newest_only_multiple_endpoints_same_cb(
|
|
|
581
587
|
connected_connector,
|
|
582
588
|
):
|
|
583
589
|
connector = connected_connector
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
connector.register(endpoint, cb=cb_mock, newest_only=True, a=1)
|
|
587
|
-
time.sleep(0.1)
|
|
588
|
-
for ep in endpoint:
|
|
589
|
-
connector.xadd(ep.endpoint, {"data": 1})
|
|
590
|
-
while cb_mock.call_count < 2:
|
|
591
|
-
time.sleep(0.1)
|
|
592
|
-
assert mock.call({"data": 1}, a=1) in cb_mock.mock_calls
|
|
593
|
-
cb_mock.reset_mock()
|
|
590
|
+
endpoints = [TestStreamEndpoint, TestStreamEndpoint2]
|
|
591
|
+
received = queue.Queue()
|
|
594
592
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
593
|
+
def callback(message, *, a):
|
|
594
|
+
received.put((message, a))
|
|
595
|
+
|
|
596
|
+
connector.register(endpoints, cb=callback, newest_only=True, a=1)
|
|
597
|
+
listeners = [
|
|
598
|
+
next(iter(connector._stream_subs._direct_read_subs[endpoint.endpoint])).thread
|
|
599
|
+
for endpoint in endpoints
|
|
600
|
+
]
|
|
601
|
+
|
|
602
|
+
for batch in (1, 2):
|
|
603
|
+
for endpoint in endpoints:
|
|
604
|
+
connector.xadd(endpoint.endpoint, {"data": (batch, endpoint.endpoint)})
|
|
605
|
+
assert {
|
|
606
|
+
tuple(message["data"]): a for message, a in (received.get(timeout=2) for _ in endpoints)
|
|
607
|
+
} == {(batch, endpoint.endpoint): 1 for endpoint in endpoints}
|
|
600
608
|
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
# each direct connection (newest_only) makes 1 thread
|
|
604
|
-
assert threading.active_count() == num_threads - len(endpoint)
|
|
609
|
+
connector.unregister(endpoints, cb=callback)
|
|
610
|
+
assert all(not listener.is_alive() for listener in listeners)
|
|
605
611
|
|
|
606
612
|
|
|
607
613
|
def test_register_raises_if_no_cb(connected_connector):
|
|
@@ -762,14 +768,14 @@ def test_stream_subs_garbage_collection(connected_connector):
|
|
|
762
768
|
connected_connector.register(TestStreamEndpoint, cb=sub3)
|
|
763
769
|
|
|
764
770
|
assert len(connected_connector._stream_subs._subs["test"].subs) == 3
|
|
765
|
-
connected_connector._stream_subs.
|
|
771
|
+
with connected_connector._stream_subs.lock:
|
|
772
|
+
connected_connector._stream_subs.gc_cb_refs()
|
|
766
773
|
assert len(connected_connector._stream_subs._subs["test"].subs) == 3
|
|
767
774
|
|
|
768
775
|
del sub2
|
|
769
|
-
time.sleep(0.05)
|
|
770
776
|
gc.collect()
|
|
771
|
-
|
|
772
|
-
|
|
777
|
+
with connected_connector._stream_subs.lock:
|
|
778
|
+
connected_connector._stream_subs.gc_cb_refs()
|
|
773
779
|
|
|
774
780
|
assert len(connected_connector._stream_subs._subs["test"].subs) == 2
|
|
775
781
|
|
|
@@ -784,10 +790,9 @@ def test_stream_subs_garbage_collection(connected_connector):
|
|
|
784
790
|
|
|
785
791
|
del sub4
|
|
786
792
|
del sub3
|
|
787
|
-
time.sleep(0.05)
|
|
788
793
|
gc.collect()
|
|
789
|
-
|
|
790
|
-
|
|
794
|
+
with connected_connector._stream_subs.lock:
|
|
795
|
+
connected_connector._stream_subs.gc_cb_refs()
|
|
791
796
|
|
|
792
797
|
assert len(connected_connector._stream_subs._subs["test"].subs) == 1
|
|
793
798
|
assert len(connected_connector._stream_subs.from_start_subs["test"]) == 1
|
|
@@ -802,10 +807,9 @@ def test_stream_subs_garbage_collection(connected_connector):
|
|
|
802
807
|
del sub1
|
|
803
808
|
del sub5
|
|
804
809
|
del sub6
|
|
805
|
-
time.sleep(0.05)
|
|
806
810
|
gc.collect()
|
|
807
|
-
|
|
808
|
-
|
|
811
|
+
with connected_connector._stream_subs.lock:
|
|
812
|
+
connected_connector._stream_subs.gc_cb_refs()
|
|
809
813
|
|
|
810
814
|
assert "test" not in connected_connector._stream_subs._subs
|
|
811
815
|
assert "test" not in connected_connector._stream_subs.from_start_subs
|
|
File without changes
|
|
File without changes
|