Fix frozen delta watermark and add error stats, lazy source, concurrent disk reads, and per-engine config

This commit is contained in:
Jan Doubravský
2026-06-08 19:35:33 +02:00
parent 209ae667ab
commit 6dc85e4f3c
17 changed files with 668 additions and 71 deletions
+63
View File
@@ -1,4 +1,5 @@
import sqlite3
import threading
import pytest
@@ -96,6 +97,68 @@ def test_disk_mode_reload_in_new_instance(tmp_path, source_conn):
c2.close()
def test_quoted_reserved_and_spaced_identifiers(tmp_path):
"""Table/column names that are reserved words or contain spaces must work."""
src = sqlite3.connect(":memory:")
src.execute('CREATE TABLE "weird tbl" ("order" TEXT, "group by" TEXT)')
src.executemany('INSERT INTO "weird tbl" VALUES (?, ?)', [("1", "a"), ("2", "b")])
src.commit()
c = CacheManager(db_path=tmp_path / "c.db", backup_interval=9999)
c.load_table("weird tbl", ["order", "group by"], src)
assert c.is_table_cached("weird tbl") is True
_, rows = c.execute_in_memory('SELECT "order", "group by" FROM "weird tbl"')
assert ("1", "a") in rows
c.close()
src.close()
def test_disk_mode_uses_separate_read_connection(tmp_path, source_conn):
"""Disk-mode reads go through a per-thread read connection, not the writer."""
c = CacheManager(db_path=tmp_path / "c.db", backup_interval=9999, in_memory=False)
c.load_table("users", ["name", "email"], source_conn)
_, rows = c.execute_in_memory("SELECT name FROM users ORDER BY name")
assert [r[0] for r in rows] == ["alice", "bob"]
assert len(c._read_conns) == 1
assert c._read_conns[0] is not c.connection # dedicated read conn
c.close()
def test_disk_mode_concurrent_reads(tmp_path, source_conn):
"""Several reader threads each get their own connection and correct results."""
c = CacheManager(db_path=tmp_path / "c.db", backup_interval=9999, in_memory=False)
c.load_table("users", ["name"], source_conn)
results: list[int] = []
errors: list[Exception] = []
def reader() -> None:
try:
_, rows = c.execute_in_memory("SELECT name FROM users")
results.append(len(rows))
except Exception as e: # noqa: BLE001
errors.append(e)
threads = [threading.Thread(target=reader) for _ in range(5)]
for t in threads:
t.start()
for t in threads:
t.join(5)
assert not errors
assert results == [2] * 5
assert len(c._read_conns) == 5 # one read connection per reader thread
c.close()
def test_memory_mode_uses_shared_connection(cache, source_conn):
"""In-memory mode can't share :memory: across connections — no read conns."""
cache.load_table("users", ["name"], source_conn)
cache.execute_in_memory("SELECT name FROM users")
assert cache._read_conns == []
def test_disk_mode_reset_keeps_file(tmp_path, source_conn):
db_path = tmp_path / "cache.db"
c = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)