]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add deaf_port fixture and base timeout tests on it
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 5 Jan 2022 00:53:07 +0000 (01:53 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 5 Jan 2022 01:14:43 +0000 (02:14 +0100)
tests/fix_proxy.py
tests/test_connection.py
tests/test_connection_async.py

index 83fce5c9f10c43274f7b5a1969b8d102f0dfb663..ba39786fc6ade637716ff831b70e30be16299b86 100644 (file)
@@ -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.
index 3581a122d00fda287cf6bf6bc611bec5106a5c24..dac8557c892dd92e448ffcf6ea51f9dbbda0431d 100644 (file)
@@ -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)
 
index c047b4646425774a4cae0bb0694d0e63f1b719b7..beacc3e7d440c49172296723f428b4af7c3a8ac3 100644 (file)
@@ -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)