(self)
| 688 | |
| 689 | @socket_helper.skip_unless_bind_unix_socket |
| 690 | def test_start_unix_server(self): |
| 691 | |
| 692 | class MyServer: |
| 693 | |
| 694 | def __init__(self, loop, path): |
| 695 | self.server = None |
| 696 | self.loop = loop |
| 697 | self.path = path |
| 698 | |
| 699 | async def handle_client(self, client_reader, client_writer): |
| 700 | data = await client_reader.readline() |
| 701 | client_writer.write(data) |
| 702 | await client_writer.drain() |
| 703 | client_writer.close() |
| 704 | await client_writer.wait_closed() |
| 705 | |
| 706 | def start(self): |
| 707 | self.server = self.loop.run_until_complete( |
| 708 | asyncio.start_unix_server(self.handle_client, |
| 709 | path=self.path)) |
| 710 | |
| 711 | def handle_client_callback(self, client_reader, client_writer): |
| 712 | self.loop.create_task(self.handle_client(client_reader, |
| 713 | client_writer)) |
| 714 | |
| 715 | def start_callback(self): |
| 716 | start = asyncio.start_unix_server(self.handle_client_callback, |
| 717 | path=self.path) |
| 718 | self.server = self.loop.run_until_complete(start) |
| 719 | |
| 720 | def stop(self): |
| 721 | if self.server is not None: |
| 722 | self.server.close() |
| 723 | self.loop.run_until_complete(self.server.wait_closed()) |
| 724 | self.server = None |
| 725 | |
| 726 | async def client(path): |
| 727 | reader, writer = await asyncio.open_unix_connection(path) |
| 728 | # send a line |
| 729 | writer.write(b"hello world!\n") |
| 730 | # read it back |
| 731 | msgback = await reader.readline() |
| 732 | writer.close() |
| 733 | await writer.wait_closed() |
| 734 | return msgback |
| 735 | |
| 736 | messages = [] |
| 737 | self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) |
| 738 | |
| 739 | # test the server variant with a coroutine as client handler |
| 740 | with test_utils.unix_socket_path() as path: |
| 741 | server = MyServer(self.loop, path) |
| 742 | server.start() |
| 743 | msg = self.loop.run_until_complete( |
| 744 | self.loop.create_task(client(path))) |
| 745 | server.stop() |
| 746 | self.assertEqual(msg, b"hello world!\n") |
| 747 |
nothing calls this directly
no test coverage detected