Skip to content

Commit

Permalink
Choose distinct debug adapter ports in different tests. (#17837)
Browse files Browse the repository at this point in the history
Prevents a port collision error if the two test files happen
to run concurrently.
  • Loading branch information
benjyw authored Dec 21, 2022
1 parent 1283216 commit bcf4f55
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ extra_requirements.add = [
"pygments",
]
lockfile = "3rdparty/python/pytest.lock"
execution_slot_var = "TEST_EXECUTION_SLOT"

[test]
extra_env_vars = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from pants.engine.rules import Get, rule
from pants.engine.target import Target
from pants.engine.unions import UnionRule
from pants.testutil.debug_adapter_util import debugadapter_port_for_testing
from pants.testutil.python_interpreter_selection import (
all_major_minor_python_versions,
skip_unless_python27_and_python3_present,
Expand Down Expand Up @@ -108,6 +109,7 @@ def _configure_pytest_runner(
args = [
"--backend-packages=pants.backend.python",
f"--source-root-patterns={SOURCE_ROOT}",
f"--debug-adapter-port={debugadapter_port_for_testing()}",
*(extra_args or ()),
]
rule_runner.set_options(args, env=env, env_inherit={"PATH", "PYENV_ROOT", "HOME"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from pants.engine.process import InteractiveProcess
from pants.engine.rules import QueryRule
from pants.engine.target import Target
from pants.testutil.debug_adapter_util import debugadapter_port_for_testing
from pants.testutil.pants_integration_test import run_pants
from pants.testutil.rule_runner import RuleRunner, mock_console

Expand Down Expand Up @@ -186,6 +187,7 @@ def my_file():
"--backend-packages=pants.backend.python",
"--backend-packages=pants.backend.codegen.protobuf.python",
"--source-root-patterns=['src_root1', 'src_root2']",
f"--debug-adapter-port={debugadapter_port_for_testing()}",
*(
(
"--python-default-run-goal-use-sandbox"
Expand Down
16 changes: 16 additions & 0 deletions src/python/pants/testutil/debug_adapter_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os


def debugadapter_port_for_testing() -> int:
"""Return a unique-per-concurrent-process debug adapter port.
Use this in Pants's (and plugins') own tests to avoid collisions.
Assumes that the env var TEST_EXECUTION_SLOT has been set. If not, all tests
will use the same port, and collisions may occur.
"""
execution_slot = os.environ.get("TEST_EXECUTION_SLOT", "0")
return 22000 + int(execution_slot)

0 comments on commit bcf4f55

Please sign in to comment.