Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelize the unit tests #16206

Merged
merged 9 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -e
# correct.
export LC_ALL="${ENCODING:-en_US.UTF-8}"
export LANG="${ENCODING:-en_US.UTF-8}"

export COVERAGE_PROCESS_START="$(pwd)/pyproject.toml"
COMMAND_ARGS="$@"

# Test the postgres connection
Expand Down Expand Up @@ -38,5 +38,6 @@ mkdir -p warehouse/static/dist/

# Actually run our tests.
python -m coverage run -m pytest --strict-markers $COMMAND_ARGS
python -m coverage combine
python -m coverage html --show-contexts
python -m coverage report -m --fail-under 100
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ services:
- ./htmlcov:/opt/warehouse/src/htmlcov:z
- ./setup.cfg:/opt/warehouse/src/setup.cfg:z
- ./pyproject.toml:/opt/warehouse/src/pyproject.toml:z
# Site customization shim to enable multiprocess coverage collection in tests.
# See: https://coverage.readthedocs.io/en/latest/subprocess.html
- ./sitecustomize.py:/opt/warehouse/src/sitecustomize.py:z
- packages:/var/opt/warehouse/packages
- packages-archive:/var/opt/warehouse/packages-archive
- sponsorlogos:/var/opt/warehouse/sponsorlogos
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ omit = [
# And again, tiny shim code.
"warehouse/celery.py",
]
# We run multiple processes in parallel, so we need to make sure that we don't
# write to the same file at the same time.
parallel = true

[tool.coverage.report]
exclude_lines = [
Expand Down Expand Up @@ -87,6 +90,7 @@ addopts = [
"--disable-socket",
"--allow-hosts=localhost,::1,notdatadog,stripe",
"--durations=20",
"--numprocesses=auto",
]
cache_dir = "dev/.pytest_cache"
norecursedirs = ['build', 'dist', 'node_modules', '*.egg-info', '.state requirements']
Expand Down
1 change: 1 addition & 0 deletions requirements/tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pytest-icdiff
pytest-postgresql>=3.1.3,<7.0.0
pytest-randomly
pytest-socket
pytest-xdist
pytz
responses>=0.5.1
webtest
9 changes: 9 additions & 0 deletions requirements/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ coverage==7.5.4 \
--hash=sha256:e564c2cf45d2f44a9da56f4e3a26b2236504a496eb4cb0ca7221cd4cc7a9aca9 \
--hash=sha256:ed550e7442f278af76d9d65af48069f1fb84c9f745ae249c1a183c1e9d1b025c
# via -r requirements/tests.in
execnet==2.0.2 \
--hash=sha256:88256416ae766bc9e8895c76a87928c0012183da3cc4fc18016e6f050e025f41 \
--hash=sha256:cc59bc4423742fd71ad227122eb0dd44db51efb3dc4095b45ac9a08c770096af
# via pytest-xdist
factory-boy==3.3.0 \
--hash=sha256:a2cdbdb63228177aa4f1c52f4b6d83fab2b8623bf602c7dedd7eb83c0f69c04c \
--hash=sha256:bc76d97d1a65bbd9842a6d722882098eb549ec8ee1081f9fb2e8ff29f0c300f1
Expand Down Expand Up @@ -238,6 +242,7 @@ pytest==8.2.2 \
# pytest-postgresql
# pytest-randomly
# pytest-socket
# pytest-xdist
pytest-icdiff==0.9 \
--hash=sha256:13aede616202e57fcc882568b64589002ef85438046f012ac30a8d959dac8b75 \
--hash=sha256:efee0da3bd1b24ef2d923751c5c547fbb8df0a46795553fba08ef57c3ca03d82
Expand All @@ -254,6 +259,10 @@ pytest-socket==0.7.0 \
--hash=sha256:71ab048cbbcb085c15a4423b73b619a8b35d6a307f46f78ea46be51b1b7e11b3 \
--hash=sha256:7e0f4642177d55d317bbd58fc68c6bd9048d6eadb2d46a89307fa9221336ce45
# via -r requirements/tests.in
pytest-xdist==3.5.0 \
--hash=sha256:cbb36f3d67e0c478baa57fa4edc8843887e0f6cfc42d677530a36d7472b32d8a \
--hash=sha256:d075629c7e00b611df89f490a5063944bee7a4362a5ff11c7cc7824a03dfce24
# via -r requirements/tests.in
python-dateutil==2.9.0.post0 \
--hash=sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3 \
--hash=sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427
Expand Down
21 changes: 21 additions & 0 deletions sitecustomize.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: This file could probably live somewhere other than the repo root, as long as it gets copied into the right place at container time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Since this file is being mounted in docker-compose.yml , we could place it in dev/ and mount it from there, but that would skip it from being run in CI.
I'm not opposed to leaving it where it is for now, but wanted to call that possibility out.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Site customization shim to enable multiprocess coverage collection in tests.
# See: https://coverage.readthedocs.io/en/latest/subprocess.html

try:
import coverage

coverage.process_startup()
except ImportError:
pass
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ def cli():


@pytest.fixture(scope="session")
def database(request):
def database(request, worker_id):
config = get_config(request)
pg_host = config.get("host")
pg_port = config.get("port") or os.environ.get("PGPORT", 5432)
pg_user = config.get("user")
pg_db = config.get("db", "tests")
pg_db = f"tests-{worker_id}"
pg_version = config.get("version", 16.1)

janitor = DatabaseJanitor(
Expand Down