88import json
99import logging
1010import re
11- from collections .abc import Callable , Coroutine , Iterable
11+ from collections .abc import AsyncIterator , Callable , Coroutine , Iterable
1212
1313# copied out from dask
1414from dataclasses import dataclass
2323from common_library .json_serialization import json_dumps
2424from dask_task_models_library .container_tasks .docker import DockerBasicAuth
2525from dask_task_models_library .container_tasks .errors import ServiceRuntimeError
26- from dask_task_models_library .container_tasks .events import (
27- TaskLogEvent ,
28- TaskProgressEvent ,
29- )
26+ from dask_task_models_library .container_tasks .events import TaskProgressEvent
3027from dask_task_models_library .container_tasks .io import (
3128 FileUrl ,
3229 TaskInputData ,
3936)
4037from faker import Faker
4138from models_library .basic_types import EnvVarKey
39+ from models_library .rabbitmq_messages import LoggerRabbitMessage
4240from models_library .services import ServiceMetaDataPublished
4341from models_library .services_resources import BootMode
4442from packaging import version
4543from pydantic import AnyUrl , SecretStr , TypeAdapter
4644from pytest_mock .plugin import MockerFixture
4745from pytest_simcore .helpers .typing_env import EnvVarsDict
46+ from servicelib .rabbitmq ._client import RabbitMQClient
4847from settings_library .s3 import S3Settings
4948from simcore_service_dask_sidecar .computational_sidecar .docker_utils import (
5049 LEGACY_SERVICE_LOG_FILE_NAME ,
@@ -466,6 +465,19 @@ def mocked_get_image_labels(
466465 )
467466
468467
468+ @pytest .fixture
469+ async def log_rabbit_client_parser (
470+ create_rabbitmq_client : Callable [[str ], RabbitMQClient ], mocker : MockerFixture
471+ ) -> AsyncIterator [mock .AsyncMock ]:
472+ client = create_rabbitmq_client ("dask_sidecar_pytest_logs_consumer" )
473+ mock = mocker .AsyncMock (return_value = True )
474+ queue_name , _ = await client .subscribe (
475+ LoggerRabbitMessage .get_channel_name (), mock , exclusive_queue = False
476+ )
477+ yield mock
478+ await client .unsubscribe (queue_name )
479+
480+
469481def test_run_computational_sidecar_real_fct (
470482 caplog_info_level : pytest .LogCaptureFixture ,
471483 event_loop : asyncio .AbstractEventLoop ,
@@ -474,6 +486,7 @@ def test_run_computational_sidecar_real_fct(
474486 sleeper_task : ServiceExampleParam ,
475487 mocked_get_image_labels : mock .Mock ,
476488 s3_settings : S3Settings ,
489+ log_rabbit_client_parser : mock .AsyncMock ,
477490):
478491 output_data = run_computational_sidecar (
479492 ** sleeper_task .sidecar_params (),
@@ -484,10 +497,11 @@ def test_run_computational_sidecar_real_fct(
484497 sleeper_task .service_key ,
485498 sleeper_task .service_version ,
486499 )
487- for event in [TaskProgressEvent , TaskLogEvent ]:
500+ for event in [TaskProgressEvent ]:
488501 dask_subsystem_mock ["dask_event_publish" ].assert_any_call (
489502 name = event .topic_name ()
490503 )
504+ log_rabbit_client_parser .assert_called_once ()
491505
492506 # check that the task produces expected logs
493507 for log in sleeper_task .expected_logs :
@@ -561,13 +575,6 @@ def test_run_multiple_computational_sidecar_dask(
561575 mocked_get_image_labels .assert_called ()
562576
563577
564- @pytest .fixture
565- def log_sub (
566- dask_client : distributed .Client ,
567- ) -> distributed .Sub :
568- return distributed .Sub (TaskLogEvent .topic_name (), client = dask_client )
569-
570-
571578@pytest .fixture
572579def progress_sub (dask_client : distributed .Client ) -> distributed .Sub :
573580 return distributed .Sub (TaskProgressEvent .topic_name (), client = dask_client )
@@ -579,10 +586,10 @@ def progress_sub(dask_client: distributed.Client) -> distributed.Sub:
579586async def test_run_computational_sidecar_dask (
580587 dask_client : distributed .Client ,
581588 sleeper_task : ServiceExampleParam ,
582- log_sub : distributed .Sub ,
583589 progress_sub : distributed .Sub ,
584590 mocked_get_image_labels : mock .Mock ,
585591 s3_settings : S3Settings ,
592+ log_rabbit_client_parser : mock .AsyncMock ,
586593):
587594 future = dask_client .submit (
588595 run_computational_sidecar ,
@@ -607,7 +614,9 @@ async def test_run_computational_sidecar_dask(
607614 ), "ordering of progress values incorrectly sorted!"
608615 assert worker_progresses [0 ] == 0 , "missing/incorrect initial progress value"
609616 assert worker_progresses [- 1 ] == 1 , "missing/incorrect final progress value"
610- worker_logs = [TaskLogEvent .model_validate_json (msg ).log for msg in log_sub .buffer ]
617+ log_rabbit_client_parser .assert_called_once ()
618+ # worker_logs = [TaskLogEvent.model_validate_json(msg).log for msg in log_sub.buffer]
619+ worker_logs = []
611620 print (f"<-- we got { len (worker_logs )} lines of logs" )
612621
613622 for log in sleeper_task .expected_logs :
@@ -641,9 +650,9 @@ async def test_run_computational_sidecar_dask(
641650async def test_run_computational_sidecar_dask_does_not_lose_messages_with_pubsub (
642651 dask_client : distributed .Client ,
643652 sidecar_task : Callable [..., ServiceExampleParam ],
644- log_sub : distributed .Sub ,
645653 progress_sub : distributed .Sub ,
646654 mocked_get_image_labels : mock .Mock ,
655+ log_rabbit_client_parser : mock .AsyncMock ,
647656):
648657 mocked_get_image_labels .assert_not_called ()
649658 NUMBER_OF_LOGS = 20000
@@ -679,7 +688,9 @@ async def test_run_computational_sidecar_dask_does_not_lose_messages_with_pubsub
679688 assert worker_progresses [0 ] == 0 , "missing/incorrect initial progress value"
680689 assert worker_progresses [- 1 ] == 1 , "missing/incorrect final progress value"
681690
682- worker_logs = [TaskLogEvent .model_validate_json (msg ).log for msg in log_sub .buffer ]
691+ log_rabbit_client_parser .assert_called_once ()
692+ # worker_logs = [TaskLogEvent.model_validate_json(msg).log for msg in log_sub.buffer]
693+ worker_logs = []
683694 # check all the awaited logs are in there
684695 filtered_worker_logs = filter (lambda log : "This is iteration" in log , worker_logs )
685696 assert len (list (filtered_worker_logs )) == NUMBER_OF_LOGS
0 commit comments