Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ def resume_reading(self):
if self._loop.get_debug():
logger.debug("%r resumes reading", self)

def is_reading(self):
return not self._paused and not self._closing

def set_protocol(self, protocol):
self._protocol = protocol

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_asyncio/test_unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,20 @@ def test_resume_reading(self, m_read):
tr.resume_reading()
self.loop.assert_reader(5, tr._read_ready)

@mock.patch('os.read')
def test_is_reading(self, m_read):
tr = self.read_pipe_transport()
tr._paused = False
tr._closing = False
self.assertTrue(tr.is_reading())
tr._paused = True
self.assertFalse(tr.is_reading())
tr._paused = False
tr._closing = True
self.assertFalse(tr.is_reading())
tr._closing = False
self.assertTrue(tr.is_reading())

@mock.patch('os.read')
def test_close(self, m_read):
tr = self.read_pipe_transport()
Expand Down
103 changes: 103 additions & 0 deletions Lib/test/test_asyncio/test_unix_pipes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""
Functional tests for Unix transport pipes, designed around opening two sockets
on the localhost and sending information between
"""

import unittest
import sys
import os
import tempfile
import threading

if sys.platform == 'win32':
raise unittest.SkipTest('UNIX only')

import asyncio
from asyncio import unix_events


def tearDownModule():
asyncio.set_event_loop_policy(None)


class UnixReadPipeTransportFuncTests(unittest.TestCase):
"""
Verify that transports on Unix can facilitate reading and have access to
all state methods: verify using class internals
"""
Comment on lines +24 to +27
Copy link
Contributor

@aeros aeros Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: As per PEP 257, within the standard library, we use the following format for all docstrings:

"""..."""

or

"""...
...
"""

Rather than:

"""
...
"""

I was originally hesitant to point it out for a new test file that doesn't have an existing convention, but during this year's virtual core dev sprint (Oct. 19 - 23), I asked in the general chat and Guido commented that he was "unwaveringly in favor" of using the PEP 257 docstring conventions.


async def register_read_handle(self):
"""
Wait for read_handle and then register it to the loop
"""
self.transport, self.protocol = await self.loop.connect_read_pipe(
asyncio.BaseProtocol,
self.read_handle,
)

def setup_read_handle(self):
"""
Open the read handle and record it in an attribute
"""
self.read_handle = open(self.pipe, "r")

def setup_write_handle(self):
"""
Open the write handle and record it in an attribute
"""
self.write_handle = open(self.pipe, "w")

def setUp(self):
"""
Create the UNIX pipe and register the read end to the loop, and connect
a write handle asynchronously
"""
self.loop = asyncio.get_event_loop()
self.temp_dir = tempfile.TemporaryDirectory(suffix="async_unix_events")
self.pipe = os.path.join(self.temp_dir.name, "unix_pipe")
os.mkfifo(self.pipe)

# Set the threads to open the handles going
r_handle_thread = threading.Thread(target=self.setup_read_handle)
r_handle_thread.start()
w_handle_thread = threading.Thread(target=self.setup_write_handle)
w_handle_thread.start()

# Wait for pipe pair to connect
r_handle_thread.join()
w_handle_thread.join()

# Once pipe is connected, get the read transport
self.loop.run_until_complete(self.register_read_handle())

self.assertIsInstance(self.transport,
unix_events._UnixReadPipeTransport)

def tearDown(self):
"""
Destroy the read transport and the pipe
"""
self.transport.close()
self.write_handle.close()
self.read_handle.close()
self.loop._run_once()
os.unlink(self.pipe)
self.temp_dir.cleanup()
self.loop.close()

def test_is_reading(self):
"""
Verify that is_reading returns True unless transport is closed/closing
or paused
"""
self.assertTrue(self.transport.is_reading())
self.transport.pause_reading()
self.assertFalse(self.transport.is_reading())
self.transport.resume_reading()
self.assertTrue(self.transport.is_reading())
self.transport.close()
self.assertFalse(self.transport.is_reading())


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The read pipe in :mod:`asyncio` UNIX transports now has access to the
`is_reading()` API.