forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_asyncioEventLoop.py
More file actions
95 lines (68 loc) · 2.83 KB
/
test_asyncioEventLoop.py
File metadata and controls
95 lines (68 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2025-2026 NV Access Limited, Bram Duvigneau, Dot Incorporated
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
"""Unit tests for _asyncioEventLoop module."""
import asyncio
import unittest
import _asyncioEventLoop
from _asyncioEventLoop.utils import runCoroutineSync
class TestRunCoroutineSync(unittest.TestCase):
"""Tests for runCoroutineSync function."""
@classmethod
def setUpClass(cls):
"""Initialize the asyncio event loop before tests."""
_asyncioEventLoop.initialize()
@classmethod
def tearDownClass(cls):
"""Terminate the asyncio event loop after tests."""
_asyncioEventLoop.terminate()
def test_returnsResult(self):
"""Test that runCoroutineSync returns the coroutine's result."""
async def simpleCoroutine():
return 42
result = runCoroutineSync(simpleCoroutine())
self.assertEqual(result, 42)
def test_returnsComplexResult(self):
"""Test that runCoroutineSync returns complex objects."""
async def complexCoroutine():
await asyncio.sleep(0.01)
return {"key": "value", "number": 123}
result = runCoroutineSync(complexCoroutine())
self.assertEqual(result, {"key": "value", "number": 123})
def test_raisesException(self):
"""Test that runCoroutineSync raises exceptions from the coroutine."""
async def failingCoroutine():
await asyncio.sleep(0.01)
raise ValueError("Test error message")
with self.assertRaises(ValueError) as cm:
runCoroutineSync(failingCoroutine())
self.assertEqual(str(cm.exception), "Test error message")
def test_timeoutRaisesTimeoutError(self):
"""Test that runCoroutineSync raises TimeoutError when timeout is exceeded."""
async def slowCoroutine():
await asyncio.sleep(10)
return "Should not reach here"
with self.assertRaises(TimeoutError) as cm:
runCoroutineSync(slowCoroutine(), timeout=0.1)
self.assertIn("timed out", str(cm.exception).lower())
def test_noTimeoutWaitsIndefinitely(self):
"""Test that runCoroutineSync waits indefinitely when no timeout is specified."""
async def delayedCoroutine():
await asyncio.sleep(0.1)
return "completed"
result = runCoroutineSync(delayedCoroutine())
self.assertEqual(result, "completed")
def test_raisesRuntimeErrorWhenEventLoopNotRunning(self):
"""Test that runCoroutineSync raises RuntimeError when event loop is not running."""
# Save original thread reference
originalThread = _asyncioEventLoop._state.asyncioThread
# Temporarily set to None to simulate not running
_asyncioEventLoop._state.asyncioThread = None
async def anyCoroutine():
return "test"
with self.assertRaises(RuntimeError) as cm:
runCoroutineSync(anyCoroutine())
self.assertIn("not running", str(cm.exception).lower())
# Restore original thread
_asyncioEventLoop._state.asyncioThread = originalThread