-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtest_asynchronous_thread.py
233 lines (164 loc) · 6.25 KB
/
test_asynchronous_thread.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import asyncio
import unittest
import threading
import dataclasses
from pebble import ThreadPool
from pebble import asynchronous
def not_decorated(argument, keyword_argument=0):
return argument + keyword_argument
@asynchronous.thread
def decorated(argument, keyword_argument=0):
"""A docstring."""
return argument + keyword_argument
@asynchronous.thread
def error_decorated():
raise RuntimeError("BOOM!")
@asynchronous.thread
def error_returned():
return RuntimeError("BOOM!")
@dataclasses.dataclass(frozen=True)
class FrozenError(Exception):
pass
@asynchronous.thread()
def frozen_error_decorated():
raise FrozenError()
@asynchronous.thread()
def name_keyword_argument(name='function_kwarg'):
return name
@asynchronous.thread(name='asynchronous_thread_name')
def name_keyword_decorated():
return threading.current_thread().name
@asynchronous.thread(name='decorator_kwarg')
def name_keyword_decorated_and_argument(name='bar'):
return (threading.current_thread().name, name)
@asynchronous.thread(daemon=False)
def daemon_keyword_decorated():
return threading.current_thread().daemon
@asynchronous.thread(pool=ThreadPool(1))
def pool_decorated(_argument, _keyword_argument=0):
return threading.current_thread().ident
class ThreadAsynchronousObj:
a = 0
def __init__(self):
self.b = 1
@classmethod
@asynchronous.thread
def clsmethod(cls):
return cls.a
@asynchronous.thread
def instmethod(self):
return self.b
@staticmethod
@asynchronous.thread
def stcmethod():
return 2
class TestThreadAsynchronous(unittest.TestCase):
def setUp(self):
self.results = 0
self.exception = None
self.event = None
self.asynchronousobj = ThreadAsynchronousObj()
def callback(self, future):
try:
self.results = future.result()
except (RuntimeError) as error:
self.exception = error
finally:
self.event.set()
def test_docstring(self):
"""Thread docstring is preserved."""
self.assertEqual(decorated.__doc__, "A docstring.")
def test_class_method(self):
"""Thread decorated classmethods."""
async def test():
return await ThreadAsynchronousObj.clsmethod()
self.assertEqual(asyncio.run(test()), 0)
def test_instance_method(self):
"""Thread decorated instance methods."""
async def test():
return await self.asynchronousobj.instmethod()
self.assertEqual(asyncio.run(test()), 1)
def test_static_method(self):
"""Thread decorated static methods ( startmethod only)."""
async def test():
return await self.asynchronousobj.stcmethod()
self.assertEqual(asyncio.run(test()), 2)
def test_not_decorated_results(self):
"""Process Fork results are produced."""
non_decorated = asynchronous.thread(not_decorated)
async def test():
return await non_decorated(1, 1)
self.assertEqual(asyncio.run(test()), 2)
def test_decorated_results(self):
"""Thread results are produced."""
async def test():
return await decorated(1, 1)
self.assertEqual(asyncio.run(test()), 2)
def test_decorated_results_callback(self):
"""Thread results are forwarded to the callback."""
async def test():
self.event = asyncio.Event()
self.event.clear()
future = decorated(1, 1)
future.add_done_callback(self.callback)
await self.event.wait()
asyncio.run(test())
self.assertEqual(self.results, 2)
def test_error_decorated(self):
"""Thread errors are raised by future.result."""
async def test():
return await error_decorated()
with self.assertRaises(RuntimeError):
asyncio.run(test())
def test_error_returned(self):
"""Thread errors are raised by future.result."""
async def test():
return await error_returned()
self.assertIsInstance(asyncio.run(test()), RuntimeError)
def test_error_decorated_callback(self):
"""Thread errors are forwarded to callback."""
async def test():
self.event = asyncio.Event()
self.event.clear()
future = error_decorated()
future.add_done_callback(self.callback)
await self.event.wait()
asyncio.run(test())
self.assertTrue(isinstance(self.exception, RuntimeError),
msg=str(self.exception))
def test_frozen_error_decorated(self):
"""Thread frozen errors are raised by future.result."""
async def test():
return await frozen_error_decorated()
with self.assertRaises(FrozenError):
asyncio.run(test())
def test_name_keyword_argument(self):
"""name keyword can be passed to a decorated function process without name """
async def test():
return await name_keyword_argument()
self.assertEqual(asyncio.run(test()), "function_kwarg")
def test_name_keyword_decorated(self):
"""
Check that a simple use case of the name keyword passed to the decorator works
"""
async def test():
return await name_keyword_decorated()
self.assertEqual(asyncio.run(test()), "asynchronous_thread_name")
def test_name_keyword_decorated_result(self):
"""name kwarg is handled without modifying the function kwargs"""
async def test():
return await name_keyword_decorated_and_argument(
name="function_kwarg")
dec_out, fn_out = asyncio.run(test())
self.assertEqual(dec_out, "decorator_kwarg")
self.assertEqual(fn_out, "function_kwarg")
def test_daemon_keyword_decorated(self):
"""Daemon keyword can be passed to a decorated function and spawns correctly."""
async def test():
return await daemon_keyword_decorated()
self.assertEqual(asyncio.run(test()), False)
def test_pool_decorated(self):
"""Thread pool decorated function."""
async def test():
return await pool_decorated(1, 1)
self.assertEqual(asyncio.run(test()), asyncio.run(test()))