Add async background sync task management

This commit is contained in:
thePR0M3TH3AN
2025-07-31 19:38:09 -04:00
parent 265817b67d
commit 77757152d7
4 changed files with 54 additions and 7 deletions

View File

@@ -1,4 +1,6 @@
import time
import asyncio
import warnings
from types import SimpleNamespace
from pathlib import Path
import sys
@@ -17,14 +19,15 @@ def test_unlock_triggers_sync(monkeypatch, tmp_path):
pm.initialize_managers = lambda: None
called = {"sync": False}
def fake_sync(self):
async def fake_sync(self):
called["sync"] = True
monkeypatch.setattr(PasswordManager, "sync_index_from_nostr", fake_sync)
monkeypatch.setattr(PasswordManager, "sync_index_from_nostr_async", fake_sync)
pm.unlock_vault("pw")
pm.start_background_sync()
time.sleep(0.05)
pm.cleanup()
assert called["sync"]
@@ -54,3 +57,29 @@ def test_quick_unlock_background_sync(monkeypatch, tmp_path):
pm.exit_managed_account()
assert called["bg"]
def test_start_background_sync_running_loop(monkeypatch):
pm = PasswordManager.__new__(PasswordManager)
pm.offline_mode = False
called = {"init": False, "sync": False}
async def fake_attempt(self):
called["init"] = True
async def fake_sync(self):
called["sync"] = True
monkeypatch.setattr(PasswordManager, "attempt_initial_sync_async", fake_attempt)
monkeypatch.setattr(PasswordManager, "sync_index_from_nostr_async", fake_sync)
async def runner():
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
pm.start_background_sync()
await asyncio.sleep(0.01)
assert not any(issubclass(wi.category, RuntimeWarning) for wi in w)
asyncio.run(runner())
pm.cleanup()
assert called["init"] and called["sync"]