test(backup): wait for the restore, not a 5 s timer, in the thread test
test_database_is_usable_from_every_thread_after_restore let its worker wait 5 s for the restore. On a stalled Windows runner (py3.11, where the previous restore test also took 25 s instead of 0.3 s) the restore took longer, the wait timed out, and the worker read the old database before the restore ran. The worker now waits for the event under a 120 s bound that only a hung test reaches, reports a timeout as an error, and the main thread releases it even when the restore raises. With a restore delayed by 6 s the old test fails and the new one passes.
This commit is contained in:
@@ -24,6 +24,9 @@ from database.operations import PromptDatabase
|
||||
from tests.open_handles import assert_closed
|
||||
from utils.hashing import generate_prompt_hash
|
||||
|
||||
# A bound on thread hand-offs that only a hung test reaches (never a pacing timer).
|
||||
THREAD_WAIT_SECONDS = 120
|
||||
|
||||
|
||||
def _count_prompts(path):
|
||||
"""Row count read through a brand-new connection (closed before return)."""
|
||||
@@ -350,7 +353,13 @@ class TestRestoreFromFile(BackupTestCase):
|
||||
def worker():
|
||||
self.db.search_prompts(text="old") # opens this thread's connection
|
||||
ready.set()
|
||||
proceed.wait(5)
|
||||
# Wait for the restore itself, not for a timer: a stalled Windows
|
||||
# runner once took longer than 5 s to restore, and the worker then
|
||||
# read the old database before the restore had run.
|
||||
if not proceed.wait(THREAD_WAIT_SECONDS):
|
||||
results["error"] = "the restore did not finish"
|
||||
self.db.close()
|
||||
return
|
||||
try:
|
||||
results["texts"] = [p["text"] for p in self.db.search_prompts()]
|
||||
except Exception as exc: # noqa: BLE001 - reported through results
|
||||
@@ -360,12 +369,15 @@ class TestRestoreFromFile(BackupTestCase):
|
||||
|
||||
thread = threading.Thread(target=worker)
|
||||
thread.start()
|
||||
ready.wait(5)
|
||||
self.assertTrue(ready.wait(THREAD_WAIT_SECONDS))
|
||||
|
||||
self.db.model.restore_from_file(src)
|
||||
proceed.set()
|
||||
thread.join(5)
|
||||
try:
|
||||
self.db.model.restore_from_file(src)
|
||||
finally:
|
||||
proceed.set()
|
||||
thread.join(THREAD_WAIT_SECONDS)
|
||||
|
||||
self.assertFalse(thread.is_alive())
|
||||
self.assertNotIn("error", results)
|
||||
self.assertEqual(results["texts"], ["restored"])
|
||||
self.assertEqual([p["text"] for p in self.db.search_prompts()], ["restored"])
|
||||
|
||||
Reference in New Issue
Block a user