Skip to content

Commit 37f7978

Browse files
committed
added logging test
1 parent 8467966 commit 37f7978

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

hello/hello_change_log_level.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ def filter(self, record: logging.LogRecord) -> bool:
3333
# --- End logging set‑up ----------------------------------------------------------
3434

3535

36+
LOG_MESSAGE = "This error is an experiment to check the log level"
37+
38+
3639
@workflow.defn
3740
class GreetingWorkflow:
3841
@workflow.run
3942
async def run(self):
40-
raise RuntimeError("This error is an experiment to check the log level")
43+
raise RuntimeError(LOG_MESSAGE)
4144

4245

4346
async def main():
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import uuid
21
import asyncio
2+
import io
33
import logging
4+
import uuid
45

56
from temporalio.client import Client
67
from 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

Comments
 (0)