From: Daniele Varrazzo Date: Wed, 5 Jan 2022 00:53:07 +0000 (+0100) Subject: Add deaf_port fixture and base timeout tests on it X-Git-Tag: pool-3.1~44 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f504ecfdb36deb43e521d4d84dd6e192562f71b1;p=thirdparty%2Fpsycopg.git Add deaf_port fixture and base timeout tests on it --- diff --git a/tests/fix_proxy.py b/tests/fix_proxy.py index 83fce5c9f..ba39786fc 100644 --- a/tests/fix_proxy.py +++ b/tests/fix_proxy.py @@ -32,6 +32,16 @@ def proxy(dsn): p.stop() +@pytest.fixture +def deaf_port(dsn): + """Return a port number with a socket open but not answering""" + with socket.socket(socket.AF_INET) as s: + s.bind(("", 0)) + port = s.getsockname()[1] + s.listen(0) + yield port + + class Proxy: """ Proxy a Postgres service for testing purpose. diff --git a/tests/test_connection.py b/tests/test_connection.py index 3581a122d..dac8557c8 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1,10 +1,8 @@ import sys import time -import socket import pytest import logging import weakref -from threading import Thread from typing import Any, List if sys.version_info >= (3, 8): @@ -47,22 +45,10 @@ def test_connect_bad(): @pytest.mark.slow @pytest.mark.timing -@pytest.mark.skipif(sys.platform == "win32", reason="connect() hangs on Win32") -def test_connect_timeout(): - s = socket.socket(socket.AF_INET) - s.bind(("localhost", 0)) - port = s.getsockname()[1] - s.listen(0) - - def closer(): - time.sleep(1.5) - s.close() - - Thread(target=closer).start() - +def test_connect_timeout(deaf_port): t0 = time.time() with pytest.raises(psycopg.OperationalError, match="timeout expired"): - Connection.connect(host="localhost", port=port, connect_timeout=1) + Connection.connect(host="localhost", port=deaf_port, connect_timeout=1) elapsed = time.time() - t0 assert elapsed == pytest.approx(1.0, abs=0.05) diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index c047b4646..beacc3e7d 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -1,5 +1,4 @@ import time -import socket import pytest import asyncio import logging @@ -44,28 +43,13 @@ async def test_connect_str_subclass(dsn): @pytest.mark.slow @pytest.mark.timing -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() - - elapsed: float = 0 - - async def connect(): - t0 = time.time() - with pytest.raises(psycopg.OperationalError, match="timeout expired"): - await AsyncConnection.connect( - host="localhost", port=port, connect_timeout=1 - ) - nonlocal elapsed - elapsed = time.time() - t0 - - await asyncio.gather(closer(), connect()) +async def test_connect_timeout(deaf_port): + t0 = time.time() + with pytest.raises(psycopg.OperationalError, match="timeout expired"): + await AsyncConnection.connect( + host="localhost", port=deaf_port, connect_timeout=1 + ) + elapsed = time.time() - t0 assert elapsed == pytest.approx(1.0, abs=0.05)