Skip to content

Commit

Permalink
Run xdist tests runs with -n auto flag - closes #1081
Browse files Browse the repository at this point in the history
    Make sure we attempt to run tests on not used port for xdist - closes #872
  • Loading branch information
fizyk committed Feb 11, 2025
1 parent aff3d56 commit 76e85f5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dockerised-postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
uses: fizyk/actions-reuse/.github/actions/[email protected]
with:
python-version: ${{ matrix.python-version }}
command: pytest -n 0 -k docker --postgresql-host=localhost --postgresql-port 5433 --postgresql-password=postgres --cov-report=xml:coverage-docker.xml
command: pytest -n 0 --max-worker-restart 0 -k docker --postgresql-host=localhost --postgresql-port 5433 --postgresql-password=postgres --cov-report=xml:coverage-docker.xml
- name: Upload coverage to Codecov
uses: codecov/[email protected]
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/single-postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ jobs:
uses: fizyk/actions-reuse/.github/actions/[email protected]
with:
python-version: ${{ matrix.python-version }}
command: py.test -svv -n 0 --postgresql-exec="/usr/lib/postgresql/${{ inputs.postgresql }}/bin/pg_ctl" -k "not docker" --cov-report=xml
command: py.test -svv -p no:xdist --postgresql-exec="/usr/lib/postgresql/${{ inputs.postgresql }}/bin/pg_ctl" -k "not docker" --cov-report=xml
- name: Run xdist test
uses: fizyk/actions-reuse/.github/actions/[email protected]
with:
python-version: ${{ matrix.python-version }}
command: py.test -n 1 --postgresql-exec="/usr/lib/postgresql/${{ inputs.postgresql }}/bin/pg_ctl" -k "not docker" --cov-report=xml:coverage-xdist.xml
command: py.test -n auto --max-worker-restart 0 --postgresql-exec="/usr/lib/postgresql/${{ inputs.postgresql }}/bin/pg_ctl" -k "not docker" --cov-report=xml:coverage-xdist.xml
- uses: actions/upload-artifact@v4
if: failure()
with:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/1081.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run xdist test with -n auto, turn off xdist for xdist-less runs
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespaces = false

[tool.pytest.ini_options]
xfail_strict=true
addopts = "--max-worker-restart=0 --showlocals --verbose --cov"
addopts = "--showlocals --verbose --cov"
testpaths = "tests"
pytester_example_dir = "tests/examples"
norecursedirs = "examples"
Expand Down
30 changes: 26 additions & 4 deletions pytest_postgresql/factories/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import platform
import subprocess
from pathlib import Path
from typing import Callable, Iterator, List, Optional, Tuple, Union
from typing import Callable, Iterator, List, Optional, Tuple, Union, Iterable

import port_for
import pytest
Expand Down Expand Up @@ -54,9 +54,9 @@ def _pg_exe(executable: Optional[str], config: PostgresqlConfigDict) -> str:
return postgresql_ctl


def _pg_port(port: Optional[PortType], config: PostgresqlConfigDict) -> int:
def _pg_port(port: Optional[PortType], config: PostgresqlConfigDict, excluded_ports: Iterable[int]) -> int:
"""User specified port, otherwise find an unused port from config."""
pg_port = get_port(port) or get_port(config["port"])
pg_port = get_port(port, excluded_ports) or get_port(config["port"], excluded_ports)
assert pg_port is not None
return pg_port

Expand Down Expand Up @@ -122,7 +122,29 @@ def postgresql_proc_fixture(
pg_dbname = dbname or config["dbname"]
pg_load = load or config["load"]
postgresql_ctl = _pg_exe(executable, config)
pg_port = _pg_port(port, config)
port_path = tmp_path_factory.getbasetemp()
if hasattr(request.config, "workerinput"):
port_path = tmp_path_factory.getbasetemp().parent

pg_port = 0
n = 0
used_ports: set[int] = set()
while True:
try:
pg_port = _pg_port(port, config, used_ports)
if pg_port in used_ports:
# if it's marked as already used, then it means it's not randomly selected. No point in looking further.
break
used_ports.add(pg_port)
with (port_path / f"postgresql-{pg_port}.port").open("x") as port_file:
port_file.write(f"pg_port {pg_port}\n")
break
except FileExistsError:
if n >= 5:
# Well, end user will get AlreadyRunning error bit later which is more informative, than we can give here.
break
n += 1

tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.fixturename}")
datadir, logfile_path = _prepare_dir(tmpdir, str(pg_port))

Expand Down
4 changes: 2 additions & 2 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_executor_init_with_password(
config = get_config(request)
monkeypatch.setenv("LC_ALL", locale)
pg_exe = process._pg_exe(None, config)
port = process._pg_port(-1, config)
port = process._pg_port(-1, config, [])
tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.node.name}")
datadir, logfile_path = process._prepare_dir(tmpdir, port)
executor = PostgreSQLExecutor(
Expand All @@ -103,7 +103,7 @@ def test_executor_init_bad_tmp_path(
r"""Test init with \ and space chars in the path."""
config = get_config(request)
pg_exe = process._pg_exe(None, config)
port = process._pg_port(-1, config)
port = process._pg_port(-1, config, [])
tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.node.name}") / r"a bad\path/"
tmpdir.mkdir(exist_ok=True)
datadir, logfile_path = process._prepare_dir(tmpdir, port)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_template_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
dbname="stories_templated",
)


@pytest.mark.xdist_group(name="template_database")
@pytest.mark.parametrize("_", range(5))
def test_template_database(postgresql_template: Connection, _: int) -> None:
"""Check that the database structure gets recreated out of a template."""
Expand Down

0 comments on commit 76e85f5

Please sign in to comment.