diff --git a/tests/test_tcp.py b/tests/test_tcp.py index 70965f7f..12ba3efa 100644 --- a/tests/test_tcp.py +++ b/tests/test_tcp.py @@ -131,6 +131,32 @@ def test_create_server_2(self): with self.assertRaisesRegex(ValueError, 'nor sock were specified'): self.loop.run_until_complete(self.loop.create_server(object)) + def test_create_server_3(self): + ''' check ephemeral port can be used ''' + + async def start_server_ephemeral_ports(): + + for port_sentinel in [0, None]: + srv = await self.loop.create_server( + asyncio.Protocol, + '127.0.0.1', port_sentinel, + family=socket.AF_INET) + + srv_socks = srv.sockets + self.assertTrue(srv_socks) + + host, port = srv_socks[0].getsockname() + self.assertNotEqual(0, port) + + self.loop.call_soon(srv.close) + await srv.wait_closed() + + # Check that the server cleaned-up proxy-sockets + for srv_sock in srv_socks: + self.assertEqual(srv_sock.fileno(), -1) + + self.loop.run_until_complete(start_server_ephemeral_ports()) + def test_create_connection_1(self): CNT = 0 TOTAL_CNT = 100 diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index fdb9c866..f72314aa 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -497,6 +497,8 @@ cdef class Loop: int proto, int flags, int unpack): + if port is None: + port = 0 if isinstance(port, str): port = port.encode() elif isinstance(port, int):