From eb08d10e88cec2ef4eed7cfcfa8bf86b84b02097 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 12 Nov 2020 02:13:03 +0000 Subject: [PATCH] Added failing tests to remember to implement connect_timeout --- tests/test_connection.py | 24 ++++++++++++++++++++++++ tests/test_connection_async.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 126865376..80f7f0b5c 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1,7 +1,10 @@ import gc +import time +import socket import pytest import logging import weakref +from threading import Thread import psycopg3 from psycopg3 import Connection, Notify @@ -30,6 +33,27 @@ def test_connect_bad(): Connection.connect("dbname=nosuchdb") +@pytest.mark.slow +@pytest.mark.xfail +def test_connect_timeout(): + s = socket.socket(socket.AF_INET) + s.bind(("", 0)) + port = s.getsockname()[1] + s.listen(0) + + def closer(): + time.sleep(1.5) + s.close() + + Thread(target=closer).start() + + t0 = time.time() + with pytest.raises(psycopg3.DatabaseError): + Connection.connect(host="localhost", port=port, connect_timeout=1) + elapsed = time.time() - t0 + assert elapsed == pytest.approx(1.0, abs=0.05) + + def test_close(conn): assert not conn.closed conn.close() diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 86dc0345c..4c0a8da62 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -1,5 +1,8 @@ import gc +import time +import socket import pytest +import asyncio import logging import weakref @@ -18,7 +21,6 @@ async def test_connect(dsn): assert conn.pgconn.status == conn.ConnStatus.OK -@pytest.mark.asyncio async def test_connect_bad(): with pytest.raises(psycopg3.OperationalError): await AsyncConnection.connect("dbname=nosuchdb") @@ -33,6 +35,32 @@ async def test_connect_str_subclass(dsn): assert conn.pgconn.status == conn.ConnStatus.OK +@pytest.mark.slow +@pytest.mark.xfail +async def test_connect_timeout(): + s = socket.socket(socket.AF_INET) + s.bind(("", 0)) + port = s.getsockname()[1] + s.listen(0) + + async def closer(): + await asyncio.sleep(1.5) + s.close() + + async def connect(): + t0 = time.time() + with pytest.raises(psycopg3.DatabaseError): + await AsyncConnection.connect( + host="localhost", port=port, connect_timeout=1 + ) + nonlocal elapsed + elapsed = time.time() - t0 + + elapsed = 0 + await asyncio.wait([closer(), connect()]) + assert elapsed == pytest.approx(1.0, abs=0.05) + + async def test_close(aconn): assert not aconn.closed await aconn.close() -- 2.47.2