|
| 1 | +# A test server for driver Happy Eyeballs behavior. See README.md for more information. |
| 2 | + |
| 3 | +import argparse |
| 4 | +import asyncio |
| 5 | +import socket |
| 6 | +import sys |
| 7 | +import platform |
| 8 | + |
| 9 | +if platform.system() not in ["Darwin", "Windows"]: |
| 10 | + print(f'Only macOS (Darwin) and Windows are supported, but got: {platform.system()}', file=sys.stderr) |
| 11 | + exit(1) |
| 12 | +parser = argparse.ArgumentParser( |
| 13 | + prog='server', |
| 14 | + description='Test server for happy eyeballs', |
| 15 | +) |
| 16 | +parser.add_argument('-c', '--control', default=10036, type=int, metavar='PORT', help='control port') |
| 17 | +parser.add_argument('--wait', action='store_true', help='wait for a running server to be ready') |
| 18 | +parser.add_argument('--stop', action='store_true', help='stop a running server') |
| 19 | +args = parser.parse_args() |
| 20 | + |
| 21 | +PREFIX='happy eyeballs server' |
| 22 | + |
| 23 | +async def control_server(): |
| 24 | + shutdown = asyncio.Event() |
| 25 | + srv = await asyncio.start_server(lambda reader, writer: on_control_connected(reader, writer, shutdown), 'localhost', args.control) |
| 26 | + print(f'{PREFIX}: listening for control connections on {args.control}', file=sys.stderr) |
| 27 | + async with srv: |
| 28 | + await shutdown.wait() |
| 29 | + print(f'{PREFIX}: all done', file=sys.stderr) |
| 30 | + |
| 31 | +async def on_control_connected(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, shutdown: asyncio.Event): |
| 32 | + # Read the control request byte |
| 33 | + data = await reader.readexactly(1) |
| 34 | + if data == b'\x04': |
| 35 | + print(f'{PREFIX}: ========================', file=sys.stderr) |
| 36 | + print(f'{PREFIX}: request for delayed IPv4', file=sys.stderr) |
| 37 | + slow = 'IPv4' |
| 38 | + elif data == b'\x06': |
| 39 | + print(f'{PREFIX}: ========================', file=sys.stderr) |
| 40 | + print(f'{PREFIX}: request for delayed IPv6', file=sys.stderr) |
| 41 | + slow = 'IPv6' |
| 42 | + elif data == b'\xF0': |
| 43 | + writer.write(b'\x01') |
| 44 | + await writer.drain() |
| 45 | + writer.close() |
| 46 | + await writer.wait_closed() |
| 47 | + return |
| 48 | + elif data == b'\xFF': |
| 49 | + print(f'{PREFIX}: shutting down', file=sys.stderr) |
| 50 | + writer.close() |
| 51 | + await writer.wait_closed() |
| 52 | + shutdown.set() |
| 53 | + return |
| 54 | + else: |
| 55 | + print(f'Unexpected control byte: {data}', file=sys.stderr) |
| 56 | + exit(1) |
| 57 | + |
| 58 | + # Bind the test ports but do not yet start accepting connections |
| 59 | + connected = asyncio.Event() |
| 60 | + on_ipv4_connected = lambda reader, writer: on_test_connected('IPv4', writer, b'\x04', connected, slow) |
| 61 | + on_ipv6_connected = lambda reader, writer: on_test_connected('IPv6', writer, b'\x06', connected, slow) |
| 62 | + # port 0: pick random unused port |
| 63 | + srv4 = await asyncio.start_server(on_ipv4_connected, 'localhost', 0, family=socket.AF_INET, start_serving=False) |
| 64 | + srv6 = await asyncio.start_server(on_ipv6_connected, 'localhost', 0, family=socket.AF_INET6, start_serving=False) |
| 65 | + ipv4_port = srv4.sockets[0].getsockname()[1] |
| 66 | + ipv6_port = srv6.sockets[0].getsockname()[1] |
| 67 | + print(f'{PREFIX}: [slow {slow}] bound for IPv4 on {ipv4_port}', file=sys.stderr) |
| 68 | + print(f'{PREFIX}: [slow {slow}] bound for IPv6 on {ipv6_port}', file=sys.stderr) |
| 69 | + |
| 70 | + # Reply to control request with success byte and test server ports |
| 71 | + writer.write(b'\x01') |
| 72 | + writer.write(ipv4_port.to_bytes(2, 'big')) |
| 73 | + writer.write(ipv6_port.to_bytes(2, 'big')) |
| 74 | + await writer.drain() |
| 75 | + writer.close() |
| 76 | + await writer.wait_closed() |
| 77 | + |
| 78 | + # Start test servers listening in parallel |
| 79 | + # Hold a reference to the tasks so they aren't GC'd |
| 80 | + test_tasks = [ |
| 81 | + asyncio.create_task(test_listen('IPv4', srv4, data == b'\x04', connected, slow)), |
| 82 | + asyncio.create_task(test_listen('IPv6', srv6, data == b'\x06', connected, slow)), |
| 83 | + ] |
| 84 | + await asyncio.wait(test_tasks) |
| 85 | + |
| 86 | + # Wait for the test servers to shut down |
| 87 | + srv4.close() |
| 88 | + srv6.close() |
| 89 | + close_tasks = [ |
| 90 | + asyncio.create_task(srv4.wait_closed()), |
| 91 | + asyncio.create_task(srv6.wait_closed()), |
| 92 | + ] |
| 93 | + await asyncio.wait(close_tasks) |
| 94 | + |
| 95 | + print(f'{PREFIX}: [slow {slow}] connection complete, test ports closed', file=sys.stderr) |
| 96 | + print(f'{PREFIX}: ========================', file=sys.stderr) |
| 97 | + |
| 98 | +async def test_listen(name: str, srv, delay: bool, connected: asyncio.Event, slow: str): |
| 99 | + # Both connections are delayed; the slow one is delayed by more than the fast one; this |
| 100 | + # ensures that the client is comparing timing and not simply choosing an immediate success |
| 101 | + # over a connection denied. |
| 102 | + if delay: |
| 103 | + print(f'{PREFIX}: [slow {slow}] delaying {name} connections', file=sys.stderr) |
| 104 | + await asyncio.sleep(2.0) |
| 105 | + else: |
| 106 | + await asyncio.sleep(1.0) |
| 107 | + async with srv: |
| 108 | + await srv.start_serving() |
| 109 | + print(f'{PREFIX}: [slow {slow}] accepting {name} connections', file=sys.stderr) |
| 110 | + # Terminate this test server when either test server has handled a request |
| 111 | + await connected.wait() |
| 112 | + |
| 113 | +async def on_test_connected(name: str, writer: asyncio.StreamWriter, payload: bytes, connected: asyncio.Event, slow: str): |
| 114 | + print(f'{PREFIX}: [slow {slow}] connected on {name}', file=sys.stderr) |
| 115 | + writer.write(payload) |
| 116 | + await writer.drain() |
| 117 | + writer.close() |
| 118 | + await writer.wait_closed() |
| 119 | + connected.set() |
| 120 | + |
| 121 | +async def stop_server(): |
| 122 | + control_r, control_w = await asyncio.open_connection('localhost', args.control) |
| 123 | + control_w.write(b'\xFF') |
| 124 | + await control_w.drain() |
| 125 | + control_w.close() |
| 126 | + await control_w.wait_closed() |
| 127 | + |
| 128 | +async def wait_for_server(): |
| 129 | + while True: |
| 130 | + try: |
| 131 | + control_r, control_w = await asyncio.open_connection('localhost', args.control) |
| 132 | + except OSError as e: |
| 133 | + print(f'{PREFIX}: failed ({e}), will retry', file=sys.stderr) |
| 134 | + await asyncio.sleep(1) |
| 135 | + continue |
| 136 | + break |
| 137 | + control_w.write(b'\xF0') |
| 138 | + await control_w.drain() |
| 139 | + data = await control_r.read(1) |
| 140 | + if data != b'\x01': |
| 141 | + print(f'{PREFIX}: expected byte 1, got {data}', file=sys.stderr) |
| 142 | + exit(1) |
| 143 | + print(f'{PREFIX}: happy eyeballs server ready on port {args.control}', file=sys.stderr) |
| 144 | + |
| 145 | + |
| 146 | +if args.stop: |
| 147 | + asyncio.run(stop_server()) |
| 148 | +elif args.wait: |
| 149 | + asyncio.run(wait_for_server()) |
| 150 | +else: |
| 151 | + asyncio.run(control_server()) |
0 commit comments