From bcf4f55e080570a0925be0995467aa5807f7d881 Mon Sep 17 00:00:00 2001 From: Benjy Weinberger Date: Wed, 21 Dec 2022 00:39:49 -0500 Subject: [PATCH] Choose distinct debug adapter ports in different tests. (#17837) Prevents a port collision error if the two test files happen to run concurrently. --- pants.toml | 1 + .../goals/pytest_runner_integration_test.py | 2 ++ .../goals/run_python_source_integration_test.py | 2 ++ src/python/pants/testutil/debug_adapter_util.py | 16 ++++++++++++++++ 4 files changed, 21 insertions(+) create mode 100644 src/python/pants/testutil/debug_adapter_util.py diff --git a/pants.toml b/pants.toml index 47de44b4b87..2f25ef35eda 100644 --- a/pants.toml +++ b/pants.toml @@ -175,6 +175,7 @@ extra_requirements.add = [ "pygments", ] lockfile = "3rdparty/python/pytest.lock" +execution_slot_var = "TEST_EXECUTION_SLOT" [test] extra_env_vars = [ diff --git a/src/python/pants/backend/python/goals/pytest_runner_integration_test.py b/src/python/pants/backend/python/goals/pytest_runner_integration_test.py index 9f649a00b86..c5dd1747007 100644 --- a/src/python/pants/backend/python/goals/pytest_runner_integration_test.py +++ b/src/python/pants/backend/python/goals/pytest_runner_integration_test.py @@ -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, @@ -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"}) diff --git a/src/python/pants/backend/python/goals/run_python_source_integration_test.py b/src/python/pants/backend/python/goals/run_python_source_integration_test.py index 5bc3640bdc2..5de50859a10 100644 --- a/src/python/pants/backend/python/goals/run_python_source_integration_test.py +++ b/src/python/pants/backend/python/goals/run_python_source_integration_test.py @@ -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 @@ -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" diff --git a/src/python/pants/testutil/debug_adapter_util.py b/src/python/pants/testutil/debug_adapter_util.py new file mode 100644 index 00000000000..2abd9c9883b --- /dev/null +++ b/src/python/pants/testutil/debug_adapter_util.py @@ -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)