|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import socket |
| 4 | +import uvloop |
| 5 | +import ssl |
| 6 | +import warnings |
| 7 | + |
| 8 | +from asyncio import test_utils |
| 9 | +from uvloop import _testbase as tb |
| 10 | + |
| 11 | + |
| 12 | +class MyDatagramProto(asyncio.DatagramProtocol): |
| 13 | + done = None |
| 14 | + |
| 15 | + def __init__(self, loop=None): |
| 16 | + self.state = 'INITIAL' |
| 17 | + self.nbytes = 0 |
| 18 | + if loop is not None: |
| 19 | + self.done = asyncio.Future(loop=loop) |
| 20 | + |
| 21 | + def connection_made(self, transport): |
| 22 | + self.transport = transport |
| 23 | + assert self.state == 'INITIAL', self.state |
| 24 | + self.state = 'INITIALIZED' |
| 25 | + |
| 26 | + def datagram_received(self, data, addr): |
| 27 | + assert self.state == 'INITIALIZED', self.state |
| 28 | + self.nbytes += len(data) |
| 29 | + |
| 30 | + def error_received(self, exc): |
| 31 | + assert self.state == 'INITIALIZED', self.state |
| 32 | + raise exc |
| 33 | + |
| 34 | + def connection_lost(self, exc): |
| 35 | + assert self.state == 'INITIALIZED', self.state |
| 36 | + self.state = 'CLOSED' |
| 37 | + if self.done: |
| 38 | + self.done.set_result(None) |
| 39 | + |
| 40 | + |
| 41 | +class _TestUDP: |
| 42 | + def test_create_datagram_endpoint_addrs(self): |
| 43 | + class TestMyDatagramProto(MyDatagramProto): |
| 44 | + def __init__(inner_self): |
| 45 | + super().__init__(loop=self.loop) |
| 46 | + |
| 47 | + def datagram_received(self, data, addr): |
| 48 | + super().datagram_received(data, addr) |
| 49 | + self.transport.sendto(b'resp:' + data, addr) |
| 50 | + |
| 51 | + for lc in (('127.0.0.1', 0), None): |
| 52 | + if lc is None and not isinstance(self.loop, uvloop._Loop): |
| 53 | + # TODO This looks like a bug in asyncio -- if no local_addr |
| 54 | + # and no remote_addr are specified, the connection |
| 55 | + # that asyncio creates is not bound anywhere. |
| 56 | + return |
| 57 | + |
| 58 | + with self.subTest(local_addr=lc): |
| 59 | + coro = self.loop.create_datagram_endpoint( |
| 60 | + TestMyDatagramProto, local_addr=lc, family=socket.AF_INET) |
| 61 | + s_transport, server = self.loop.run_until_complete(coro) |
| 62 | + host, port = s_transport.get_extra_info('sockname') |
| 63 | + |
| 64 | + self.assertIsInstance(server, TestMyDatagramProto) |
| 65 | + self.assertEqual('INITIALIZED', server.state) |
| 66 | + self.assertIs(server.transport, s_transport) |
| 67 | + |
| 68 | + coro = self.loop.create_datagram_endpoint( |
| 69 | + lambda: MyDatagramProto(loop=self.loop), |
| 70 | + family=socket.AF_INET, |
| 71 | + remote_addr=None if lc is None else (host, port)) |
| 72 | + transport, client = self.loop.run_until_complete(coro) |
| 73 | + |
| 74 | + self.assertIsInstance(client, MyDatagramProto) |
| 75 | + self.assertEqual('INITIALIZED', client.state) |
| 76 | + self.assertIs(client.transport, transport) |
| 77 | + |
| 78 | + transport.sendto(b'xxx', (host, port) if lc is None else None) |
| 79 | + test_utils.run_until(self.loop, lambda: server.nbytes) |
| 80 | + self.assertEqual(3, server.nbytes) |
| 81 | + test_utils.run_until(self.loop, lambda: client.nbytes) |
| 82 | + |
| 83 | + # received |
| 84 | + self.assertEqual(8, client.nbytes) |
| 85 | + |
| 86 | + # extra info is available |
| 87 | + self.assertIsNotNone(transport.get_extra_info('sockname')) |
| 88 | + |
| 89 | + # close connection |
| 90 | + transport.close() |
| 91 | + self.loop.run_until_complete(client.done) |
| 92 | + self.assertEqual('CLOSED', client.state) |
| 93 | + server.transport.close() |
| 94 | + self.loop.run_until_complete(server.done) |
| 95 | + |
| 96 | + def test_create_datagram_endpoint_sock(self): |
| 97 | + sock = None |
| 98 | + local_address = ('127.0.0.1', 0) |
| 99 | + infos = self.loop.run_until_complete( |
| 100 | + self.loop.getaddrinfo( |
| 101 | + *local_address, type=socket.SOCK_DGRAM)) |
| 102 | + for family, type, proto, cname, address in infos: |
| 103 | + try: |
| 104 | + sock = socket.socket(family=family, type=type, proto=proto) |
| 105 | + sock.setblocking(False) |
| 106 | + sock.bind(address) |
| 107 | + except: |
| 108 | + pass |
| 109 | + else: |
| 110 | + break |
| 111 | + else: |
| 112 | + assert False, 'Can not create socket.' |
| 113 | + |
| 114 | + with sock: |
| 115 | + f = self.loop.create_connection( |
| 116 | + lambda: MyDatagramProto(loop=self.loop), sock=sock) |
| 117 | + tr, pr = self.loop.run_until_complete(f) |
| 118 | + self.assertIsInstance(pr, MyDatagramProto) |
| 119 | + tr.close() |
| 120 | + self.loop.run_until_complete(pr.done) |
| 121 | + |
| 122 | + |
| 123 | +class Test_UV_UDP(_TestUDP, tb.UVTestCase): |
| 124 | + pass |
| 125 | + |
| 126 | + |
| 127 | +class Test_AIO_UDP(_TestUDP, tb.AIOTestCase): |
| 128 | + pass |
0 commit comments