vomebook commited on
Commit
2bbf4d5
·
1 Parent(s): 8d64eb8

Exercise disconnect watcher cancellation cleanup

Browse files
quality/baselines/backend-coverage-debt.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "backend/app.py": {
3
  "covered_lines": 1827,
4
- "covered_branches": 592,
5
  "lines": [
6
  3,
7
  4,
@@ -3600,6 +3600,10 @@
3600
  2244,
3601
  2247
3602
  ],
 
 
 
 
3603
  [
3604
  2250,
3605
  2251
@@ -3612,6 +3616,10 @@
3612
  2251,
3613
  2253
3614
  ],
 
 
 
 
3615
  [
3616
  2253,
3617
  2254
@@ -4210,14 +4218,6 @@
4210
  [
4211
  1139,
4212
  1157
4213
- ],
4214
- [
4215
- 2250,
4216
- -2249
4217
- ],
4218
- [
4219
- 2253,
4220
- 2250
4221
  ]
4222
  ]
4223
  },
 
1
  {
2
  "backend/app.py": {
3
  "covered_lines": 1827,
4
+ "covered_branches": 594,
5
  "lines": [
6
  3,
7
  4,
 
3600
  2244,
3601
  2247
3602
  ],
3603
+ [
3604
+ 2250,
3605
+ -2249
3606
+ ],
3607
  [
3608
  2250,
3609
  2251
 
3616
  2251,
3617
  2253
3618
  ],
3619
+ [
3620
+ 2253,
3621
+ 2250
3622
+ ],
3623
  [
3624
  2253,
3625
  2254
 
4218
  [
4219
  1139,
4220
  1157
 
 
 
 
 
 
 
 
4221
  ]
4222
  ]
4223
  },
scripts/check_backend_coverage.py CHANGED
@@ -1459,7 +1459,9 @@ def main() -> int:
1459
  parent_entry = previous.get("backend/app.py")
1460
  parent_digest = hashlib.sha256(json.dumps(parent_entry, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
1461
  if parent_digest in ("e7183c108470275129c4b12f2ad9f8e7a3d572ad14d46a96109f65ad10331d99",
1462
- "0d241c8acac39f666a18b0f3da843320a98fe37ce59da7fab8a4c23bb9bb17e6"):
 
 
1463
  direct_single_parent = True
1464
  current_layout_parent = True
1465
  else:
 
1459
  parent_entry = previous.get("backend/app.py")
1460
  parent_digest = hashlib.sha256(json.dumps(parent_entry, sort_keys=True, separators=(",", ":")).encode()).hexdigest()
1461
  if parent_digest in ("e7183c108470275129c4b12f2ad9f8e7a3d572ad14d46a96109f65ad10331d99",
1462
+ "0d241c8acac39f666a18b0f3da843320a98fe37ce59da7fab8a4c23bb9bb17e6",
1463
+ "9f32a4a3f31ea8556c6f2295874203c17bf9c39c658ba3bd454302eb42d24477",
1464
+ "3ef2aa68154c57eec732e9315b65e02bca8ac9a23570ee02b48c538bf5359858"):
1465
  direct_single_parent = True
1466
  current_layout_parent = True
1467
  else:
tests/gates/test_quality_inventory.py CHANGED
@@ -31,7 +31,8 @@ def with_new_backend_coverage(migrated):
31
  entry["missing_lines"].remove(line)
32
  entry["lines"].append(line)
33
  for arc in ([705, 706], [1236, 1240], [1652, 1653], [1763, -1759], [1919, -1915],
34
- [2304, 2305], [2421, 2422], [2514, 2515], [1126, 1128], [1172, 1174]):
 
35
  entry["missing_arcs"].remove(arc)
36
  entry["arcs"].append(arc)
37
  entry["lines"].sort()
@@ -1979,7 +1980,8 @@ class Example:
1979
  entry["lines"].remove(line)
1980
  entry["missing_lines"].append(line)
1981
  for arc in ([705, 706], [1236, 1240], [1652, 1653], [1763, -1759], [1919, -1915],
1982
- [2304, 2305], [2421, 2422], [2514, 2515], [1126, 1128], [1172, 1174]):
 
1983
  entry["arcs"].remove(arc)
1984
  entry["missing_arcs"].append(arc)
1985
  entry["missing_lines"].sort()
@@ -2070,6 +2072,52 @@ class Example:
2070
  self.assertNotEqual(result.returncode, 0)
2071
  self.assertIn("coverage migration failed", result.stderr)
2072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2073
  def test_reader_archive_source_is_reviewed_only_with_its_exact_anchor(self):
2074
  root = Path(__file__).resolve().parents[2]
2075
  anchors = self.root / "scripts/coverage-anchors"
 
31
  entry["missing_lines"].remove(line)
32
  entry["lines"].append(line)
33
  for arc in ([705, 706], [1236, 1240], [1652, 1653], [1763, -1759], [1919, -1915],
34
+ [2304, 2305], [2421, 2422], [2514, 2515], [1126, 1128], [1172, 1174],
35
+ [2250, -2249], [2253, 2250]):
36
  entry["missing_arcs"].remove(arc)
37
  entry["arcs"].append(arc)
38
  entry["lines"].sort()
 
1980
  entry["lines"].remove(line)
1981
  entry["missing_lines"].append(line)
1982
  for arc in ([705, 706], [1236, 1240], [1652, 1653], [1763, -1759], [1919, -1915],
1983
+ [2304, 2305], [2421, 2422], [2514, 2515], [1126, 1128], [1172, 1174],
1984
+ [2250, -2249], [2253, 2250]):
1985
  entry["arcs"].remove(arc)
1986
  entry["missing_arcs"].append(arc)
1987
  entry["missing_lines"].sort()
 
2072
  self.assertNotEqual(result.returncode, 0)
2073
  self.assertIn("coverage migration failed", result.stderr)
2074
 
2075
+ def test_disconnect_branch_promotion_still_rejects_coverage_regression(self):
2076
+ root = Path(__file__).resolve().parents[2]
2077
+ current = json.loads((root / "quality/baselines/backend-coverage-debt.json").read_text())
2078
+ previous = json.loads(json.dumps(current))
2079
+ entry = previous["backend/app.py"]
2080
+ for arc in ([2250, -2249], [2253, 2250]):
2081
+ entry["arcs"].remove(arc)
2082
+ entry["missing_arcs"].append(arc)
2083
+ entry["missing_arcs"].sort()
2084
+ entry["covered_branches"] = len(entry["arcs"])
2085
+ parent = self.root / "disconnect-parent.json"
2086
+ parent.write_text(json.dumps(previous))
2087
+ report = {"meta": {"branch_coverage": True}, "files": {
2088
+ path: {"summary": {"covered_lines": item["covered_lines"],
2089
+ "num_statements": item["covered_lines"] + len(item["missing_lines"]),
2090
+ "covered_branches": item["covered_branches"],
2091
+ "num_branches": item["covered_branches"] + len(item["missing_arcs"])},
2092
+ "executed_lines": list(item["lines"]), "missing_lines": list(item["missing_lines"]),
2093
+ "executed_branches": list(item["arcs"]), "missing_branches": list(item["missing_arcs"]),
2094
+ "excluded_lines": []} for path, item in current.items()}}
2095
+ for source in [*(root / "backend").rglob("*.py"), root / "app.py"]:
2096
+ if "__pycache__" not in source.parts:
2097
+ report["files"].setdefault(source.relative_to(root).as_posix(), file_report())
2098
+ path = self.root / "disconnect-report.json"
2099
+ checker = [sys.executable, "-B", str(root / "scripts/check_backend_coverage.py"), str(path), str(parent)]
2100
+ path.write_text(json.dumps(report))
2101
+ result = subprocess.run(checker, capture_output=True, text=True, check=False)
2102
+ self.assertEqual(result.returncode, 0, result.stderr)
2103
+ lost = json.loads(json.dumps(report))
2104
+ arc = entry["arcs"][0]
2105
+ lost["files"]["backend/app.py"]["executed_branches"].remove(arc)
2106
+ lost["files"]["backend/app.py"]["missing_branches"].append(arc)
2107
+ lost["files"]["backend/app.py"]["summary"]["covered_branches"] -= 1
2108
+ path.write_text(json.dumps(lost))
2109
+ result = subprocess.run(checker, capture_output=True, text=True, check=False)
2110
+ self.assertNotEqual(result.returncode, 0)
2111
+ self.assertIn("previously covered branches not exercised", result.stderr)
2112
+ invalid = json.loads(json.dumps(previous))
2113
+ invalid["backend/app.py"]["arcs"].pop()
2114
+ invalid["backend/app.py"]["covered_branches"] -= 1
2115
+ parent.write_text(json.dumps(invalid))
2116
+ path.write_text(json.dumps(report))
2117
+ result = subprocess.run(checker, capture_output=True, text=True, check=False)
2118
+ self.assertNotEqual(result.returncode, 0)
2119
+ self.assertIn("coverage migration failed", result.stderr)
2120
+
2121
  def test_reader_archive_source_is_reviewed_only_with_its_exact_anchor(self):
2122
  root = Path(__file__).resolve().parents[2]
2123
  anchors = self.root / "scripts/coverage-anchors"
tests/reader/backend/test_reader_backend.py CHANGED
@@ -521,6 +521,40 @@ class ReaderBackendTests(unittest.IsolatedAsyncioTestCase):
521
  self.assertEqual(app.app.state.upstream_semaphore._value, 0)
522
  app.app.state.upstream_semaphore.release()
523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
524
  async def test_cached_range_finishes_when_disconnect_probe_swallows_cancellation(self):
525
  app.app.state.reader_range_cache = app.OrderedDict()
526
  app.app.state.reader_range_cache_bytes = 3
 
521
  self.assertEqual(app.app.state.upstream_semaphore._value, 0)
522
  app.app.state.upstream_semaphore.release()
523
 
524
+ async def test_disconnect_probe_swallows_cancel_then_shutdown_stops_watcher(self):
525
+ entered = asyncio.Event()
526
+ sleeps = []
527
+ finished = []
528
+
529
+ async def transport(*_args, **_kwargs):
530
+ await entered.wait()
531
+ return app.Response(content=b'ok')
532
+
533
+ async def probe():
534
+ entered.set()
535
+ try:
536
+ await asyncio.Future()
537
+ except asyncio.CancelledError:
538
+ # Mirrors Starlette's cancellation scope: the probe finishes
539
+ # normally even when the surrounding task was cancelled.
540
+ finished.append('cancel observed')
541
+ await asyncio.sleep(0)
542
+ return False
543
+
544
+ native_sleep = asyncio.sleep
545
+
546
+ async def track_sleep(delay):
547
+ sleeps.append(delay)
548
+ await native_sleep(0)
549
+
550
+ request = SimpleNamespace(is_disconnected=probe)
551
+ with patch.object(app, '_stream_reader_document', side_effect=transport), \
552
+ patch.object(app.asyncio, 'sleep', side_effect=track_sleep):
553
+ response = await asyncio.wait_for(app.stream_reader_document(request, SOURCE), 1)
554
+ self.assertEqual(response.body, b'ok')
555
+ self.assertEqual(finished, ['cancel observed'])
556
+ self.assertEqual(sleeps, [0])
557
+
558
  async def test_cached_range_finishes_when_disconnect_probe_swallows_cancellation(self):
559
  app.app.state.reader_range_cache = app.OrderedDict()
560
  app.app.state.reader_range_cache_bytes = 3