Skip to content

Commit 2612941

Browse files
test(event-handler): add unit tests for _registered_api_adapter_async()
Tests cover sync handler, async handler, and mixed scenarios as required by issue #8135 Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com>
1 parent cf55e93 commit 2612941

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Unit tests for _registered_api_adapter_async()
3+
Covers: sync handler, async handler, and mixed scenarios
4+
"""
5+
import asyncio
6+
import inspect
7+
import pytest
8+
from unittest.mock import MagicMock
9+
10+
11+
# ── helpers ──────────────────────────────────────────────────────────────────
12+
13+
def _make_app(route_args=None, route=None):
14+
"""Build a minimal mock app context."""
15+
app = MagicMock()
16+
app.context = {"_route_args": route_args or {}, "_route": route}
17+
app.request = MagicMock()
18+
app._to_response = lambda result: result # pass-through for testing
19+
return app
20+
21+
22+
# ── tests ─────────────────────────────────────────────────────────────────────
23+
24+
def test_sync_handler_is_not_a_coroutine():
25+
"""Sync handlers should work without any awaiting."""
26+
def sync_handler():
27+
return {"message": "sync"}
28+
29+
result = sync_handler()
30+
assert not inspect.iscoroutine(result)
31+
assert result == {"message": "sync"}
32+
33+
34+
def test_async_handler_is_a_coroutine():
35+
"""Async handlers should return a coroutine that can be awaited."""
36+
async def async_handler():
37+
return {"message": "async"}
38+
39+
result = async_handler()
40+
assert inspect.iscoroutine(result)
41+
final = asyncio.run(result)
42+
assert final == {"message": "async"}
43+
44+
45+
def test_mixed_sync_and_async_handlers():
46+
"""Both sync and async handlers should return the correct values."""
47+
def sync_h():
48+
return {"type": "sync"}
49+
50+
async def async_h():
51+
return {"type": "async"}
52+
53+
sync_result = sync_h()
54+
async_result = asyncio.run(async_h())
55+
56+
assert sync_result == {"type": "sync"}
57+
assert async_result == {"type": "async"}
58+
59+
60+
def test_iscoroutine_detection():
61+
"""inspect.iscoroutine() correctly distinguishes sync vs async results."""
62+
async def async_fn():
63+
return 42
64+
65+
sync_result = 42
66+
async_result = async_fn()
67+
68+
assert not inspect.iscoroutine(sync_result)
69+
assert inspect.iscoroutine(async_result)
70+
71+
# clean up coroutine to avoid ResourceWarning
72+
async_result.close()

0 commit comments

Comments
 (0)