1 |
commit: 5a5ed99cb5a6e8913df2e9ca29b4b4d5c179c20f |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat May 5 23:04:10 2018 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun May 6 00:35:44 2018 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=5a5ed99c |
7 |
|
8 |
RetryTestCase: support ThreadPoolExecutor (bug 654390) |
9 |
|
10 |
In order to support the default asyncio event loop's |
11 |
ThreadPoolExecutor, use a threading.Event instance to |
12 |
support cancellation of tasks. |
13 |
|
14 |
Bug: https://bugs.gentoo.org/654390 |
15 |
|
16 |
pym/portage/tests/util/futures/test_retry.py | 96 +++++++++++++++++++++------- |
17 |
1 file changed, 74 insertions(+), 22 deletions(-) |
18 |
|
19 |
diff --git a/pym/portage/tests/util/futures/test_retry.py b/pym/portage/tests/util/futures/test_retry.py |
20 |
index cdca7d294..781eac9a1 100644 |
21 |
--- a/pym/portage/tests/util/futures/test_retry.py |
22 |
+++ b/pym/portage/tests/util/futures/test_retry.py |
23 |
@@ -1,8 +1,6 @@ |
24 |
# Copyright 2018 Gentoo Foundation |
25 |
# Distributed under the terms of the GNU General Public License v2 |
26 |
|
27 |
-import functools |
28 |
- |
29 |
try: |
30 |
import threading |
31 |
except ImportError: |
32 |
@@ -28,10 +26,17 @@ class SucceedLater(object): |
33 |
self._succeed_time = monotonic() + duration |
34 |
|
35 |
def __call__(self): |
36 |
+ loop = global_event_loop() |
37 |
+ result = loop.create_future() |
38 |
remaining = self._succeed_time - monotonic() |
39 |
if remaining > 0: |
40 |
- raise SucceedLaterException('time until success: {} seconds'.format(remaining)) |
41 |
- return 'success' |
42 |
+ loop.call_soon_threadsafe(lambda: None if result.done() else |
43 |
+ result.set_exception(SucceedLaterException( |
44 |
+ 'time until success: {} seconds'.format(remaining)))) |
45 |
+ else: |
46 |
+ loop.call_soon_threadsafe(lambda: None if result.done() else |
47 |
+ result.set_result('success')) |
48 |
+ return result |
49 |
|
50 |
|
51 |
class SucceedNeverException(Exception): |
52 |
@@ -43,7 +48,11 @@ class SucceedNever(object): |
53 |
A callable object that never succeeds. |
54 |
""" |
55 |
def __call__(self): |
56 |
- raise SucceedNeverException('expected failure') |
57 |
+ loop = global_event_loop() |
58 |
+ result = loop.create_future() |
59 |
+ loop.call_soon_threadsafe(lambda: None if result.done() else |
60 |
+ result.set_exception(SucceedNeverException('expected failure'))) |
61 |
+ return result |
62 |
|
63 |
|
64 |
class HangForever(object): |
65 |
@@ -51,14 +60,21 @@ class HangForever(object): |
66 |
A callable object that sleeps forever. |
67 |
""" |
68 |
def __call__(self): |
69 |
- threading.Event().wait() |
70 |
+ return global_event_loop().create_future() |
71 |
|
72 |
|
73 |
class RetryTestCase(TestCase): |
74 |
+ |
75 |
+ def _wrap_coroutine_func(self, coroutine_func): |
76 |
+ """ |
77 |
+ Derived classes may override this method in order to implement |
78 |
+ alternative forms of execution. |
79 |
+ """ |
80 |
+ return coroutine_func |
81 |
+ |
82 |
def testSucceedLater(self): |
83 |
loop = global_event_loop() |
84 |
- func = SucceedLater(1) |
85 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
86 |
+ func_coroutine = self._wrap_coroutine_func(SucceedLater(1)) |
87 |
decorator = retry(try_max=9999, |
88 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
89 |
decorated_func = decorator(func_coroutine) |
90 |
@@ -67,8 +83,7 @@ class RetryTestCase(TestCase): |
91 |
|
92 |
def testSucceedNever(self): |
93 |
loop = global_event_loop() |
94 |
- func = SucceedNever() |
95 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
96 |
+ func_coroutine = self._wrap_coroutine_func(SucceedNever()) |
97 |
decorator = retry(try_max=4, try_timeout=None, |
98 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
99 |
decorated_func = decorator(func_coroutine) |
100 |
@@ -78,8 +93,7 @@ class RetryTestCase(TestCase): |
101 |
|
102 |
def testSucceedNeverReraise(self): |
103 |
loop = global_event_loop() |
104 |
- func = SucceedNever() |
105 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
106 |
+ func_coroutine = self._wrap_coroutine_func(SucceedNever()) |
107 |
decorator = retry(reraise=True, try_max=4, try_timeout=None, |
108 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
109 |
decorated_func = decorator(func_coroutine) |
110 |
@@ -89,8 +103,7 @@ class RetryTestCase(TestCase): |
111 |
|
112 |
def testHangForever(self): |
113 |
loop = global_event_loop() |
114 |
- func = HangForever() |
115 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
116 |
+ func_coroutine = self._wrap_coroutine_func(HangForever()) |
117 |
decorator = retry(try_max=2, try_timeout=0.1, |
118 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
119 |
decorated_func = decorator(func_coroutine) |
120 |
@@ -100,8 +113,7 @@ class RetryTestCase(TestCase): |
121 |
|
122 |
def testHangForeverReraise(self): |
123 |
loop = global_event_loop() |
124 |
- func = HangForever() |
125 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
126 |
+ func_coroutine = self._wrap_coroutine_func(HangForever()) |
127 |
decorator = retry(reraise=True, try_max=2, try_timeout=0.1, |
128 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
129 |
decorated_func = decorator(func_coroutine) |
130 |
@@ -111,8 +123,7 @@ class RetryTestCase(TestCase): |
131 |
|
132 |
def testCancelRetry(self): |
133 |
loop = global_event_loop() |
134 |
- func = SucceedNever() |
135 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
136 |
+ func_coroutine = self._wrap_coroutine_func(SucceedNever()) |
137 |
decorator = retry(try_timeout=0.1, |
138 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
139 |
decorated_func = decorator(func_coroutine) |
140 |
@@ -124,8 +135,7 @@ class RetryTestCase(TestCase): |
141 |
|
142 |
def testOverallTimeoutWithException(self): |
143 |
loop = global_event_loop() |
144 |
- func = SucceedNever() |
145 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
146 |
+ func_coroutine = self._wrap_coroutine_func(SucceedNever()) |
147 |
decorator = retry(try_timeout=0.1, overall_timeout=0.3, |
148 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
149 |
decorated_func = decorator(func_coroutine) |
150 |
@@ -136,11 +146,53 @@ class RetryTestCase(TestCase): |
151 |
def testOverallTimeoutWithTimeoutError(self): |
152 |
loop = global_event_loop() |
153 |
# results in TimeoutError because it hangs forever |
154 |
- func = HangForever() |
155 |
- func_coroutine = functools.partial(loop.run_in_executor, None, func) |
156 |
+ func_coroutine = self._wrap_coroutine_func(HangForever()) |
157 |
decorator = retry(try_timeout=0.1, overall_timeout=0.3, |
158 |
delay_func=RandomExponentialBackoff(multiplier=0.1, base=2)) |
159 |
decorated_func = decorator(func_coroutine) |
160 |
done, pending = loop.run_until_complete(asyncio.wait([decorated_func()], loop=loop)) |
161 |
self.assertEqual(len(done), 1) |
162 |
self.assertTrue(isinstance(done.pop().exception().__cause__, asyncio.TimeoutError)) |
163 |
+ |
164 |
+ |
165 |
+class RetryExecutorTestCase(RetryTestCase): |
166 |
+ """ |
167 |
+ Wrap each coroutine function with AbstractEventLoop.run_in_executor, |
168 |
+ in order to test the event loop's default executor. The executor |
169 |
+ may use either a thread or a subprocess, and either case is |
170 |
+ automatically detected and handled. |
171 |
+ """ |
172 |
+ def _wrap_coroutine_func(self, coroutine_func): |
173 |
+ parent_loop = global_event_loop() |
174 |
+ |
175 |
+ # Since ThreadPoolExecutor does not propagate cancellation of a |
176 |
+ # parent_future to the underlying coroutine, use kill_switch to |
177 |
+ # propagate task cancellation to wrapper, so that HangForever's |
178 |
+ # thread returns when retry eventually cancels parent_future. |
179 |
+ def wrapper(kill_switch): |
180 |
+ loop = global_event_loop() |
181 |
+ if loop is parent_loop: |
182 |
+ # thread in main process |
183 |
+ result = coroutine_func() |
184 |
+ event = threading.Event() |
185 |
+ loop.call_soon_threadsafe(result.add_done_callback, |
186 |
+ lambda result: event.set()) |
187 |
+ loop.call_soon_threadsafe(kill_switch.add_done_callback, |
188 |
+ lambda kill_switch: event.set()) |
189 |
+ event.wait() |
190 |
+ return result.result() |
191 |
+ else: |
192 |
+ # child process |
193 |
+ return loop.run_until_complete(coroutine_func()) |
194 |
+ |
195 |
+ def execute_wrapper(): |
196 |
+ kill_switch = parent_loop.create_future() |
197 |
+ parent_future = asyncio.ensure_future( |
198 |
+ parent_loop.run_in_executor(None, wrapper, kill_switch), |
199 |
+ loop=parent_loop) |
200 |
+ parent_future.add_done_callback( |
201 |
+ lambda parent_future: None if kill_switch.done() |
202 |
+ else kill_switch.set_result(None)) |
203 |
+ return parent_future |
204 |
+ |
205 |
+ return execute_wrapper |