summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-06-10 08:23:10 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-06-10 08:23:10 (GMT)
commitbb2fc5b2a58993c80ee81f10fe463039520a4162 (patch)
tree7f072c58fe93ed3fec34e99c5bb642e81f68be52 /Lib/test
parent15386652bfc57721d52e00e43a0e2ed66724995d (diff)
downloadcpython-bb2fc5b2a58993c80ee81f10fe463039520a4162.zip
cpython-bb2fc5b2a58993c80ee81f10fe463039520a4162.tar.gz
cpython-bb2fc5b2a58993c80ee81f10fe463039520a4162.tar.bz2
Issue #21326: Add a new is_closed() method to asyncio.BaseEventLoop
Add BaseEventLoop._closed attribute and use it to check if the event loop was closed or not, instead of checking different attributes in each subclass of BaseEventLoop. run_forever() and run_until_complete() methods now raise a RuntimeError('Event loop is closed') exception if the event loop was closed. BaseProactorEventLoop.close() now also cancels "accept futures".
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_asyncio/test_base_events.py14
-rw-r--r--Lib/test/test_asyncio/test_selector_events.py17
2 files changed, 28 insertions, 3 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
index e28c327..1611a11 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -52,6 +52,20 @@ class BaseEventLoopTests(unittest.TestCase):
gen = self.loop._make_subprocess_transport(m, m, m, m, m, m, m)
self.assertRaises(NotImplementedError, next, iter(gen))
+ def test_close(self):
+ self.assertFalse(self.loop.is_closed())
+ self.loop.close()
+ self.assertTrue(self.loop.is_closed())
+
+ # it should be possible to call close() more than once
+ self.loop.close()
+ self.loop.close()
+
+ # operation blocked when the loop is closed
+ f = asyncio.Future(loop=self.loop)
+ self.assertRaises(RuntimeError, self.loop.run_forever)
+ self.assertRaises(RuntimeError, self.loop.run_until_complete, f)
+
def test__add_callback_handle(self):
h = asyncio.Handle(lambda: False, (), self.loop)
diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py
index d7fafab..36f6508 100644
--- a/Lib/test/test_asyncio/test_selector_events.py
+++ b/Lib/test/test_asyncio/test_selector_events.py
@@ -80,7 +80,10 @@ class BaseSelectorEventLoopTests(unittest.TestCase):
self.loop._selector.close()
self.loop._selector = selector = mock.Mock()
+ self.assertFalse(self.loop.is_closed())
+
self.loop.close()
+ self.assertTrue(self.loop.is_closed())
self.assertIsNone(self.loop._selector)
self.assertIsNone(self.loop._csock)
self.assertIsNone(self.loop._ssock)
@@ -89,9 +92,20 @@ class BaseSelectorEventLoopTests(unittest.TestCase):
csock.close.assert_called_with()
remove_reader.assert_called_with(7)
+ # it should be possible to call close() more than once
self.loop.close()
self.loop.close()
+ # operation blocked when the loop is closed
+ f = asyncio.Future(loop=self.loop)
+ self.assertRaises(RuntimeError, self.loop.run_forever)
+ self.assertRaises(RuntimeError, self.loop.run_until_complete, f)
+ fd = 0
+ def callback():
+ pass
+ self.assertRaises(RuntimeError, self.loop.add_reader, fd, callback)
+ self.assertRaises(RuntimeError, self.loop.add_writer, fd, callback)
+
def test_close_no_selector(self):
ssock = self.loop._ssock
csock = self.loop._csock
@@ -101,9 +115,6 @@ class BaseSelectorEventLoopTests(unittest.TestCase):
self.loop._selector = None
self.loop.close()
self.assertIsNone(self.loop._selector)
- self.assertFalse(ssock.close.called)
- self.assertFalse(csock.close.called)
- self.assertFalse(remove_reader.called)
def test_socketpair(self):
self.assertRaises(NotImplementedError, self.loop._socketpair)