|
| 1 | +import asyncio |
| 2 | +import contextlib |
1 | 3 | import time |
2 | 4 | import urllib.parse |
3 | 5 | from datetime import datetime |
4 | 6 |
|
5 | 7 | from simcore_service_webserver.utils import ( |
6 | 8 | DATETIME_FORMAT, |
7 | 9 | compose_support_error_msg, |
| 10 | + get_task_info, |
8 | 11 | now_str, |
9 | 12 | to_datetime, |
10 | 13 | ) |
@@ -70,3 +73,74 @@ def test_compose_support_error_msg(): |
70 | 73 | msg == "First sentence for Mr.X. Second sentence." |
71 | 74 | " For more information please forward this message to [email protected] (supportID=OEC:139641204989600)" |
72 | 75 | ) |
| 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