|
1 | 1 | """Tests for the `labone.core.subscription` module.""" |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import logging |
| 5 | +from unittest.mock import MagicMock |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
7 | | -from labone.core import errors |
| 9 | +from labone.core import errors, hpk_schema |
8 | 10 | from labone.core.subscription import ( |
9 | 11 | CircularDataQueue, |
10 | 12 | DataQueue, |
@@ -257,6 +259,86 @@ def test_streaming_handle_with_parser_callback(): |
257 | 259 | ) |
258 | 260 |
|
259 | 261 |
|
| 262 | +@pytest.mark.asyncio |
| 263 | +async def test_capnp_callback(caplog): |
| 264 | + streaming_handle = StreamingHandle() |
| 265 | + queue = DataQueue( |
| 266 | + path="dummy", |
| 267 | + handle=streaming_handle, |
| 268 | + ) |
| 269 | + call_param = hpk_schema.StreamingHandleSendValuesParams() |
| 270 | + values = call_param.init_values(2) |
| 271 | + |
| 272 | + values[0].init_metadata(timestamp=0, path="dummy") |
| 273 | + values[0].init_value(int64=42) |
| 274 | + |
| 275 | + values[1].init_metadata(timestamp=1, path="dummy") |
| 276 | + values[1].init_value(double=22.0) |
| 277 | + |
| 278 | + fulfiller = MagicMock() |
| 279 | + with caplog.at_level(logging.ERROR): |
| 280 | + await streaming_handle.capnp_callback(0, 0, call_param, fulfiller) |
| 281 | + assert "" in caplog.text |
| 282 | + assert queue.qsize() == 2 |
| 283 | + assert queue.get_nowait() == AnnotatedValue(value=42, path="dummy", timestamp=0) |
| 284 | + assert queue.get_nowait() == AnnotatedValue(value=22.0, path="dummy", timestamp=1) |
| 285 | + fulfiller.fulfill.assert_called_once() |
| 286 | + |
| 287 | + |
| 288 | +@pytest.mark.asyncio |
| 289 | +async def test_streaming_error(caplog): |
| 290 | + streaming_handle = StreamingHandle() |
| 291 | + queue = DataQueue( |
| 292 | + path="dummy", |
| 293 | + handle=streaming_handle, |
| 294 | + ) |
| 295 | + call_param = hpk_schema.StreamingHandleSendValuesParams() |
| 296 | + values = call_param.init_values(1) |
| 297 | + values[0].init_metadata(timestamp=0, path="dummy") |
| 298 | + values[0].init_value().init_streamingError( |
| 299 | + code=1, |
| 300 | + message="test error", |
| 301 | + category="unknown", |
| 302 | + ) |
| 303 | + fulfiller = MagicMock() |
| 304 | + with caplog.at_level(logging.ERROR): |
| 305 | + await streaming_handle.capnp_callback(0, 0, call_param, fulfiller) |
| 306 | + assert "test error" in caplog.text |
| 307 | + assert queue.qsize() == 0 |
| 308 | + fulfiller.fulfill.assert_called_once() |
| 309 | + |
| 310 | + |
| 311 | +@pytest.mark.asyncio |
| 312 | +async def test_streaming_error_with_value(caplog): |
| 313 | + streaming_handle = StreamingHandle() |
| 314 | + queue = DataQueue( |
| 315 | + path="dummy", |
| 316 | + handle=streaming_handle, |
| 317 | + ) |
| 318 | + call_param = hpk_schema.StreamingHandleSendValuesParams() |
| 319 | + values = call_param.init_values(2) |
| 320 | + |
| 321 | + # Fist value is a streaming error |
| 322 | + values[0].init_metadata(timestamp=0, path="dummy") |
| 323 | + values[0].init_value().init_streamingError( |
| 324 | + code=1, |
| 325 | + message="test error", |
| 326 | + category="unknown", |
| 327 | + ) |
| 328 | + |
| 329 | + # Second value is a normal value |
| 330 | + values[1].init_metadata(timestamp=0, path="dummy") |
| 331 | + values[1].init_value(int64=42) |
| 332 | + |
| 333 | + fulfiller = MagicMock() |
| 334 | + with caplog.at_level(logging.ERROR): |
| 335 | + await streaming_handle.capnp_callback(0, 0, call_param, fulfiller) |
| 336 | + assert "test error" in caplog.text |
| 337 | + assert queue.qsize() == 1 |
| 338 | + assert queue.get_nowait() == AnnotatedValue(value=42, path="dummy", timestamp=0) |
| 339 | + fulfiller.fulfill.assert_called_once() |
| 340 | + |
| 341 | + |
260 | 342 | @pytest.mark.asyncio |
261 | 343 | async def test_distinct_data_queue_put_no_wait_new_value(): |
262 | 344 | subscription = FakeSubscription() |
|
0 commit comments