summaryrefslogtreecommitdiff
path: root/Lib/test/test_asyncio/test_futures.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2016-09-09 14:26:31 -0700
committerGuido van Rossum <guido@python.org>2016-09-09 14:26:31 -0700
commit7b3b3dc85da3ec176d7fd7caa546298c232c9c0a (patch)
tree045958462b64c3f8c393c3cc05131c03ceb43aef /Lib/test/test_asyncio/test_futures.py
parent9b32bda851c113cf4a85cdc01c603a1daba4d5d4 (diff)
downloadcpython-git-7b3b3dc85da3ec176d7fd7caa546298c232c9c0a.tar.gz
Merge asyncio upstream.
Diffstat (limited to 'Lib/test/test_asyncio/test_futures.py')
-rw-r--r--Lib/test/test_asyncio/test_futures.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py
index c38c1f29e7..d20eb687f9 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -25,6 +25,74 @@ def last_cb():
pass
+class DuckFuture:
+ # Class that does not inherit from Future but aims to be duck-type
+ # compatible with it.
+
+ _asyncio_future_blocking = False
+ __cancelled = False
+ __result = None
+ __exception = None
+
+ def cancel(self):
+ if self.done():
+ return False
+ self.__cancelled = True
+ return True
+
+ def cancelled(self):
+ return self.__cancelled
+
+ def done(self):
+ return (self.__cancelled
+ or self.__result is not None
+ or self.__exception is not None)
+
+ def result(self):
+ assert not self.cancelled()
+ if self.__exception is not None:
+ raise self.__exception
+ return self.__result
+
+ def exception(self):
+ assert not self.cancelled()
+ return self.__exception
+
+ def set_result(self, result):
+ assert not self.done()
+ assert result is not None
+ self.__result = result
+
+ def set_exception(self, exception):
+ assert not self.done()
+ assert exception is not None
+ self.__exception = exception
+
+ def __iter__(self):
+ if not self.done():
+ self._asyncio_future_blocking = True
+ yield self
+ assert self.done()
+ return self.result()
+
+
+class DuckTests(test_utils.TestCase):
+
+ def setUp(self):
+ self.loop = self.new_test_loop()
+ self.addCleanup(self.loop.close)
+
+ def test_wrap_future(self):
+ f = DuckFuture()
+ g = asyncio.wrap_future(f)
+ assert g is f
+
+ def test_ensure_future(self):
+ f = DuckFuture()
+ g = asyncio.ensure_future(f)
+ assert g is f
+
+
class FutureTests(test_utils.TestCase):
def setUp(self):