|
| 1 | +"""Cancelled task diagnostics must survive the Python 3.10 Task boundary.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import json |
| 5 | +import time |
| 6 | +from copy import deepcopy |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from pg_perf_bench.benchmark import BenchmarkRunner |
| 11 | +from pg_perf_bench.errors import CommandFailure, exception_evidence |
| 12 | +from tests.test_interrupted_benchmark import partial_report |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize('wait_for', [False, True]) |
| 16 | +def test_cancelled_task_retains_checkpoint_and_partial_report_evidence(tmp_path, wait_for): |
| 17 | + async def scenario(): |
| 18 | + reference = partial_report(cancelled=True) |
| 19 | + started = asyncio.Event() |
| 20 | + command = CommandFailure('pgbench', -15, 'partial secret-value', 'cancelled', 1) |
| 21 | + |
| 22 | + async def child(): |
| 23 | + try: |
| 24 | + started.set() |
| 25 | + await asyncio.Event().wait() |
| 26 | + except asyncio.CancelledError as exc: |
| 27 | + exc.failure = command |
| 28 | + exc.benchmark_run = reference['failed_iteration'] |
| 29 | + exc.completed_runs = reference['benchmark_runs'] |
| 30 | + raise |
| 31 | + |
| 32 | + task = asyncio.create_task(child()) |
| 33 | + await started.wait() |
| 34 | + task.cancel() |
| 35 | + with pytest.raises(asyncio.CancelledError) as caught: |
| 36 | + if wait_for: |
| 37 | + await asyncio.wait_for(task, timeout=2) |
| 38 | + else: |
| 39 | + await task |
| 40 | + error = caught.value |
| 41 | + assert task.cancelled() # Evidence handling must not turn cancellation into success. |
| 42 | + assert exception_evidence(error, 'failure') is command |
| 43 | + results = exception_evidence(error, 'completed_runs') |
| 44 | + assert results == reference['benchmark_runs'] |
| 45 | + path = tmp_path / 'checkpoint.json' |
| 46 | + BenchmarkRunner.write_checkpoint( |
| 47 | + {'checkpoint_path': str(path)}, |
| 48 | + {'password': 'secret-value'}, |
| 49 | + results, |
| 50 | + 'cancelled', |
| 51 | + error, |
| 52 | + ) |
| 53 | + checkpoint = json.loads(path.read_text()) |
| 54 | + assert checkpoint['completed_iterations'] == 1 |
| 55 | + assert checkpoint['failed_iteration'] == reference['failed_iteration'] |
| 56 | + assert checkpoint['failed_command']['returncode'] == -15 |
| 57 | + assert 'partial' in checkpoint['failed_command']['stdout'] |
| 58 | + assert 'secret-value' not in path.read_text() |
| 59 | + report = BenchmarkRunner.partial_report( |
| 60 | + deepcopy(reference), |
| 61 | + { |
| 62 | + 'workload_conf': { |
| 63 | + 'pgbench_iter_name': 'pgbench_clients', |
| 64 | + 'pgbench_iter_list': [8, 16, 64], |
| 65 | + }, |
| 66 | + 'report_conf': {'report_name': 'cancellation'}, |
| 67 | + }, |
| 68 | + error, |
| 69 | + started_at='start', |
| 70 | + started_clock=time.monotonic(), |
| 71 | + ) |
| 72 | + assert report['benchmark_status'] == 'cancelled' |
| 73 | + assert report['benchmark_runs'] == reference['benchmark_runs'] |
| 74 | + assert report['failed_iteration'] == reference['failed_iteration'] |
| 75 | + assert report['maximum_tps']['tps'] == 10 |
| 76 | + |
| 77 | + asyncio.run(scenario()) |
| 78 | + |
| 79 | + |
| 80 | +def test_cancellation_lookup_respects_current_value_and_ignores_unrelated_errors(): |
| 81 | + inner = asyncio.CancelledError() |
| 82 | + inner.benchmark_run = {'workload': 'original'} |
| 83 | + outer = asyncio.CancelledError() |
| 84 | + outer.__context__ = inner |
| 85 | + assert exception_evidence(outer, 'benchmark_run') == inner.benchmark_run |
| 86 | + outer.benchmark_run = None |
| 87 | + assert exception_evidence(outer, 'benchmark_run', {}) is None |
| 88 | + unrelated = RuntimeError('new operation') |
| 89 | + unrelated.__context__ = inner |
| 90 | + assert exception_evidence(unrelated, 'benchmark_run') is None |
| 91 | + inner.__context__ = outer # A malformed context cycle must not hang reporting. |
| 92 | + assert exception_evidence(outer, 'missing', 'fallback') == 'fallback' |
0 commit comments