From 63cf3ebb7310bd55a90b3804d1d97d1e9ef80a74 Mon Sep 17 00:00:00 2001 From: xiaoxinw Date: Thu, 11 Jan 2024 10:25:32 +0800 Subject: [PATCH 1/2] Raise exact mysql error when the packet is an error packet --- aiomysql/connection.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/aiomysql/connection.py b/aiomysql/connection.py index 3520dfcc..0a0e2c8e 100644 --- a/aiomysql/connection.py +++ b/aiomysql/connection.py @@ -615,20 +615,21 @@ async def _read_packet(self, packet_type=MysqlPacket): ' Date: Fri, 22 Mar 2024 14:45:39 +0800 Subject: [PATCH 2/2] Add a test case to cover the changes --- tests/test_connection.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_connection.py b/tests/test_connection.py index c0c1be3d..c6705c5a 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1,6 +1,7 @@ import asyncio import gc import os +import time import pytest @@ -263,3 +264,17 @@ async def test_commit_during_multi_result(connection_creator): await cur.execute("SELECT 3;") resp = await cur.fetchone() assert resp[0] == 3 + + +@pytest.mark.run_loop +async def test_connect_timeout_error(mysql_params, loop): + async def mock_block_code(): + # block a little longer then default connect_timeout variable (10s) + time.sleep(11) + + async def connect(): + mysql_params.pop("ssl", None) + await aiomysql.connect(loop=loop, **mysql_params) + + with pytest.raises(aiomysql.OperationalError): + await asyncio.gather(connect(), mock_block_code())