Skip to content

Commit 583fb76

Browse files
committed
full coverat in utils
1 parent a1cd7ff commit 583fb76

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

services/web/server/src/simcore_service_webserver/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TaskInfoDict(TypedDict):
6565

6666
def get_task_info(task: asyncio.Task) -> TaskInfoDict:
6767
def _format_frame(f):
68-
return StackInfoDict(f_code=f.f_code, f_lineno=f.f_lineno)
68+
return StackInfoDict(f_code=str(f.f_code), f_lineno=str(f.f_lineno))
6969

7070
info = TaskInfoDict(
7171
txt=str(task),

services/web/server/tests/unit/isolated/test_utils.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import asyncio
2+
import contextlib
13
import time
24
import urllib.parse
35
from datetime import datetime
46

57
from simcore_service_webserver.utils import (
68
DATETIME_FORMAT,
79
compose_support_error_msg,
10+
get_task_info,
811
now_str,
912
to_datetime,
1013
)
@@ -70,3 +73,74 @@ def test_compose_support_error_msg():
7073
msg == "First sentence for Mr.X. Second sentence."
7174
" For more information please forward this message to [email protected] (supportID=OEC:139641204989600)"
7275
)
76+
77+
78+
async def test_get_task_info():
79+
"""Test get_task_info function with asyncio tasks"""
80+
81+
async def dummy_task():
82+
await asyncio.sleep(0.1)
83+
return "task_result"
84+
85+
# Create a named task
86+
task = asyncio.create_task(dummy_task(), name="test_task")
87+
88+
task_info = get_task_info(task)
89+
90+
# Check that task_info is a dictionary
91+
assert isinstance(task_info, dict)
92+
93+
# Check that it contains expected keys from TaskInfoDict
94+
expected_keys = {"txt", "type", "done", "cancelled", "stack", "exception"}
95+
assert all(key in task_info for key in expected_keys)
96+
97+
# Check basic types
98+
assert isinstance(task_info["txt"], str)
99+
assert isinstance(task_info["type"], str)
100+
assert isinstance(task_info["done"], bool)
101+
assert isinstance(task_info["cancelled"], bool)
102+
assert isinstance(task_info["stack"], list)
103+
104+
# Check that task name is in the txt representation
105+
assert "test_task" in task_info["txt"]
106+
107+
# Check that stack contains frame info when task is running
108+
if not task_info["done"]:
109+
assert len(task_info["stack"]) > 0
110+
# Check stack frame structure
111+
for frame_info in task_info["stack"]:
112+
assert "f_code" in frame_info
113+
assert "f_lineno" in frame_info
114+
assert isinstance(frame_info["f_code"], str)
115+
assert isinstance(frame_info["f_lineno"], str)
116+
117+
# Clean up
118+
task.cancel()
119+
with contextlib.suppress(asyncio.CancelledError):
120+
await task
121+
122+
123+
async def test_get_task_info_unnamed_task():
124+
"""Test get_task_info function with unnamed tasks"""
125+
126+
async def dummy_task():
127+
await asyncio.sleep(0.1)
128+
129+
# Create an unnamed task
130+
task = asyncio.create_task(dummy_task())
131+
132+
task_info = get_task_info(task)
133+
134+
# Check basic structure
135+
assert isinstance(task_info, dict)
136+
expected_keys = {"txt", "type", "done", "cancelled", "stack", "exception"}
137+
assert all(key in task_info for key in expected_keys)
138+
139+
# Check that txt contains task representation
140+
assert isinstance(task_info["txt"], str)
141+
assert "Task" in task_info["txt"]
142+
143+
# Clean up
144+
task.cancel()
145+
with contextlib.suppress(asyncio.CancelledError):
146+
await task

0 commit comments

Comments
 (0)