commit: be0b5d33ee8b355dcc9932b52bb1e025e6bd58d4
Author: Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Mon Jan 18 11:16:53 2021 +0000
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Mon Jan 18 11:31:57 2021 +0000
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=be0b5d33
RetryTestCase: Use async and await syntax
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>
lib/portage/tests/util/futures/test_retry.py | 28 +++++++++-------------------
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/lib/portage/tests/util/futures/test_retry.py
b/lib/portage/tests/util/futures/test_retry.py
index 6648b1b2c..bce48a693 100644
--- a/lib/portage/tests/util/futures/test_retry.py
+++ b/lib/portage/tests/util/futures/test_retry.py
@@ -1,4 +1,4 @@
-# Copyright 2018-2020 Gentoo Authors
+# Copyright 2018-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
from concurrent.futures import Future, ThreadPoolExecutor
@@ -32,18 +32,11 @@ class SucceedLater:
def __init__(self, duration):
self._succeed_time = time.monotonic() + duration
- def __call__(self):
- loop = global_event_loop()
- result = loop.create_future()
+ async def __call__(self):
remaining = self._succeed_time - time.monotonic()
if remaining > 0:
- loop.call_soon_threadsafe(lambda: None if result.done()
else
- result.set_exception(SucceedLaterException(
- 'time until success: {}
seconds'.format(remaining))))
- else:
- loop.call_soon_threadsafe(lambda: None if result.done()
else
- result.set_result('success'))
- return result
+ await asyncio.sleep(remaining)
+ return 'success'
class SucceedNeverException(Exception):
@@ -54,20 +47,17 @@ class SucceedNever:
"""
A callable object that never succeeds.
"""
- def __call__(self):
- loop = global_event_loop()
- result = loop.create_future()
- loop.call_soon_threadsafe(lambda: None if result.done() else
- result.set_exception(SucceedNeverException('expected
failure')))
- return result
+ async def __call__(self):
+ raise SucceedNeverException('expected failure')
class HangForever:
"""
A callable object that sleeps forever.
"""
- def __call__(self):
- return asyncio.Future()
+ async def __call__(self):
+ while True:
+ await asyncio.sleep(9)
class RetryTestCase(TestCase):