Skip to content

Commit af2c9b1

Browse files
authored
fix(connection): Raise OperationalError for socket timeouts (#179)
* Raise OperationalError for socket timeouts * add unit test and integrationt test * rectify unit test * rectify integration test for socket_timeout * remove empty config.ini
1 parent 5b94858 commit af2c9b1

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

redshift_connector/core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,11 @@ def get_calling_module() -> str:
656656
self._sock: typing.Optional[typing.BinaryIO] = self._usock.makefile(mode="rwb")
657657
if tcp_keepalive:
658658
self._usock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
659+
660+
except socket.timeout as timeout_error:
661+
self._usock.close()
662+
raise OperationalError("connection time out", timeout_error)
663+
659664
except socket.error as e:
660665
self._usock.close()
661666
raise InterfaceError("communication error", e)

test/integration/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,9 @@ def test_execute_do_parsing_bind_params_when_exist(mocker, db_kwargs, sql, args)
307307
with redshift_connector.connect(**db_kwargs) as conn:
308308
conn.cursor().execute(sql, args)
309309
assert convert_paramstyle_spy.called
310+
311+
def test_socket_timeout(db_kwargs):
312+
db_kwargs["timeout"] = 0
313+
314+
with pytest.raises(redshift_connector.InterfaceError):
315+
redshift_connector.connect(**db_kwargs)

test/unit/test_connection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from collections import deque
33
from decimal import Decimal
44
from unittest.mock import patch
5+
import socket
6+
from unittest import mock
57

68
import pytest # type: ignore
79

@@ -12,6 +14,7 @@
1214
IntegrityError,
1315
InterfaceError,
1416
ProgrammingError,
17+
OperationalError
1518
)
1619
from redshift_connector.config import (
1720
ClientProtocolVersion,
@@ -328,3 +331,9 @@ def test_client_os_version_is_not_present():
328331

329332
with patch("platform.platform", side_effect=Exception("not for you")):
330333
assert mock_connection.client_os_version == "unknown"
334+
335+
def test_socket_timeout_error():
336+
with mock.patch('socket.socket.connect') as mock_socket:
337+
mock_socket.side_effect = (socket.timeout)
338+
with pytest.raises(OperationalError):
339+
Connection(user='mock_user', password='mock_password', host='localhost', port=8080, database='mocked')

0 commit comments

Comments
 (0)