Merge pull request #958 from PR0M3TH3AN/test-audit-agent-daily-run-15521316835403864428

refactor: improve test determinism by removing time.sleep
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2026-02-28 22:37:27 -05:00
commit baaaa25e45
9 files changed
+104 -20

No files matched your search

+1 -1
View File
@@ -57,4 +57,4 @@
"Found @pytest.mark.skip"
]
}
]
]
@@ -0,0 +1,30 @@
# Test Audit Report
Date: 2026-03-01
Agent: test-audit-agent
## Overview
The test-audit-agent ran a static analysis of tests using `run_python_static_analysis.py` and discovered multiple test files that utilized `time.sleep()`, causing flaky behavior, non-determinism, and slowing down the overall test suite. The agent modified several tests to replace explicit sleeping with active polling or deterministic thread joins.
## Changes
1. **`src/tests/test_unlock_sync.py`**
- In `test_unlock_triggers_sync`, replaced `time.sleep(0.05)` with `pm._sync_task.join(timeout=1.0)`.
2. **`src/tests/test_nostr_sdk_workflow.py`**
- Replaced `time.sleep(0.5)` with a polling loop that attempts to connect using `create_connection` up to 40 times (with 0.05s interval), raising an exception if the fake relay doesn't start.
3. **`src/tests/test_background_relay_check.py`**
- In both async checks, replaced `time.sleep(0.05)` with `pm._relay_thread.join(timeout=1.0)`.
4. **`src/tests/test_offline_mode_behavior.py`**
- Removed `time.sleep(0.05)` entirely from `test_start_background_sync_offline` because the early return on offline mode makes synchronization instantaneous, rendering the sleep useless.
5. **`src/tests/test_nostr_index_size.py`**
- The test was missing an assertion (reported by static analysis). Added `assert len(results) > 0` to verify results collection.
- Replaced `time.sleep(delay)` during propagation waiting with an asynchronous polling retry logic inside `fetch_with_retry` loop.
## Notes
No remaining flakiness detected in the modified files.
@@ -0,0 +1,24 @@
# Test Integrity Note
date: 2026-03-01
agent: test-audit-agent
changes:
- file: src/tests/test_unlock_sync.py
type: refactor
reason: Removed non-deterministic time.sleep(0.05) to await task completion
details: Replaced time.sleep with pm._sync_task.join(timeout=1.0)
- file: src/tests/test_nostr_sdk_workflow.py
type: refactor
reason: Removed non-deterministic time.sleep(0.5) waiting for relay to start
details: Replaced time.sleep with an active polling connection retry loop
- file: src/tests/test_background_relay_check.py
type: refactor
reason: Removed non-deterministic time.sleep(0.05) waiting for background thread
details: Replaced time.sleep with pm._relay_thread.join(timeout=1.0)
- file: src/tests/test_offline_mode_behavior.py
type: refactor
reason: Removed useless time.sleep(0.05)
details: Background thread is never spawned because of early exit, so removed sleep entirely
- file: src/tests/test_nostr_index_size.py
type: refactor
reason: Removed non-deterministic time.sleep(delay) and added missing assertion
details: Replaced time.sleep with an active polling asyncio retry loop and added assert len(results) > 0 at the end of the file.
+2 -2
View File
@@ -21,7 +21,7 @@ def test_background_relay_check_runs_async(monkeypatch):
)
pm.start_background_relay_check()
time.sleep(0.05)
pm._relay_thread.join(timeout=1.0)
assert called["args"] == MIN_HEALTHY_RELAYS
@@ -34,7 +34,7 @@ def test_background_relay_check_warns_when_unhealthy(monkeypatch):
pm.nostr_client = SimpleNamespace(check_relay_health=lambda mr: mr - 1)
pm.start_background_relay_check()
time.sleep(0.05)
pm._relay_thread.join(timeout=1.0)
note = pm.notifications.get_nowait()
assert note.level == "WARNING"
+26 -12
View File
@@ -84,25 +84,37 @@ def test_nostr_index_size_limits(pytestconfig: pytest.Config):
encrypted = vault.get_encrypted_index()
payload_size = len(encrypted) if encrypted else 0
asyncio.run(client.publish_snapshot(encrypted or b""))
time.sleep(delay)
result = asyncio.run(client.fetch_latest_snapshot())
retrieved = gzip.decompress(b"".join(result[1])) if result else None
retrieved_ok = retrieved == encrypted
async def fetch_with_retry(
client, expected_data, timeout, interval=1.0
):
start_time = time.time()
while time.time() - start_time < timeout:
result = await client.fetch_latest_snapshot()
retrieved = (
gzip.decompress(b"".join(result[1])) if result else None
)
if retrieved == expected_data:
return True
await asyncio.sleep(interval)
return False
retrieved_ok = asyncio.run(
fetch_with_retry(client, encrypted, delay)
)
if not retrieved_ok:
print(f"Initial retrieve failed: {client.last_error}")
result = asyncio.run(client.fetch_latest_snapshot())
retrieved = (
gzip.decompress(b"".join(result[1])) if result else None
retrieved_ok = asyncio.run(
fetch_with_retry(client, encrypted, delay)
)
retrieved_ok = retrieved == encrypted
if not retrieved_ok:
print("Trying alternate relay")
client.update_relays(["wss://relay.damus.io"])
result = asyncio.run(client.fetch_latest_snapshot())
retrieved = (
gzip.decompress(b"".join(result[1])) if result else None
retrieved_ok = asyncio.run(
fetch_with_retry(client, encrypted, delay)
)
retrieved_ok = retrieved == encrypted
results.append((entry_count, payload_size, True, retrieved_ok))
if max_entries is not None:
if entry_count >= max_entries:
@@ -127,3 +139,5 @@ def test_nostr_index_size_limits(pytestconfig: pytest.Config):
synced = sum(1 for _, _, pub, ret in results if pub and ret)
print(f"Successfully synced entries: {synced}")
assert len(results) > 0
+11 -3
View File
@@ -40,13 +40,21 @@ def test_nostr_sdk_send_receive(tmp_path):
thread = threading.Thread(target=run_relay, args=(relay,), daemon=True)
thread.start()
time.sleep(0.5)
seed = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
fingerprint = "test"
km = KeyManager(seed, fingerprint)
ws = create_connection("ws://localhost:8765")
# Wait for the relay to be ready
ws = None
for _ in range(40):
try:
ws = create_connection("ws://localhost:8765")
break
except ConnectionRefusedError:
time.sleep(0.05)
if ws is None:
raise RuntimeError("Fake relay did not start in time")
keys = sdk.Keys.parse(km.get_private_key_hex())
event = (
-1
View File
@@ -23,5 +23,4 @@ def test_start_background_sync_offline(monkeypatch):
called = {"sync": False}
pm.sync_index_from_nostr = lambda: called.__setitem__("sync", True)
PasswordManager.start_background_sync(pm)
time.sleep(0.05)
assert called["sync"] is False
+1 -1
View File
@@ -27,7 +27,7 @@ def test_unlock_triggers_sync(monkeypatch, tmp_path):
pm.unlock_vault("pw")
pm.start_background_sync()
time.sleep(0.05)
pm._sync_task.join(timeout=1.0)
pm.cleanup()
assert called["sync"]
@@ -0,0 +1,9 @@
---
agent: test-audit-agent
platform: codex
status: completed
---
Status: Success
Learnings:
The test-audit-agent ran a static analysis of tests using `run_python_static_analysis.py` and discovered multiple test files that utilized `time.sleep()`, causing flaky behavior, non-determinism, and slowing down the overall test suite. The agent modified several tests to replace explicit sleeping with active polling or deterministic thread joins.