Skip to content

The agent that grows with you

test(tools): inject the delegation schema failure on the cursor replay

Tekniume898e92317ed
diff --git a/tests/tools/test_async_delegation_fd_leak.py b/tests/tools/test_async_delegation_fd_leak.py
index 45ac84c2914..89b00519d92 100644
--- a/tests/tools/test_async_delegation_fd_leak.py
+++ b/tests/tools/test_async_delegation_fd_leak.py
@@ -79,18 +79,29 @@ def test_ledger_operations_close_every_connection(monkeypatch, tmp_path):
assert set(opened) == set(closed)
-def _fail_large_schema_replay(self, script):
- # The schema initializer replays the full canonical schema as one
- # large multi-statement script (single durable-shape authority,
- # #94691). Simulate the DDL failure on that path so the
- # connect-close-on-init-failure contract stays pinned.
- if len(script) > 1000:
- raise sqlite3.OperationalError("simulated schema init failure")
- return self._real.executescript(script)
+class _FailingSchemaCursor:
+ """Cursor proxy that fails the canonical DDL replay. ``apply_declared_schema``
+ replays SCHEMA_SQL statement by statement through ``conn.cursor()`` (no
+ ``executescript`` implicit COMMIT), so the injection sits on the cursor."""
+
+ def __init__(self, real):
+ object.__setattr__(self, "_real", real)
+
+ def execute(self, sql, *args, **kwargs):
+ if "CREATE TABLE" in sql:
+ raise sqlite3.OperationalError("simulated schema init failure")
+ return self._real.execute(sql, *args, **kwargs)
+
+ def __getattr__(self, name):
+ return getattr(self._real, name)
def test_schema_init_failure_still_closes_connection(monkeypatch, tmp_path):
- """A PRAGMA/DDL failure after connect() must still close the connection."""
+ """A PRAGMA/DDL failure after connect() must still close the state.db connection.
+
+ Only state.db opens are tracked: the column reconciler builds its reference
+ shape from SCHEMA_SQL in a private ``:memory:`` database, which it closes itself
+ and which holds no file descriptor."""
_point_ledger(monkeypatch, tmp_path)
opened, closed = [], []
real_connect = sqlite3.connect
@@ -101,18 +112,26 @@ def test_schema_init_failure_still_closes_connection(monkeypatch, tmp_path):
raise sqlite3.OperationalError("simulated schema init failure")
return self._real.execute(sql, *args, **kwargs)
- def tracking_connect(*args, **kwargs):
- conn = real_connect(*args, **kwargs)
+ def executescript(self, script):
+ if "CREATE TABLE" in script:
+ raise sqlite3.OperationalError("simulated schema init failure")
+ return self._real.executescript(script)
+
+ def cursor(self, *args, **kwargs):
+ return _FailingSchemaCursor(self._real.cursor(*args, **kwargs))
+
+ def tracking_connect(database, *args, **kwargs):
+ conn = real_connect(database, *args, **kwargs)
+ if str(database) == ":memory:":
+ return conn
opened.append(id(conn))
return _FailingSchemaConnection(conn, closed)
monkeypatch.setattr(ad.sqlite3, "connect", tracking_connect)
- _FailingSchemaConnection.executescript = _fail_large_schema_replay
-
- with pytest.raises(sqlite3.OperationalError):
+ with pytest.raises(sqlite3.OperationalError, match="simulated schema init failure"):
with ad._transaction():
pass
assert len(opened) == 1
- assert len(closed) == 1
+ assert closed == opened