Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 2abb370

Browse files
committed
Add a few tests
1 parent 152ea2f commit 2abb370

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

asyncio/base_events.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,13 @@ def connect_accepted_socket(self, protocol_factory, sock, *, ssl=None):
10551055
This method is a coroutine. When completed, the coroutine
10561056
returns a (transport, protocol) pair.
10571057
"""
1058-
if sock.family not in {socket.AF_INET, socket.AF_INET6,
1059-
socket.AF_UNIX}:
1058+
socket_families = {socket.AF_INET, socket.AF_INET6}
1059+
if hasattr(socket, 'AF_UNIX'):
1060+
socket_families.add(socket.AF_UNIX)
1061+
if sock.family not in socket_families:
10601062
raise ValueError(
1061-
'A TCP Stream Socket was expected, got {!r}'.format(sock))
1063+
'A TCP/Unix Stream Socket was expected, got {!r}'.format(sock))
1064+
10621065
transport, protocol = yield from self._create_connection_transport(
10631066
sock, protocol_factory, ssl, '', server_side=True)
10641067
if self._debug:

tests/test_base_events.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,24 @@ def test_create_connection_host_port_sock(self):
10401040
MyProto, 'example.com', 80, sock=object())
10411041
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
10421042

1043+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
1044+
def test_create_connection_wrong_sock(self):
1045+
sock = socket.socket(socket.AF_UNIX)
1046+
with sock:
1047+
coro = self.loop.create_connection(MyProto, sock=sock)
1048+
with self.assertRaisesRegex(ValueError,
1049+
'A TCP Stream Socket was expected'):
1050+
self.loop.run_until_complete(coro)
1051+
1052+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
1053+
def test_create_server_wrong_sock(self):
1054+
sock = socket.socket(socket.AF_UNIX)
1055+
with sock:
1056+
coro = self.loop.create_server(MyProto, sock=sock)
1057+
with self.assertRaisesRegex(ValueError,
1058+
'A TCP Stream Socket was expected'):
1059+
self.loop.run_until_complete(coro)
1060+
10431061
def test_create_connection_no_host_port_sock(self):
10441062
coro = self.loop.create_connection(MyProto)
10451063
self.assertRaises(ValueError, self.loop.run_until_complete, coro)

tests/test_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ def client():
791791
conn, _ = lsock.accept()
792792
proto = MyProto(loop=loop)
793793
proto.loop = loop
794-
f = loop.create_task(
794+
loop.run_until_complete(
795795
loop.connect_accepted_socket(
796-
(lambda : proto), conn, ssl=server_ssl))
796+
(lambda: proto), conn, ssl=server_ssl))
797797
loop.run_forever()
798798
proto.transport.close()
799799
lsock.close()

0 commit comments

Comments
 (0)