|
2 | 2 |
|
3 | 3 | import gc |
4 | 4 | import os |
| 5 | +import queue |
5 | 6 | import socket |
6 | 7 | import sys |
| 8 | +import threading |
7 | 9 | import unittest |
8 | 10 | from unittest import mock |
9 | 11 | try: |
@@ -632,6 +634,47 @@ def test_streamreaderprotocol_constructor(self): |
632 | 634 | protocol = asyncio.StreamReaderProtocol(reader) |
633 | 635 | self.assertIs(protocol._loop, self.loop) |
634 | 636 |
|
| 637 | + def test_drain_raises(self): |
| 638 | + # See http://bugs.python.org/issue25441 |
| 639 | + |
| 640 | + # This test should not use asyncio for the mock server; the |
| 641 | + # whole point of the test is to test for a bug in drain() |
| 642 | + # where it never gives up the event loop but the socket is |
| 643 | + # closed on the server side. |
| 644 | + |
| 645 | + q = queue.Queue() |
| 646 | + |
| 647 | + def server(): |
| 648 | + # Runs in a separate thread. |
| 649 | + sock = socket.socket() |
| 650 | + sock.bind(('localhost', 0)) |
| 651 | + sock.listen(1) |
| 652 | + addr = sock.getsockname() |
| 653 | + q.put(addr) |
| 654 | + clt, _ = sock.accept() |
| 655 | + clt.close() |
| 656 | + |
| 657 | + @asyncio.coroutine |
| 658 | + def client(host, port): |
| 659 | + reader, writer = yield from asyncio.open_connection(host, port, loop=self.loop) |
| 660 | + while True: |
| 661 | + writer.write(b"foo\n") |
| 662 | + yield from writer.drain() |
| 663 | + |
| 664 | + # Start the server thread and wait for it to be listening. |
| 665 | + thread = threading.Thread(target=server) |
| 666 | + thread.setDaemon(True) |
| 667 | + thread.start() |
| 668 | + addr = q.get() |
| 669 | + |
| 670 | + # Should not be stuck in an infinite loop. |
| 671 | + with self.assertRaises((ConnectionResetError, BrokenPipeError)): |
| 672 | + self.loop.run_until_complete(client(*addr)) |
| 673 | + |
| 674 | + # Clean up the thread. (Only on success; on failure, it may |
| 675 | + # be stuck in accept().) |
| 676 | + thread.join() |
| 677 | + |
635 | 678 | def test___repr__(self): |
636 | 679 | stream = asyncio.StreamReader(loop=self.loop) |
637 | 680 | self.assertEqual("<StreamReader>", repr(stream)) |
|
0 commit comments