Skip to content
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
7 changes: 7 additions & 0 deletions caldav/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,17 @@ def _get_test_server_config(

for server in _collect_test_servers(name, config_file):
was_already_started = server._started
# Servers from caldav.testing (pip-installed caldav) have no session lock
claim = getattr(server, "claim_server", None)
claimed = False
try:
# Another process using this server: try the next one rather than wait
claimed = bool(claim and claim(server.name, 0))
server.start()
except Exception as e:
logging.warning("Failed to start test server %s: %s", server.name, e)
if claimed:
server.release_server(server.name)
continue
logging.info("Using test server: %s at %s", server.name, server.url)
return _test_server_to_params(server, was_already_started)
Expand Down
4 changes: 4 additions & 0 deletions docs/design/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ Known downsides: no Unreleased section visible on master (consider `sphinxcontri
## An error response with a non-XML body labelled as XML

A response whose `Content-Type` says XML but whose body is not XML (an HTML error page sent as `application/xml`) makes `DAVResponse.__init__` raise an lxml parse error, on any status. The caller never gets the `DAVError` (with `body` and `preconditions`, https://github.com/python-caldav/caldav/issues/756) that the status calls for. Only the async client's 401/403 path was fixed (https://github.com/python-caldav/caldav/issues/738). Fix: tolerate a parse failure on a 4xx/5xx response and keep the raw body. Found in the review of the #756 fix.

## Search-cache delay misses writes from other clients

`tests/search_cache_delay.py` only resets its clock on writes through the one wrapped client. Writes through another `DAVClient` in the same test (e.g. the scheduling users' clients) are not counted, so a filtered search right after one may wait less than the server's cache lag, and get flaky on servers like Purelymail or Bedework. Fix: wrap `DAVClient.request` at class level for the duration of the test, or document the limitation. Found in the review of https://github.com/python-caldav/caldav/pull/776.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ build-backend = "hatchling.build"

[tool.hatch.version]
source = "vcs"
# Only v<digit>* tags are releases. The default describe matches any tag
# containing a digit, so a non-release tag with a number in it could
# become the version and break the build.
raw-options = { scm = { git = { describe_command = "git describe --dirty --tags --long --abbrev=40 --match v[0-9]*" } } }

[tool.hatch.build.hooks.vcs]
version-file = "caldav/_version.py"
Expand Down
24 changes: 24 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ pytest -k "test_to_utc"
pytest tests/test_caldav_unit.py
```

### Parallel Test Runs

Test runs share the servers (fixed ports, users and calendar names), so
two runs against the same server would clobber each other. Each process
takes an exclusive per-server lock (`$TMPDIR/caldav-test-server-<name>.session.lock`)
when it first starts using a server and holds it until it exits. A second run
against the same server waits; runs against different servers proceed in
parallel:

```bash
pytest tests/test_caldav.py -k Baikal &
pytest tests/test_caldav.py -k Nextcloud &
```

The lock file lives in the temp directory, so runs only see each other's
locks when they share `TMPDIR`.

The wait is capped at `PYTHON_CALDAV_TEST_SERVER_LOCK_TIMEOUT` seconds
(default 1800), as two runs taking servers in different orders could
otherwise deadlock. After that, the tests for that server fail at once,
naming the pid holding the lock; the run continues with the other servers.
`get_davclient()` with `PYTHON_CALDAV_USE_TEST_SERVER` does not wait at all:
it moves on to the next available server.

## Test Configuration

Test configuration uses YAML or JSON files. The configuration loader searches
Expand Down
132 changes: 124 additions & 8 deletions tests/test_servers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
- DockerTestServer: For servers that run in Docker containers
"""

import os
import threading
from abc import ABC, abstractmethod
from collections.abc import Iterator
from contextlib import contextmanager
from typing import Any
from pathlib import Path
from typing import IO, Any

try:
import niquests as requests
Expand All @@ -21,6 +24,22 @@
DEFAULT_HTTP_TIMEOUT = 5
MAX_STARTUP_WAIT_SECONDS = 60
STARTUP_POLL_INTERVAL = 0.05
SESSION_LOCK_TIMEOUT_SECONDS = 1800

# Per-server session locks held by this process, keyed by server name.
# flock conflicts between two opens even within one process, so each
# lock file is opened only once.
_session_locks: dict[str, IO[str]] = {}
# Servers whose lock timed out; later claims fail at once instead of
# waiting again for every remaining test.
_session_lock_failures: dict[str, RuntimeError] = {}
_session_locks_mutex = threading.Lock()


def _lock_path(name: str, suffix: str = "") -> Path:
import tempfile

return Path(tempfile.gettempdir()) / f"caldav-test-server-{name.lower()}{suffix}.lock"


class TestServer(ABC):
Expand Down Expand Up @@ -106,11 +125,97 @@ def features(self) -> Any:

return resolve_features(self.config.get("features", []))

@staticmethod
def claim_server(name: str, timeout: float | None = None) -> bool:
"""Take an exclusive lock on server ``name`` for the rest of this process.

Test runs sharing a server would clobber each other's calendars (and
one run's teardown may stop a container another run still uses), so
a second process waits here until the first one exits. Runs against
different servers proceed in parallel. The OS drops the lock when
the process dies, so a crashed run leaves nothing stale behind.

Waits at most ``timeout`` seconds (default
``$PYTHON_CALDAV_TEST_SERVER_LOCK_TIMEOUT``, or 30 minutes) - two runs
taking servers in different orders could otherwise deadlock. A
timeout is remembered, so the remaining tests for that server fail
at once - except with ``timeout=0``, a mere probe.

Returns True if the lock was taken just now, False if this process
already held it. Anything learned about the server before a fresh
claim may be stale, since its previous user may have stopped it.

Without ``fcntl`` (Windows) nothing is locked between processes.
"""
import time

try:
import fcntl
except ImportError:
fcntl = None # type: ignore[assignment]

with _session_locks_mutex:
if name in _session_locks:
return False
if name in _session_lock_failures:
raise _session_lock_failures[name]
if timeout is None:
timeout = float(
os.environ.get(
"PYTHON_CALDAV_TEST_SERVER_LOCK_TIMEOUT", SESSION_LOCK_TIMEOUT_SECONDS
)
)
lock_file = open(_lock_path(name, ".session"), "a+")
Comment thread
github-code-quality[bot] marked this conversation as resolved.
Fixed
try:
deadline = time.monotonic() + timeout
announced = False
while fcntl:
try:
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except BlockingIOError:
lock_file.seek(0)
holder = lock_file.read().strip() or "unknown"
if time.monotonic() > deadline:
error = RuntimeError(
f"Test server {name} is in use by pid {holder}; "
f"gave up after {timeout:.0f}s"
)
if timeout:
_session_lock_failures[name] = error
raise error from None
if not announced:
print(f"[WAIT] {name} is in use by pid {holder} - waiting")
announced = True
time.sleep(1)
lock_file.truncate(0)
lock_file.write(str(os.getpid()))
lock_file.flush()
except BaseException:
lock_file.close()
raise
_session_locks[name] = lock_file
return True

@staticmethod
def release_server(name: str) -> None:
"""Give up a claim taken by ``claim_server()``, letting other runs have the server."""
with _session_locks_mutex:
lock_file = _session_locks.pop(name, None)
if lock_file:
lock_file.close()

def claim(self) -> bool:
"""``claim_server()`` for this server."""
return self.claim_server(self.name)

@abstractmethod
def start(self) -> None:
"""
Start the server if not already running.

Implementations must call ``self.claim()`` first.

This method should be idempotent - calling it multiple times
should not cause issues.

Expand Down Expand Up @@ -206,15 +311,15 @@ def get_server_params(self) -> dict[str, Any]:
# Pass through scheduling_users if configured (for TestScheduling generation)
if "scheduling_users" in self.config:
params["scheduling_users"] = self.config["scheduling_users"]
# setup is always needed, since start() also takes the session lock
params["setup"] = lambda _: self.start()
# Check if server is already running (either started by us or externally)
already_running = self._started or self.is_accessible()
if already_running:
# Server is already running - mark as started but don't add teardown
# to avoid stopping a server that was running before tests started
self._started = True
else:
# Server needs to be started - add setup/teardown callbacks
params["setup"] = lambda _: self.start()
params["teardown"] = lambda _: self.stop()
return params

Expand Down Expand Up @@ -340,7 +445,13 @@ def start(self) -> None:
Raises:
RuntimeError: If Docker is not available or container fails to start
"""
if self._started or self.is_accessible():
if self.claim():
# Whatever was seen before the lock (e.g. at collection) may be
# stale: the previous holder may have stopped the container.
self._started = False
if self._started:
return
if self.is_accessible():
self._started = True # Mark as started even if already running
# Don't set _started_by_us - we didn't start it this time
print(f"[OK] {self.name} is already running")
Expand Down Expand Up @@ -371,11 +482,8 @@ def _lock(self) -> Iterator[None]:
repo share the same containers and ports.
"""
import fcntl
import tempfile
from pathlib import Path

lock_path = Path(tempfile.gettempdir()) / f"caldav-test-server-{self.name.lower()}.lock"
with open(lock_path, "w") as lock_file:
with open(_lock_path(self.name), "w") as lock_file:
fcntl.flock(lock_file, fcntl.LOCK_EX)
try:
yield
Expand Down Expand Up @@ -412,6 +520,13 @@ def _start_container(self) -> None:

raise RuntimeError(f"{self.name} failed to start after {MAX_STARTUP_WAIT_SECONDS}s")

def get_server_params(self) -> dict[str, Any]:
params = super().get_server_params()
# A run that waited for the lock may have restarted a container that
# was up at collection; stop() leaves alone one we did not start.
params["teardown"] = lambda _: self.stop()
return params

def stop(self) -> None:
"""Stop the Docker container and cleanup.

Expand Down Expand Up @@ -482,6 +597,7 @@ def start(self) -> None:
# No accessibility check here - the caldav library handles URL
# normalization, RFC6764 discovery, and will provide proper error
# messages if the server is unreachable
self.claim()
self._started = True

def stop(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_servers/embedded.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def is_accessible(self) -> bool:
return self._core.is_accessible()

def start(self) -> None:
self.claim()
self._core.start()
self._started = self._core._started

Expand Down Expand Up @@ -73,6 +74,7 @@ def is_accessible(self) -> bool:
return self._core.is_accessible()

def start(self) -> None:
self.claim()
self._core.start()
self._started = self._core._started

Expand Down
Loading
Loading