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

Commit

Permalink
Add a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Nov 7, 2016
1 parent 152ea2f commit 2abb370
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 6 additions & 3 deletions asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,10 +1055,13 @@ def connect_accepted_socket(self, protocol_factory, sock, *, ssl=None):
This method is a coroutine. When completed, the coroutine
returns a (transport, protocol) pair.
"""
if sock.family not in {socket.AF_INET, socket.AF_INET6,
socket.AF_UNIX}:
socket_families = {socket.AF_INET, socket.AF_INET6}
if hasattr(socket, 'AF_UNIX'):
socket_families.add(socket.AF_UNIX)
if sock.family not in socket_families:
raise ValueError(
'A TCP Stream Socket was expected, got {!r}'.format(sock))
'A TCP/Unix Stream Socket was expected, got {!r}'.format(sock))

transport, protocol = yield from self._create_connection_transport(
sock, protocol_factory, ssl, '', server_side=True)
if self._debug:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,24 @@ def test_create_connection_host_port_sock(self):
MyProto, 'example.com', 80, sock=object())
self.assertRaises(ValueError, self.loop.run_until_complete, coro)

@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
def test_create_connection_wrong_sock(self):
sock = socket.socket(socket.AF_UNIX)
with sock:
coro = self.loop.create_connection(MyProto, sock=sock)
with self.assertRaisesRegex(ValueError,
'A TCP Stream Socket was expected'):
self.loop.run_until_complete(coro)

@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'no Unix sockets')
def test_create_server_wrong_sock(self):
sock = socket.socket(socket.AF_UNIX)
with sock:
coro = self.loop.create_server(MyProto, sock=sock)
with self.assertRaisesRegex(ValueError,
'A TCP Stream Socket was expected'):
self.loop.run_until_complete(coro)

def test_create_connection_no_host_port_sock(self):
coro = self.loop.create_connection(MyProto)
self.assertRaises(ValueError, self.loop.run_until_complete, coro)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,9 @@ def client():
conn, _ = lsock.accept()
proto = MyProto(loop=loop)
proto.loop = loop
f = loop.create_task(
loop.run_until_complete(
loop.connect_accepted_socket(
(lambda : proto), conn, ssl=server_ssl))
(lambda: proto), conn, ssl=server_ssl))
loop.run_forever()
proto.transport.close()
lsock.close()
Expand Down

0 comments on commit 2abb370

Please sign in to comment.