This repository was archived by the owner on Mar 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathnest_test.py
More file actions
66 lines (51 loc) · 1.92 KB
/
nest_test.py
File metadata and controls
66 lines (51 loc) · 1.92 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
import unittest
import asyncio
import nest_asyncio
nest_asyncio.apply()
def exception_handler(loop, context):
print('Exception:', context)
class NestTest(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.loop.set_debug(True)
self.loop.set_exception_handler(exception_handler)
def tearDown(self):
self.loop.stop()
del self.loop
async def coro(self):
await asyncio.sleep(0.01)
return 42
async def coro2(self):
result = self.loop.run_until_complete(self.coro())
self.assertEqual(result, await self.coro())
return result
async def coro3(self):
result = self.loop.run_until_complete(self.coro2())
self.assertEqual(result, await self.coro2())
return result
def test_nesting(self):
result = self.loop.run_until_complete(self.coro3())
self.assertEqual(result, 42)
async def ensure_future_with_run_until_complete(self):
task = asyncio.ensure_future(self.coro())
return self.loop.run_until_complete(task)
def test_ensure_future_with_run_until_complete(self):
result = self.loop.run_until_complete(
self.ensure_future_with_run_until_complete())
self.assertEqual(result, 42)
async def ensure_future_with_run_until_complete_with_wait(self):
task = asyncio.ensure_future(self.coro())
done, pending = self.loop.run_until_complete(
asyncio.wait([task], return_when=asyncio.ALL_COMPLETED))
task = done.pop()
return task.result()
def test_ensure_future_with_run_until_complete_with_wait(self):
result = self.loop.run_until_complete(
self.ensure_future_with_run_until_complete_with_wait())
self.assertEqual(result, 42)
if __name__ == '__main__':
try:
unittest.main()
except SystemExit:
pass