import gc
+import time
+import socket
import pytest
import logging
import weakref
+from threading import Thread
import psycopg3
from psycopg3 import Connection, Notify
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()
import gc
+import time
+import socket
import pytest
+import asyncio
import logging
import weakref
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")
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()