aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2018-07-28 06:53:11 -0700
committerZac Medico <zmedico@gentoo.org>2018-09-23 20:41:32 -0700
commitde0b60ff277311e780102131dce3111b4db1c196 (patch)
treec12bd54c0ec80fed55f7541f045c0ab541bd3f75 /lib/portage/tests
parentImplement asyncio.iscoroutinefunction for compat_coroutine (diff)
downloadportage-de0b60ff277311e780102131dce3111b4db1c196.tar.gz
portage-de0b60ff277311e780102131dce3111b4db1c196.tar.bz2
portage-de0b60ff277311e780102131dce3111b4db1c196.zip
Add _sync_decorator module
Add functions that decorate coroutine methods and functions for synchronous usage, allowing coroutines to smoothly blend with synchronous code. This eliminates clutter that might otherwise discourage the proliferation of coroutine usage for I/O bound tasks. In the next commit, _sync_decorator will be used for smooth integration of new classes that have coroutine methods. Bug: https://bugs.gentoo.org/662070 Reviewed-by: Brian Dolbec <dolsen@gentoo.org> Signed-off-by: Zac Medico <zmedico@gentoo.org>
Diffstat (limited to 'lib/portage/tests')
-rw-r--r--lib/portage/tests/util/futures/test_compat_coroutine.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/portage/tests/util/futures/test_compat_coroutine.py b/lib/portage/tests/util/futures/test_compat_coroutine.py
index b6f75b1a2..f96aa9be5 100644
--- a/lib/portage/tests/util/futures/test_compat_coroutine.py
+++ b/lib/portage/tests/util/futures/test_compat_coroutine.py
@@ -6,6 +6,7 @@ from portage.util.futures.compat_coroutine import (
coroutine,
coroutine_return,
)
+from portage.util.futures._sync_decorator import _sync_decorator, _sync_methods
from portage.tests import TestCase
@@ -161,3 +162,16 @@ class CompatCoroutineTestCase(TestCase):
loop.run_until_complete(asyncio.wait([writer, reader]))
self.assertEqual(reader.result(), values)
+
+ # Test decoration of coroutine methods and functions for
+ # synchronous usage, allowing coroutines to smoothly
+ # blend with synchronous code.
+ sync_cubby = _sync_methods(cubby, loop=loop)
+ sync_reader = _sync_decorator(reader_coroutine, loop=loop)
+ writer = asyncio.ensure_future(writer_coroutine(cubby, values, None), loop=loop)
+ self.assertEqual(sync_reader(cubby, None), values)
+ self.assertTrue(writer.done())
+
+ for i in range(3):
+ sync_cubby.write(i)
+ self.assertEqual(sync_cubby.read(), i)