|
1 | 1 | """Tests for streams.py."""
|
2 | 2 |
|
3 | 3 | import gc
|
| 4 | +import os |
4 | 5 | import socket
|
| 6 | +import sys |
5 | 7 | import unittest
|
6 | 8 | from unittest import mock
|
7 | 9 | try:
|
@@ -583,6 +585,40 @@ def client(path):
|
583 | 585 | server.stop()
|
584 | 586 | self.assertEqual(msg, b"hello world!\n")
|
585 | 587 |
|
| 588 | + @unittest.skipIf(sys.platform == 'win32', "Don't have pipes") |
| 589 | + def test_read_all_from_pipe_reader(self): |
| 590 | + # See Tulip issue 168. This test is derived from the example |
| 591 | + # subprocess_attach_read_pipe.py, but we configure the |
| 592 | + # StreamReader's limit so that twice it is less than the size |
| 593 | + # of the data writter. Also we must explicitly attach a child |
| 594 | + # watcher to the event loop. |
| 595 | + |
| 596 | + watcher = asyncio.get_child_watcher() |
| 597 | + watcher.attach_loop(self.loop) |
| 598 | + |
| 599 | + code = """\ |
| 600 | +import os, sys |
| 601 | +fd = int(sys.argv[1]) |
| 602 | +os.write(fd, b'data') |
| 603 | +os.close(fd) |
| 604 | +""" |
| 605 | + rfd, wfd = os.pipe() |
| 606 | + args = [sys.executable, '-c', code, str(wfd)] |
| 607 | + |
| 608 | + pipe = open(rfd, 'rb', 0) |
| 609 | + reader = asyncio.StreamReader(loop=self.loop, limit=1) |
| 610 | + protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop) |
| 611 | + transport, _ = self.loop.run_until_complete( |
| 612 | + self.loop.connect_read_pipe(lambda: protocol, pipe)) |
| 613 | + |
| 614 | + proc = self.loop.run_until_complete( |
| 615 | + asyncio.create_subprocess_exec(*args, pass_fds={wfd}, loop=self.loop)) |
| 616 | + self.loop.run_until_complete(proc.wait()) |
| 617 | + |
| 618 | + os.close(wfd) |
| 619 | + data = self.loop.run_until_complete(reader.read(-1)) |
| 620 | + self.assertEqual(data, b'data') |
| 621 | + |
586 | 622 |
|
587 | 623 | if __name__ == '__main__':
|
588 | 624 | unittest.main()
|
0 commit comments