From c5c9fe325782a790d563a0a8b1cf62a855a50d81 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sat, 25 May 2024 10:22:21 +0200 Subject: add a FileDescriptor type It serves two main purposes. For one thing, it allows telling bare integers and file descriptors apart on a typing level similar to a NewType. For another it adds common methods to a file descriptor and enables closing it via a context manager. --- tests/test_simple.py | 62 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'tests/test_simple.py') diff --git a/tests/test_simple.py b/tests/test_simple.py index b3331a3..212f414 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -76,40 +76,40 @@ class AsnycioTest(unittest.IsolatedAsyncioTestCase): await asyncio.wait_for(fut, 10) async def test_copyfd(self) -> None: - rfd1, wfd1 = os.pipe2(os.O_NONBLOCK) - rfd2, wfd2 = os.pipe2(os.O_NONBLOCK) - fut = asyncio.ensure_future(linuxnamespaces.async_copyfd(rfd1, wfd2)) - os.write(wfd1, b"hello") - await asyncio.sleep(0.000001) # Let the loop run - os.write(wfd1, b"world") - loop = asyncio.get_running_loop() - fut2 = loop.create_future() - def callback() -> None: - loop.remove_reader(rfd2) - fut2.set_result(None) - loop.add_reader(rfd2, callback) - await fut2 - self.assertEqual(os.read(rfd2, 11), b"helloworld") - self.assertFalse(fut.done()) - os.close(wfd1) - await asyncio.sleep(0.000001) # Let the loop run - self.assertTrue(fut.done()) - os.close(rfd1) - os.close(rfd2) - os.close(wfd2) + rfd1, wfd1 = linuxnamespaces.FileDescriptor.pipe(blocking=False) + rfd2, wfd2 = linuxnamespaces.FileDescriptor.pipe(blocking=False) + with wfd2, rfd2, rfd1: + with wfd1: + fut = asyncio.ensure_future( + linuxnamespaces.async_copyfd(rfd1, wfd2) + ) + os.write(wfd1, b"hello") + await asyncio.sleep(0.000001) # Let the loop run + os.write(wfd1, b"world") + loop = asyncio.get_running_loop() + fut2 = loop.create_future() + def callback() -> None: + loop.remove_reader(rfd2) + fut2.set_result(None) + loop.add_reader(rfd2, callback) + await fut2 + self.assertEqual(os.read(rfd2, 11), b"helloworld") + self.assertFalse(fut.done()) + await asyncio.sleep(0.000001) # Let the loop run + self.assertTrue(fut.done()) self.assertEqual(await fut, 10) async def test_copyfd_epipe(self) -> None: - rfd1, wfd1 = os.pipe2(os.O_NONBLOCK) - rfd2, wfd2 = os.pipe2(os.O_NONBLOCK) - fut = asyncio.ensure_future(linuxnamespaces.async_copyfd(rfd1, wfd2)) - os.close(rfd2) - os.write(wfd1, b"hello") - await asyncio.sleep(0.000001) # Let the loop run - self.assertTrue(fut.done()) - os.close(rfd1) - os.close(wfd1) - os.close(wfd2) + rfd1, wfd1 = linuxnamespaces.FileDescriptor.pipe(blocking=False) + rfd2, wfd2 = linuxnamespaces.FileDescriptor.pipe(blocking=False) + with wfd2, wfd1, rfd1: + with rfd2: + fut = asyncio.ensure_future( + linuxnamespaces.async_copyfd(rfd1, wfd2) + ) + os.write(wfd1, b"hello") + await asyncio.sleep(0.000001) # Let the loop run + self.assertTrue(fut.done()) exc = fut.exception() self.assertIsInstance(exc, OSError) self.assertEqual(exc.errno, errno.EPIPE) -- cgit v1.2.3