Add timeout to wait_for_connection and test

This commit is contained in:
thePR0M3TH3AN
2025-06-30 16:01:25 -04:00
parent 01a81246a5
commit d36607fa9a
5 changed files with 106 additions and 124 deletions

View File

@@ -3,6 +3,8 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from cryptography.fernet import Fernet
from types import SimpleNamespace
import time
sys.path.append(str(Path(__file__).resolve().parents[1]))
@@ -16,11 +18,32 @@ def test_nostr_client_uses_custom_relays():
enc_mgr = EncryptionManager(key, Path(tmpdir))
custom_relays = ["wss://relay1", "wss://relay2"]
with patch("nostr.client.ClientPool") as MockPool, patch(
with patch("nostr.client.RelayManager") as MockManager, patch(
"nostr.client.KeyManager"
), patch.object(NostrClient, "initialize_client_pool"):
with patch.object(enc_mgr, "decrypt_parent_seed", return_value="seed"):
client = NostrClient(enc_mgr, "fp", relays=custom_relays)
MockPool.assert_called_with(custom_relays)
assert client.relays == custom_relays
added = [c.args[0] for c in MockManager.return_value.add_relay.call_args_list]
assert added == custom_relays
def test_wait_for_connection_timeout():
with TemporaryDirectory() as tmpdir:
key = Fernet.generate_key()
enc_mgr = EncryptionManager(key, Path(tmpdir))
with patch.object(NostrClient, "initialize_client_pool"), patch(
"nostr.client.RelayManager"
), patch("nostr.client.KeyManager"), patch.object(
enc_mgr, "decrypt_parent_seed", return_value="seed"
):
client = NostrClient(enc_mgr, "fp")
client.client_pool = SimpleNamespace(connection_statuses={"wss://r": False})
start = time.monotonic()
client.wait_for_connection(timeout=0.2)
duration = time.monotonic() - start
assert duration >= 0.2

View File

@@ -14,7 +14,7 @@ def setup_client(tmp_path):
key = Fernet.generate_key()
enc_mgr = EncryptionManager(key, tmp_path)
with patch("nostr.client.ClientPool"), patch(
with patch("nostr.client.RelayManager"), patch(
"nostr.client.KeyManager"
), patch.object(NostrClient, "initialize_client_pool"), patch.object(
enc_mgr, "decrypt_parent_seed", return_value="seed"
@@ -25,12 +25,12 @@ def setup_client(tmp_path):
class FakeEvent:
KIND_TEXT_NOTE = 1
KIND_ENCRYPT = 2
KIND_ENCRYPT = 4
def __init__(self, kind, content, pub_key):
def __init__(self, kind, content, pubkey):
self.kind = kind
self.content = content
self.pub_key = pub_key
self.pubkey = pubkey
self.id = "id"
def sign(self, _):