-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
185 lines (131 loc) · 5.14 KB
/
tests.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Copyright (c) 2024 nggit
import asyncio
import sys
import time
import unittest
from awaiter import ThreadExecutor, MultiThreadExecutor
class TestThreadExecutor(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.executor = ThreadExecutor(loop=self.loop)
self.executor.start()
def tearDown(self):
self.loop.run_until_complete(self.executor.shutdown())
self.loop.close()
self.assertEqual(self.executor.queue.qsize(), 0)
def test_result(self):
def blocking_function(name):
return f'Hello, {name}!'
async def test():
result = await self.executor(blocking_function)('World')
self.assertEqual(result, 'Hello, World!')
self.loop.run_until_complete(test())
def test_exception(self):
@self.executor
def blocking_function():
sys.exit(1)
async def test():
with self.assertRaises(SystemExit) as cm:
await blocking_function()
self.assertEqual(str(cm.exception), '1')
self.loop.run_until_complete(test())
def test_multiple_tasks(self):
def blocking_function(name):
time.sleep(0.25)
return f'Hello, {name}!'
async def test():
# submit to the thread without waiting
fut1 = self.executor.submit(blocking_function, 'Foo')
fut2 = self.executor.submit(blocking_function, 'Bar')
await asyncio.sleep(1)
# after 1 seconds there should be a result without these:
# await fut1
# await fut2
self.assertEqual(fut1.result(), 'Hello, Foo!')
self.assertEqual(fut2.result(), 'Hello, Bar!')
self.loop.run_until_complete(test())
def test_context_manager(self):
executor = ThreadExecutor()
@executor
def blocking_function(name):
return f'Hello, {name}!'
async def test():
async with executor:
result = await blocking_function('World')
self.assertEqual(result, 'Hello, World!')
self.loop.run_until_complete(test())
def test_generator_function(self):
@self.executor
def generator_function(name):
yield b'Hello, '
time.sleep(0.1)
yield name
yield b'!'
async def test():
result = bytearray()
async for data in generator_function(b'World'):
result.extend(data)
self.assertEqual(result, b'Hello, World!')
self.loop.run_until_complete(test())
def test_iterable(self):
async def test():
result = bytearray()
async for data in self.executor(b'Hello, World!')():
result.append(data)
self.assertEqual(result, b'Hello, World!')
self.loop.run_until_complete(test())
def test_cancel_future(self):
def blocking_function():
return
async def test():
fut = self.executor.submit(blocking_function)
fut.cancel()
await asyncio.sleep(1)
self.assertTrue(fut.done())
self.loop.run_until_complete(test())
def test_submit_before_start(self):
executor = MultiThreadExecutor(1)
with self.assertRaises(RuntimeError) as cm:
executor.submit(None)
self.assertEqual(
str(cm.exception),
'calling submit() before start() or after shutdown()'
)
def test_submit_after_shutdown(self):
self.loop.run_until_complete(self.executor.shutdown())
with self.assertRaises(RuntimeError) as cm:
self.executor.submit(None)
self.assertEqual(
str(cm.exception),
'calling submit() before start() or after shutdown()'
)
def test_submit_none(self):
with self.assertRaises(TypeError) as cm:
self.executor.submit(None)
self.assertEqual(str(cm.exception), 'None is not callable or iterable')
def test_shutdown(self):
fut = self.executor.shutdown()
self.loop.run_until_complete(fut)
self.assertTrue(fut.done())
self.assertFalse(self.executor.is_alive())
def test_multithread_executor(self):
def blocking_function(name):
time.sleep(0.1)
return f'Hello, {name}!'
async def test():
for size in (1, 2, 10):
async with MultiThreadExecutor(size) as executor:
fut1 = executor(blocking_function)('Foo')
fut2 = executor(blocking_function)('Bar')
# test early shutdown
await executor.shutdown()
# await fut1
# await fut2
self.assertEqual(executor.queue.qsize(), 0)
self.assertEqual(fut1.result(), 'Hello, Foo!')
self.assertEqual(fut2.result(), 'Hello, Bar!')
self.assertEqual(executor.queue.qsize(), 0)
self.loop.run_until_complete(test())
if __name__ == '__main__':
unittest.main()