summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
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)