1- import uuid
21import asyncio
2+ import io
33import logging
4+ import uuid
45
56from temporalio .client import Client
67from temporalio .worker import Worker
78
8- from hello .hello_change_log_level import GreetingWorkflow
9+ from hello .hello_change_log_level import LOG_MESSAGE , GreetingWorkflow
10+
911
12+ async def test_workflow_with_log_capture (client : Client ):
1013
11- async def test_workflow_with_changed_log_level (client : Client , caplog ):
14+ log_stream = io .StringIO ()
15+ handler = logging .StreamHandler (log_stream )
16+ handler .setLevel (logging .DEBUG )
17+
18+ logger = logging .getLogger ()
19+ logger .addHandler (handler )
20+ logger .setLevel (logging .DEBUG )
1221
1322 task_queue = f"tq-{ uuid .uuid4 ()} "
1423
@@ -17,14 +26,18 @@ async def test_workflow_with_changed_log_level(client: Client, caplog):
1726 task_queue = task_queue ,
1827 workflows = [GreetingWorkflow ],
1928 ):
20- with caplog .at_level (logging .ERROR ):
21- handle = await client .start_workflow (
22- GreetingWorkflow .run ,
23- id = f"wf-{ uuid .uuid4 ()} " ,
24- task_queue = task_queue ,
25- )
26- await asyncio .sleep (.1 )
27- handle .terminate ()
28-
29- assert any ("log level" in m for m in caplog .messages )
30- assert True
29+ handle = await client .start_workflow (
30+ GreetingWorkflow .run ,
31+ id = f"wf-{ uuid .uuid4 ()} " ,
32+ task_queue = task_queue ,
33+ )
34+ await asyncio .sleep (
35+ 0.2
36+ ) # arbitrary wait to ensure the workflow has started and logged
37+ await handle .terminate ()
38+
39+ logger .removeHandler (handler )
40+ handler .flush ()
41+
42+ logs = log_stream .getvalue ()
43+ assert LOG_MESSAGE in logs
0 commit comments