Add configurable Nostr retry backoff

This commit is contained in:
thePR0M3TH3AN
2025-07-24 18:53:45 -04:00
parent 61ffb073b5
commit 8e7224dfd2
5 changed files with 75 additions and 22 deletions

View File

@@ -12,6 +12,7 @@ sys.path.append(str(Path(__file__).resolve().parents[1]))
from seedpass.core.encryption import EncryptionManager
from nostr.client import NostrClient
import nostr.client as nostr_client
import constants
def test_nostr_client_uses_custom_relays():
@@ -151,3 +152,31 @@ def test_update_relays_reinitializes_pool(tmp_path, monkeypatch):
assert called["ran"] is True
assert isinstance(client.client, FakeAddRelaysClient)
assert client.relays == new_relays
def test_retrieve_json_sync_backoff(tmp_path, monkeypatch):
client = _setup_client(tmp_path, FakeAddRelayClient)
monkeypatch.setattr("nostr.client.MAX_RETRIES", 3)
monkeypatch.setattr("nostr.client.RETRY_DELAY", 1)
monkeypatch.setattr("constants.MAX_RETRIES", 3)
monkeypatch.setattr("constants.RETRY_DELAY", 1)
monkeypatch.setattr("seedpass.core.config_manager.MAX_RETRIES", 3)
monkeypatch.setattr("seedpass.core.config_manager.RETRY_DELAY", 1)
sleeps: list[float] = []
def fake_sleep(d):
sleeps.append(d)
monkeypatch.setattr(nostr_client.time, "sleep", fake_sleep)
async def fake_async(self):
return None
monkeypatch.setattr(NostrClient, "_retrieve_json_from_nostr", fake_async)
result = client.retrieve_json_from_nostr_sync()
assert result is None
assert sleeps == [1, 2]

View File

@@ -8,6 +8,7 @@ from seedpass.core.backup import BackupManager
from seedpass.core.config_manager import ConfigManager
from nostr.client import prepare_snapshot
from nostr.backup_models import KIND_SNAPSHOT_CHUNK
import constants
def test_manifest_generation(tmp_path):
@@ -73,7 +74,17 @@ def test_fetch_snapshot_fallback_on_missing_chunk(dummy_nostr_client, monkeypatc
client, relay = dummy_nostr_client
monkeypatch.setattr("nostr.client.MAX_RETRIES", 3)
monkeypatch.setattr("nostr.client.RETRY_DELAY", 0)
monkeypatch.setattr("nostr.client.RETRY_DELAY", 1)
monkeypatch.setattr("constants.MAX_RETRIES", 3)
monkeypatch.setattr("constants.RETRY_DELAY", 1)
monkeypatch.setattr("seedpass.core.config_manager.MAX_RETRIES", 3)
monkeypatch.setattr("seedpass.core.config_manager.RETRY_DELAY", 1)
delays: list[float] = []
async def fake_sleep(d):
delays.append(d)
monkeypatch.setattr("nostr.client.asyncio.sleep", fake_sleep)
data1 = os.urandom(60000)
manifest1, _ = asyncio.run(client.publish_snapshot(data1))
@@ -102,6 +113,7 @@ def test_fetch_snapshot_fallback_on_missing_chunk(dummy_nostr_client, monkeypatc
)
)
assert attempts == 3
assert delays == [1, 2]
def test_fetch_snapshot_uses_event_ids(dummy_nostr_client):