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
93 changes: 93 additions & 0 deletions .github/scripts/run_platform_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Run one of exactly two exhaustive, deterministic Windows non-slow shards."""

import argparse
import hashlib
import json
from collections import Counter
from pathlib import Path

import pytest


def validate_partition(nodeids, partitions):
"""Fail before execution if any collected test is missing, repeated, or unknown."""
if len(nodeids) != len(set(nodeids)):
raise pytest.UsageError("platform collection contains duplicate node IDs")
if len(partitions) != 2 or any(not part for part in partitions):
raise pytest.UsageError("platform partition requires exactly two nonempty shards")
if Counter(node for part in partitions for node in part) != Counter(nodeids):
raise pytest.UsageError("platform shards must cover every selected test exactly once")


def partition_nodeids(nodeids):
# Alternating individual cases, rather than whole directories/files, spreads
# expensive parameterized SQLite tests across both fixed-size CI jobs.
ordered = sorted(nodeids)
partitions = [ordered[0::2], ordered[1::2]]
validate_partition(nodeids, partitions)
return partitions


class PlatformPartition:
def __init__(self, shard, manifest=Path("platform-partition.json")):
if shard not in (0, 1):
raise pytest.UsageError("platform shard must be 0 or 1")
self.shard = shard
self.manifest = manifest

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_collection_modifyitems(self, config, items):
# Run after ordinary selection hooks, particularly pytest's -m filter.
# Both jobs collect the entire suite before assigning any non-slow case.
yield
nodeids = [item.nodeid for item in items]
partitions = partition_nodeids(nodeids)
# Keep validation independent of construction so future partition changes
# cannot silently drop/duplicate tests before modifying pytest's items.
validate_partition(nodeids, partitions)
selected = set(partitions[self.shard])
omitted = [item for item in items if item.nodeid not in selected]
items[:] = [item for item in items if item.nodeid in selected]
config.hook.pytest_deselected(items=omitted)
ordered = sorted(nodeids)
self.manifest.write_text(
json.dumps(
{
"schema_version": 1,
"selection": "not slow",
"shard": self.shard,
"shard_count": 2,
"collection_sha256": hashlib.sha256(
"\n".join(ordered).encode("utf-8")
).hexdigest(),
"full_collection": ordered,
"partitions": partitions,
"selected": partitions[self.shard],
},
indent=2,
ensure_ascii=False,
)
+ "\n",
encoding="utf-8",
)


def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--shard", required=True, type=int, choices=(0, 1))
parser.add_argument("--collect-only", action="store_true")
args = parser.parse_args(argv)
pytest_args = [
"-m",
"not slow",
"--junitxml=platform-tests.xml",
"-o",
"junit_family=legacy",
]
if args.collect_only:
pytest_args.append("--collect-only")
return pytest.main(pytest_args, plugins=[PlatformPartition(args.shard)])


if __name__ == "__main__":
raise SystemExit(main())
64 changes: 64 additions & 0 deletions .github/workflows/platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Platform compatibility

on:
push:
branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize, reopened, edited]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: platform-${{ github.ref }}
cancel-in-progress: true

jobs:
platform:
name: Non-slow (${{ matrix.os }}, Python 3.13, ${{ matrix.partition }})
runs-on: ${{ matrix.os }}
timeout-minutes: 20
strategy:
fail-fast: false
max-parallel: 2
matrix:
include:
- os: windows-latest
partition: shard-0
command: python .github/scripts/run_platform_tests.py --shard 0
- os: windows-latest
partition: shard-1
command: python .github/scripts/run_platform_tests.py --shard 1
- os: macos-latest
partition: all
command: python -m pytest -m "not slow" --junitxml=platform-tests.xml -o junit_family=legacy
env:
# Keep multilingual source/fixture text and subprocess output deterministic.
PYTHONUTF8: "1"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.13"
cache: pip
cache-dependency-path: pyproject.toml
- name: Install test dependencies and console launcher
run: python -m pip install -e ".[dev]"
- name: Verify executable fixture provenance before collection
run: python -m pytest tests/test_fixture_provenance.py
- name: Run exhaustive non-slow partition
# No coverage denominator or scale matrix here. Resource-warning gates
# remain enabled; required Chromium interaction runs in the Linux job.
run: ${{ matrix.command }}
- name: Upload platform test results
if: always()
uses: actions/upload-artifact@v4
with:
name: platform-tests-${{ matrix.os }}-py313-${{ matrix.partition }}
path: |
platform-tests.xml
platform-partition.json
if-no-files-found: warn
retention-days: 7
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dist/
.coverage
.coverage.*
coverage.xml
platform-tests.xml
platform-partition.json
htmlcov/
.pytest_cache/
.ruff_cache/
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ For the regular suite use `pytest -m 'not slow'`. Default-capacity scale and
non-editable installed exporter tests are described in the
[dataset acceptance guide](docs/en/dataset-acceptance.md).

The supplemental platform workflow runs Python 3.13 non-slow tests on macOS and
Windows. Windows uses two deterministic, disjoint shards of the full non-slow
collection; macOS runs it in one job. All three jobs retain a 20-minute limit,
with at most two running concurrently. Each Windows shard validates exhaustive
coverage before execution and uploads its full collection/partition manifest
alongside JUnit results. No tests are skipped to meet the time limit. To reproduce
one Windows partition locally, run
`python .github/scripts/run_platform_tests.py --shard 0` (or `--shard 1`);
add `--collect-only` to inspect the partition without executing tests.

### Building distributions

Build from a fresh checkout or a new worktree at the intended commit, with no
Expand Down
8 changes: 8 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ black --check . && ruff check . # 检查格式和 lint
常规套件可用 `pytest -m 'not slow'`。默认容量规模及非 editable 安装包导出测试见
[数据集验收指南](docs/zh/dataset-acceptance.md)。

补充的平台工作流在 macOS 和 Windows 上运行 Python 3.13 非 slow 测试。
Windows 将完整的非 slow 集合确定性地划分为两个互不重叠的分片;macOS 使用一个任务。
三个任务均保持 20 分钟上限,最多同时运行两个。每个 Windows 分片在执行前验证
划分无遗漏、无重复,并随 JUnit 结果上传完整的收集及分片清单。不会为了满足时间上限
而跳过测试。本地复现一个 Windows 分片可运行
`python .github/scripts/run_platform_tests.py --shard 0`(或 `--shard 1`);
增加 `--collect-only` 可仅检查划分,不执行测试。

### 构建分发包

请从目标提交的全新 checkout 或新 worktree 构建,确保没有已有的 `build/` 和
Expand Down
28 changes: 22 additions & 6 deletions benchmarks/dataset_import_baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import logging
import os
import platform
import resource
import sqlite3
import subprocess
import sys
Expand All @@ -23,13 +22,23 @@
from contextlib import closing
from pathlib import Path

try:
import resource
except ImportError: # Windows has no POSIX getrusage module.
resource = None

MODES = ("single-db", "pt-snap-native-v2", "compatibility-v1")


def rss_kib(raw: float, system: str) -> float:
return raw / 1024 if system == "darwin" else raw


def process_peak_rss() -> float | None:
"""Return this process's high-water RSS, or explicitly unavailable on Windows."""
return None if resource is None else resource.getrusage(resource.RUSAGE_SELF).ru_maxrss


def source_identity(source: Path, expected: str) -> dict:
if source.is_symlink() or source.resolve() != source or not source.is_file():
raise ValueError("Source must be an explicit canonical regular file")
Expand Down Expand Up @@ -158,7 +167,8 @@ def no_rebuild(*_args, **_kwargs):
service._msinsight_backend.dump_to_dataset = no_rebuild
result = service.import_snapshot(options)
wall = time.perf_counter() - start
usage = resource.getrusage(resource.RUSAGE_SELF) # this process ONLY, before analysis
peak_rss = process_peak_rss() # this process ONLY, before analysis
peak_rss_kib = rss_kib(peak_rss, sys.platform) if peak_rss is not None else None
if result.reused != (args.phase == "reuse"):
raise AssertionError("Cold/reuse measurement did not follow requested cache path")
members = [result.db_path] if result.db_path.is_file() else sorted(result.db_path.rglob("*.db"))
Expand Down Expand Up @@ -225,10 +235,16 @@ def no_rebuild(*_args, **_kwargs):
"mode": args.mode,
"phase": args.phase,
"import_wall_seconds": wall,
"isolated_import_process_peak_rss_raw": usage.ru_maxrss,
"isolated_import_process_peak_rss_kib": rss_kib(usage.ru_maxrss, sys.platform),
"isolated_import_process_peak_rss_mib": rss_kib(usage.ru_maxrss, sys.platform) / 1024,
"rss_scope": "RUSAGE_SELF fresh child high-water through import, before queries; includes interpreter/imports",
"isolated_import_process_peak_rss_raw": peak_rss,
"isolated_import_process_peak_rss_kib": peak_rss_kib,
"isolated_import_process_peak_rss_mib": (
peak_rss_kib / 1024 if peak_rss_kib is not None else None
),
"rss_scope": (
"RUSAGE_SELF fresh child high-water through import, before queries; includes interpreter/imports"
if peak_rss is not None
else "unavailable: this platform does not provide POSIX resource.getrusage"
),
"phase_timings": dict(phases),
"phase_timing_scope": "named wrapped functions, inclusive; sharded replay includes SQLite writes/backfills, NOT parser-only",
"phase_rss": {
Expand Down
5 changes: 5 additions & 0 deletions src/pt_snap_cli/core/snapshot_import_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def close_backup() -> None:
raise
os.replace(tmp_db_path, db_path)
except OSError as exc:
# Windows cannot unlink a file while its mkstemp handle is open.
# Publication has not committed, so release the secure backup
# before removing it; the original destination is still intact.
close_backup()
backup_fd = None
if backup_path is not None:
try:
backup_path.unlink(missing_ok=True)
Expand Down
8 changes: 8 additions & 0 deletions tests/core/test_context_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def test_same_mtime_replacement_refreshes_cached_context(self, tmp_path: Path, r
conn.commit()
conn.close()
os.utime(replacement, ns=(original_stat.st_atime_ns, original_stat.st_mtime_ns))
if os.name == "nt":
# Windows forbids replacing an open SQLite file. Release the handle
# but retain the cache entry so signature invalidation is still tested.
first.close()
os.replace(replacement, db_path)

second = cache.get(db_path)
Expand Down Expand Up @@ -121,6 +125,10 @@ def test_replaced_database_redetects_callstack_layout(self, tmp_path: Path, requ
conn.execute("CREATE TABLE trace_entry_0 (id INTEGER PRIMARY KEY, callstackId INTEGER)")
conn.commit()
conn.close()
if os.name == "nt":
# Windows forbids replacing an open SQLite file. Release the handle
# but retain the cache entry so signature invalidation is still tested.
first.close()
os.replace(replacement, db_path)

second = cache.get(db_path)
Expand Down
5 changes: 4 additions & 1 deletion tests/core/test_dataset_content_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ def test_immutable_v1_entry_cannot_seed_default_reuse(tmp_path, monkeypatch):
root = make_dataset(tmp_path / "dataset", devices=(0,), slices=1)
monkeypatch.setenv("PT_SNAP_DATASET_CACHE", "immutable")
resolver.DatasetResolver().inspect(root)
assert not resolver._VALIDATED[root].content_verified
if os.name == "posix":
assert not resolver._VALIDATED[root].content_verified
else:
assert root not in resolver._VALIDATED
monkeypatch.delenv("PT_SNAP_DATASET_CACHE")
calls = []
original = resolver.validate_dataset
Expand Down
1 change: 1 addition & 0 deletions tests/core/test_dataset_id_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def million_event_dataset(tmp_path_factory):
return root


@pytest.mark.slow
@pytest.mark.parametrize("template", ["event", "allocation"])
@pytest.mark.parametrize("direction", ["ASC", "DESC"])
def test_million_offset_keeps_fetched_work_constant(million_event_dataset, template, direction):
Expand Down
6 changes: 5 additions & 1 deletion tests/core/test_dataset_import_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def isolate(tmp_path, monkeypatch):
def test_directory_descriptors_closed_on_success_and_failure(tmp_path, monkeypatch, fail):
service, options, first, old, focus = saved_state(tmp_path)
descriptors = []
identities = []
original = SplitService._stage_identity

def track(path):
result = original(path)
identities.append(result)
if result[2] is not None:
descriptors.append(result[2])
return result
Expand All @@ -43,7 +45,9 @@ def track(path):
assert_preserved(tmp_path, first, old, focus)
else:
service.import_snapshot(options)
assert descriptors
assert identities # Windows uses path identity rather than directory descriptors.
assert bool(descriptors) == (os.name != "nt")
assert list(first.db_path.parent.iterdir()) == [first.db_path]
for fd in descriptors:
with pytest.raises(OSError):
os.fstat(fd)
Expand Down
10 changes: 6 additions & 4 deletions tests/core/test_dataset_integrity_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ def test_cold_warm_actual_hash_bytes_and_validator_counts(artifact, mode, work):
for state in ("cold", "warm"):
work.clear()
assert resolver.DatasetResolver().inspect(root) is not None
passes = cold_member_passes if state == "cold" else int(mode == "strong")
# Immutable reuse requires POSIX change-time tokens; Windows revalidates.
cold_validation = state == "cold" or (mode == "immutable" and os.name != "posix")
passes = cold_member_passes if cold_validation else int(mode == "strong")
for path in members:
assert work[("hashes", path)] == passes
assert work[("bytes", path)] == passes * path.stat().st_size
manifest_passes = 2 if state == "cold" else int(mode == "strong")
manifest_passes = 2 if cold_validation else int(mode == "strong")
assert work[("bytes", manifest)] == manifest_passes * manifest.stat().st_size
assert work["validators"] == int(state == "cold")
assert work["validators"] == int(cold_validation)
evidence[state] = {
"hash_read_bytes": sum(
v for k, v in work.items() if isinstance(k, tuple) and k[0] == "bytes"
Expand Down Expand Up @@ -147,7 +149,7 @@ def test_hidden_change_tokens_document_immutable_trust_boundary(artifact, mode,
# Simulate a filesystem concealing ctime too. This violates immutable mode's
# explicit caller contract; that mode must not be described as strong integrity.
monkeypatch.setattr(resolver, "_generation", lambda *args: first.generation)
if mode == "strong":
if mode == "strong" or os.name != "posix":
with pytest.raises(DatabaseSchemaError):
resolver.DatasetResolver().inspect(root)
assert root not in resolver._VALIDATED
Expand Down
7 changes: 5 additions & 2 deletions tests/core/test_dataset_lifecycle_scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ def build(database, items, maximum):
original(database, items, maximum)
path = Path(database.connection.execute("PRAGMA database_list").fetchone()[2])
observed.append(database.connection)
assert os.stat(path.parent).st_mode & 0o777 == 0o700
assert os.stat(path).st_mode & 0o777 == 0o600
# Windows exposes read-only flags, not POSIX permission bits. The
# actual route, byte bound and handle/temporary cleanup run everywhere.
if os.name != "nt":
assert os.stat(path.parent).st_mode & 0o777 == 0o700
assert os.stat(path).st_mode & 0o777 == 0o600
assert path.stat().st_size <= maximum
assert database.connection.getlimit(sqlite3.SQLITE_LIMIT_ATTACHED) == limit

Expand Down
Loading
Loading