Skip to content

Commit

Permalink
fix: Move port selection into the filelock. (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Dec 21, 2022
1 parent e9d5f0f commit 76c7ed3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@ jobs:
python-versions: '["3.7", "3.8", "3.9", "3.10"]'
lint: false

test:
uses: ./.github/workflows/lint_and_test.yml
with:
install: make install
lint: false
sqlalchemy-versions: '["1.4.39"]'
python-versions: '["3.10"]'

test-and-lint:
uses: ./.github/workflows/lint_and_test.yml
with:
install: make install
sqlalchemy-versions: '["1.4.39"]'
python-versions: '["3.7", "3.8", "3.9", "3.10"]'
python-versions: '["3.7", "3.8", "3.9"]'
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest-mock-resources"
version = "2.6.3"
version = "2.6.4"
description = "A pytest plugin for easily instantiating reproducible mock resources."
authors = [
"Omar Khan <oakhan3@gmail.com>",
Expand Down Expand Up @@ -119,6 +119,7 @@ markers = [
]
filterwarnings = [
"error",
"ignore:There is no current event loop:DeprecationWarning",
]

[build-system]
Expand Down
32 changes: 14 additions & 18 deletions src/pytest_mock_resources/container/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ def get_container(pytestconfig, config, *, retries=DEFAULT_RETRIES, interval=DEF
# we will need to know whether it's been created already or not.
container = None

if config.port is None:
config.set("port", unused_tcp_port())

run_kwargs = dict(
publish=[(dest, source) for source, dest in config.ports().items()],
envs=config.environment(),
name=container_name(config.name, config.port),
)

try:
if multiprocess_safe_mode:
from filelock import FileLock
Expand All @@ -59,9 +50,7 @@ def get_container(pytestconfig, config, *, retries=DEFAULT_RETRIES, interval=DEF
# wait for the container one process at a time
with FileLock(str(fn)):
container = wait_for_container(
config.check_fn,
run_args=(config.image,),
run_kwargs=run_kwargs,
config,
retries=retries,
interval=interval,
)
Expand All @@ -70,9 +59,7 @@ def get_container(pytestconfig, config, *, retries=DEFAULT_RETRIES, interval=DEF

else:
container = wait_for_container(
config.check_fn,
run_args=(config.image,),
run_kwargs=run_kwargs,
config,
retries=retries,
interval=interval,
)
Expand All @@ -84,16 +71,25 @@ def get_container(pytestconfig, config, *, retries=DEFAULT_RETRIES, interval=DEF
container.kill()


def wait_for_container(
check_fn, *, run_args, run_kwargs, retries=DEFAULT_RETRIES, interval=DEFAULT_INTERVAL
):
def wait_for_container(config, *, retries=DEFAULT_RETRIES, interval=DEFAULT_INTERVAL):
"""Wait for evidence that the container is up and healthy.
The caller must provide a `check_fn` which should `raise ContainerCheckFailed` if
it finds that the container is not yet up.
"""
from python_on_whales import docker

if config.port is None:
config.set("port", unused_tcp_port())

check_fn = config.check_fn
run_args = (config.image,)
run_kwargs = dict(
publish=[(dest, source) for source, dest in config.ports().items()],
envs=config.environment(),
name=container_name(config.name, config.port),
)

try:
from python_on_whales.exceptions import DockerException
except ImportError: # pragma: no cover
Expand Down

0 comments on commit 76c7ed3

Please sign in to comment.