Fix references to redis client in updater services #205
@@ -91,6 +91,7 @@ class BeamlineConfig:
|
||||
redis (redis.Redis): Redis client instance used for interacting with the datastore.
|
||||
"""
|
||||
|
||||
redis: Redis
|
||||
GUI_SESSION_EXPIRE_SECONDS = 60 * 10
|
||||
|
||||
def __init__(self, bl: MXBeamline):
|
||||
|
||||
@@ -30,7 +30,7 @@ def set_spreadsheet_in_redis(spreadsheet):
|
||||
"[REDIS][DEBUG] Data to write:", json.dumps(spreadsheet, indent=4)
|
||||
) # Pretty-print the data
|
||||
print("[REDIS][INFO] Writing spreadsheet to Redis...")
|
||||
config.__client.set(f"{config._bl}:spreadsheet", json.dumps(spreadsheet))
|
||||
config.redis.set(f"{config._bl}:spreadsheet", json.dumps(spreadsheet))
|
||||
|
||||
|
||||
def on_message(ws, message):
|
||||
@@ -93,19 +93,19 @@ def on_message(ws, message):
|
||||
# Write normal pucks to sample_spreadsheet
|
||||
normal_key = f"{config._bl}:sample_spreadsheet"
|
||||
normal_list = SampleShortInfoList(s=normal_short_infos)
|
||||
config._client.set(normal_key, normal_list.model_dump_json())
|
||||
config.redis.set(normal_key, normal_list.model_dump_json())
|
||||
print("[REDIS][INFO] Written normal spreadsheet to:", normal_key)
|
||||
|
||||
# Write reference tools to reference-tools
|
||||
ref_key = f"{config._bl}:reference-tools"
|
||||
if reference_short_infos:
|
||||
ref_list = SampleShortInfoList(s=reference_short_infos)
|
||||
config._client.set(ref_key, ref_list.model_dump_json())
|
||||
config.redis.set(ref_key, ref_list.model_dump_json())
|
||||
print("[REDIS][INFO] Written reference tools to:", ref_key)
|
||||
else:
|
||||
# Clear key if empty
|
||||
try:
|
||||
config._client.delete(ref_key)
|
||||
config.redis.delete(ref_key)
|
||||
print("[REDIS][INFO] Cleared reference tools key:", ref_key)
|
||||
except Exception:
|
||||
logger.debug("Could not clear the reference tools key", exc_info=True)
|
||||
|
||||
@@ -98,7 +98,7 @@ def _get_redis_context() -> tuple[Any | None, str | None]:
|
||||
logger.debug("[REDIS] BeamlineConfig unavailable; skipping TELL redis write")
|
||||
return None, None
|
||||
|
||||
redis_client = getattr(config, "_client", None)
|
||||
redis_client = getattr(config, "redis", None)
|
||||
beamline_key = getattr(config, "_bl", None)
|
||||
if redis_client is None or beamline_key is None:
|
||||
logger.error("[REDIS] BeamlineConfig internals unavailable; skipping TELL redis write")
|
||||
|
||||
@@ -11,10 +11,7 @@ from aare.daq.spreadsheetupdater import get_ws_headers, on_message, set_spreadsh
|
||||
def mock_config():
|
||||
with patch("aare.daq.spreadsheetupdater.config") as mock:
|
||||
mock._bl = "X10SA"
|
||||
mock._client = MagicMock()
|
||||
# Mocking private attributes access which the code uses
|
||||
mock._client = mock._client
|
||||
mock._bl = mock._bl
|
||||
mock.redis = MagicMock()
|
||||
yield mock
|
||||
|
||||
|
||||
@@ -33,8 +30,7 @@ def test_set_spreadsheet_in_redis(mock_config):
|
||||
data = {"test": "data"}
|
||||
with patch("aare.daq.spreadsheetupdater.config") as mock_cfg_internal:
|
||||
mock_client = MagicMock()
|
||||
mock_cfg_internal._client = mock_client
|
||||
mock_cfg_internal.client = mock_client
|
||||
mock_cfg_internal.redis = mock_client
|
||||
|
||||
set_spreadsheet_in_redis(data)
|
||||
|
||||
@@ -87,7 +83,7 @@ def test_on_message_success(mock_config):
|
||||
with patch("aare.daq.spreadsheetupdater.PuckWithTellPosition", side_effect=mock_pucks):
|
||||
on_message(None, message)
|
||||
|
||||
calls = mock_config._client.set.call_args_list
|
||||
calls = mock_config.redis.set.call_args_list
|
||||
written_keys = [call.args[0] for call in calls]
|
||||
|
||||
assert "X10SA:sample_spreadsheet" in written_keys
|
||||
@@ -118,9 +114,9 @@ def test_on_message_empty_ref(mock_config):
|
||||
on_message(None, message)
|
||||
|
||||
ref_key = "X10SA:reference-tools"
|
||||
mock_config._client.delete.assert_called_with(ref_key)
|
||||
mock_config.redis.delete.assert_called_with(ref_key)
|
||||
|
||||
|
||||
def test_on_message_invalid_json(mock_config):
|
||||
on_message(None, "invalid json")
|
||||
mock_config._client.set.assert_not_called()
|
||||
mock_config.redis.set.assert_not_called()
|
||||
|
||||
@@ -2,6 +2,7 @@ import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from aare.daq import tellupdater
|
||||
from aare.daq.config import BeamlineConfig
|
||||
|
||||
|
||||
def test_compare_and_report_change():
|
||||
@@ -107,14 +108,15 @@ def test_record_tell_event_writes_history_to_redis():
|
||||
tellupdater.latest_tell_events.clear()
|
||||
tellupdater.tell_event_history.clear()
|
||||
|
||||
mock_config = MagicMock()
|
||||
mock_config = MagicMock(spec=BeamlineConfig)
|
||||
mock_config.redis = MagicMock()
|
||||
mock_config._bl = "x10sa"
|
||||
|
||||
with patch("aare.daq.tellupdater.config", mock_config):
|
||||
tellupdater.record_tell_event("Motion Task", "dry")
|
||||
|
||||
mock_config._client.set.assert_called_once()
|
||||
redis_key, redis_value = mock_config._client.set.call_args.args
|
||||
mock_config.redis.set.assert_called_once()
|
||||
redis_key, redis_value = mock_config.redis.set.call_args.args
|
||||
assert redis_key == "x10sa:tell_events"
|
||||
|
||||
payload = json.loads(redis_value)
|
||||
@@ -135,7 +137,7 @@ def test_record_tell_event_keeps_last_25_events():
|
||||
for idx in range(30):
|
||||
tellupdater.record_tell_event("Motion Sync", f"event-{idx}")
|
||||
|
||||
redis_key, redis_value = mock_config._client.set.call_args.args
|
||||
redis_key, redis_value = mock_config.redis.set.call_args.args
|
||||
assert redis_key == "x10sa:tell_events"
|
||||
|
||||
payload = json.loads(redis_value)
|
||||
|
||||
Reference in New Issue
Block a user