summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2024-05-25 10:22:21 +0200
committerHelmut Grohne <helmut@subdivi.de>2024-05-25 10:22:21 +0200
commitc5c9fe325782a790d563a0a8b1cf62a855a50d81 (patch)
tree16fb8834cd3e260615497faf03f0f78e2929c305 /tests
parent992e877614476dd40abd11a82ffedc6e261dabdf (diff)
downloadpython-linuxnamespaces-c5c9fe325782a790d563a0a8b1cf62a855a50d81.tar.gz
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_simple.py62
1 files changed, 31 insertions, 31 deletions
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)