pytest_plugins = (
"tests.fix_db",
"tests.fix_pq",
+ "tests.fix_dns",
"tests.fix_mypy",
"tests.fix_faker",
"tests.fix_proxy",
--- /dev/null
+import asyncio
+import socket
+
+import pytest
+
+
+@pytest.fixture
+def fake_resolve(monkeypatch):
+ """
+ Fixture to return known name from name resolution.
+ """
+ fake_hosts = {
+ "localhost": ["127.0.0.1"],
+ "foo.com": ["1.1.1.1"],
+ "qux.com": ["2.2.2.2"],
+ "dup.com": ["3.3.3.3", "3.3.3.4"],
+ "alot.com": [f"4.4.4.{n}" for n in range(10, 30)],
+ }
+
+ def family(host):
+ return socket.AF_INET6 if ":" in host else socket.AF_INET
+
+ def fake_getaddrinfo(host, port, *args, **kwargs):
+ assert isinstance(port, int) or (isinstance(port, str) and port.isdigit())
+ try:
+ addrs = fake_hosts[host]
+ except KeyError:
+ raise OSError(f"unknown test host: {host}")
+ else:
+ return [
+ (family(addr), socket.SOCK_STREAM, 6, "", (addr, port))
+ for addr in addrs
+ ]
+
+ _patch_gai(monkeypatch, fake_getaddrinfo)
+
+
+@pytest.fixture
+def fail_resolve(monkeypatch):
+ """
+ Fixture to fail any name resolution.
+ """
+
+ def fail_getaddrinfo(host, port, **kwargs):
+ pytest.fail(f"shouldn't try to resolve {host}")
+
+ _patch_gai(monkeypatch, fail_getaddrinfo)
+
+
+def _patch_gai(monkeypatch, f):
+ monkeypatch.setattr(socket, "getaddrinfo", f)
+ try:
+ loop = asyncio.get_running_loop()
+ except RuntimeError:
+ pass
+ else:
+
+ async def af(*args, **kwargs):
+ return f(*args, **kwargs)
+
+ monkeypatch.setattr(loop, "getaddrinfo", af)
from .test_connection import conninfo_params_timeout
from .test_connection import testctx # noqa: F401 # fixture
from .test_adapt import make_bin_dumper, make_dumper
-from .test_conninfo import fake_resolve # noqa: F401
pytestmark = pytest.mark.anyio
aconn.cancel()
-async def test_resolve_hostaddr_conn(monkeypatch, fake_resolve): # noqa: F811
+async def test_resolve_hostaddr_conn(monkeypatch, fake_resolve):
got = []
def fake_connect_gen(conninfo, **kwargs):
params = conninfo_to_dict(conninfo)
timeout = timeout_from_conninfo(params)
assert timeout == want
-
-
-@pytest.fixture
-async def fake_resolve(monkeypatch):
- fake_hosts = {
- "localhost": ["127.0.0.1"],
- "foo.com": ["1.1.1.1"],
- "qux.com": ["2.2.2.2"],
- "dup.com": ["3.3.3.3", "3.3.3.4"],
- "alot.com": [f"4.4.4.{n}" for n in range(10, 30)],
- }
-
- def family(host):
- return socket.AF_INET6 if ":" in host else socket.AF_INET
-
- async def fake_getaddrinfo(host, port, **kwargs):
- assert isinstance(port, int) or (isinstance(port, str) and port.isdigit())
- try:
- addrs = fake_hosts[host]
- except KeyError:
- raise OSError(f"unknown test host: {host}")
- else:
- return [
- (family(addr), socket.SOCK_STREAM, 6, "", (addr, port))
- for addr in addrs
- ]
-
- monkeypatch.setattr(asyncio.get_running_loop(), "getaddrinfo", fake_getaddrinfo)
-
-
-@pytest.fixture
-async def fail_resolve(monkeypatch):
- async def fail_getaddrinfo(host, port, **kwargs):
- pytest.fail(f"shouldn't try to resolve {host}")
-
- monkeypatch.setattr(asyncio.get_running_loop(), "getaddrinfo", fail_getaddrinfo)
import psycopg
from psycopg.conninfo import conninfo_to_dict
-from .test_conninfo import fake_resolve # noqa: F401 # fixture
-
@pytest.mark.usefixtures("fake_resolve")
async def test_resolve_hostaddr_conn(aconn_cls, monkeypatch):