120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""gen_connection_pool_truth.py — freeze the connection_pool parity oracle
|
|
(dichotomy wave #2479 tranche).
|
|
|
|
Generator law: RUNE_CPOOL_CORE=python pinned (the Rust core must never see
|
|
the freeze), inputs deep-copied, every public kernel branch exercised
|
|
(new / reuse / stale-removal / capacity-eviction / commit / status /
|
|
close_all) against a REAL sqlite-backed pool in a temp dir. Output:
|
|
src/rust/connection_pool/frozen_cpool_truth.json — replayed VERBATIM by the
|
|
crate's cfg(test) battery.
|
|
"""
|
|
import copy
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "src"))
|
|
|
|
os.environ["RUNE_CPOOL_CORE"] = "python"
|
|
|
|
from rune.connection_pool import ConnectionPool # noqa: E402
|
|
|
|
MAX = 2
|
|
cases: list = []
|
|
|
|
|
|
def pool_state(p: ConnectionPool) -> dict:
|
|
"""The kernel-shaped pool state, as the oracle holds it right now."""
|
|
st = p.get_status()
|
|
return {"max_connections": st["max_connections"],
|
|
"databases": copy.deepcopy(st["databases"])}
|
|
|
|
|
|
def record_begin_get(p: ConnectionPool, db_path: str, valid: bool) -> None:
|
|
"""Run one oracle get_connection op and freeze it: a cpool_begin_get
|
|
case (and, when the oracle created a connection, the matching
|
|
cpool_commit case)."""
|
|
before = pool_state(p)
|
|
pooled_before = dict(p.connections)
|
|
conn = p.get_connection(db_path)
|
|
after_full = pool_state(p)
|
|
reuse = db_path in pooled_before and conn is pooled_before[db_path]
|
|
if reuse:
|
|
after_begin = copy.deepcopy(before)
|
|
else:
|
|
after_begin = {"max_connections": after_full["max_connections"],
|
|
"databases": [d for d in after_full["databases"]
|
|
if d != db_path]}
|
|
evicted = (set(before["databases"]) - set(after_full["databases"])
|
|
- {db_path})
|
|
cases.append({
|
|
"fn": "cpool_begin_get",
|
|
"args": {"pool": copy.deepcopy(before), "db_path": db_path,
|
|
"valid": valid},
|
|
"expected": {"pool": after_begin, "reuse": reuse,
|
|
"evict": sorted(evicted)[0] if evicted else None},
|
|
})
|
|
if not reuse:
|
|
cases.append({
|
|
"fn": "cpool_commit",
|
|
"args": {"pool": copy.deepcopy(after_begin), "db_path": db_path},
|
|
"expected": {"pool": copy.deepcopy(after_full)},
|
|
})
|
|
|
|
|
|
def main() -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = ConnectionPool(max_connections=MAX)
|
|
cases.append({
|
|
"fn": "cpool_new",
|
|
"args": {"max_connections": MAX},
|
|
"expected": copy.deepcopy(pool_state(p)),
|
|
})
|
|
|
|
dbs = [os.path.join(td, f"{n}.db") for n in "ABCD"]
|
|
|
|
record_begin_get(p, dbs[0], True) # create A
|
|
record_begin_get(p, dbs[0], True) # reuse A
|
|
record_begin_get(p, dbs[1], True) # create B
|
|
record_begin_get(p, dbs[2], True) # create C — evicts A
|
|
# force B stale: close the underlying connection, then re-get
|
|
p.connections[dbs[1]].close()
|
|
record_begin_get(p, dbs[1], False) # stale B — no evict
|
|
record_begin_get(p, dbs[3], True) # create D — evicts C
|
|
|
|
before_status = pool_state(p)
|
|
cases.append({
|
|
"fn": "cpool_status",
|
|
"args": {"pool": copy.deepcopy(before_status)},
|
|
"expected": copy.deepcopy(p.get_status()),
|
|
})
|
|
|
|
before_close = pool_state(p)
|
|
p.close_all()
|
|
cases.append({
|
|
"fn": "cpool_close_all",
|
|
"args": {"pool": copy.deepcopy(before_close)},
|
|
"expected": copy.deepcopy(pool_state(p)),
|
|
})
|
|
|
|
record_begin_get(p, dbs[0], True) # fresh pool: create A
|
|
|
|
doc = {
|
|
"module": "connection_pool",
|
|
"pin": "RUNE_CPOOL_CORE=python",
|
|
"frozen_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
"cases": cases,
|
|
}
|
|
out = ROOT / "src" / "rust" / "connection_pool" / "frozen_cpool_truth.json"
|
|
out.write_text(json.dumps(doc, indent=1) + "\n")
|
|
print(f"frozen {len(cases)} cases -> {out}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|