Skip to content

Commit

Permalink
connect_timeout fix (#360)
Browse files Browse the repository at this point in the history
* fix the python 3.7 compatibility

* Fixes #257
  • Loading branch information
terrycain authored Dec 4, 2018
1 parent f712904 commit 2eb8533
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 11 additions & 4 deletions aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,21 @@ async def _connect(self):
try:
if self._unix_socket and self._host in ('localhost', '127.0.0.1'):
self._reader, self._writer = await \
asyncio.open_unix_connection(self._unix_socket,
loop=self._loop)
asyncio.wait_for(
asyncio.open_unix_connection(
self._unix_socket,
loop=self._loop),
timeout=self.connect_timeout)
self.host_info = "Localhost via UNIX socket: " + \
self._unix_socket
else:
self._reader, self._writer = await \
asyncio.open_connection(self._host, self._port,
loop=self._loop)
asyncio.wait_for(
asyncio.open_connection(
self._host,
self._port,
loop=self._loop),
timeout=self.connect_timeout)
self._set_keep_alive()
self.host_info = "socket %s:%d" % (self._host, self._port)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def fill_my_cnf(self):
with open(path2, 'w') as f2:
f2.write(tmpl.format_map(self.__dict__))

@run_until_complete
def test_connect_timeout(self):
# All exceptions are caught and raised as operational errors
with self.assertRaises(aiomysql.OperationalError):
yield from self.connect(connect_timeout=0.000000000001)

@run_until_complete
def test_config_file(self):
self.fill_my_cnf()
Expand Down

0 comments on commit 2eb8533

Please sign in to comment.