fix(tests): eliminate tarpit OOM from global asyncio.sleep mock
Two interacting bugs caused asyncio.sleep to be mocked globally, letting tarpit_watcher_worker spin the event loop on a non-async mock and accumulate _increment_mock_call records without bound: 1. test_ingester.py patched `decnet.web.ingester.asyncio.sleep` via the asyncio singleton — any code in the process using asyncio.sleep (including the tarpit worker) hit the fake_sleep side_effect. Fix: add `_sleep = asyncio.sleep` alias in ingester.py and patch `decnet.web.ingester._sleep` instead — scopes the mock to ingester. 2. test_api_startup_guards.py called `_run_lifespan_startup` without DECNET_CONTRACT_TEST=true, which started the real tarpit task in a manually-constructed event loop that the tests never cancelled. Fix: set DECNET_CONTRACT_TEST=true inside _run_lifespan_startup so the lifespan skips all background workers.
This commit is contained in:
@@ -30,17 +30,26 @@ def _strip_pytest_vars(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
|
||||
|
||||
async def _run_lifespan_startup(api_mod) -> None:
|
||||
"""Run the lifespan up to (but not past) yield, then unwind cleanly."""
|
||||
app = FastAPI()
|
||||
cm = api_mod.lifespan(app)
|
||||
await cm.__aenter__()
|
||||
"""Run the lifespan up to (but not past) yield, then unwind cleanly.
|
||||
|
||||
DECNET_CONTRACT_TEST suppresses all background workers (ingestion,
|
||||
collector, TTP, tarpit) so no tasks escape test teardown.
|
||||
"""
|
||||
import os
|
||||
os.environ["DECNET_CONTRACT_TEST"] = "true"
|
||||
try:
|
||||
return
|
||||
finally:
|
||||
app = FastAPI()
|
||||
cm = api_mod.lifespan(app)
|
||||
await cm.__aenter__()
|
||||
try:
|
||||
await cm.__aexit__(None, None, None)
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
finally:
|
||||
try:
|
||||
await cm.__aexit__(None, None, None)
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
os.environ.pop("DECNET_CONTRACT_TEST", None)
|
||||
|
||||
|
||||
def test_master_api_refuses_to_start_in_agent_mode(
|
||||
|
||||
@@ -476,7 +476,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
mock_repo.add_logs.assert_not_awaited()
|
||||
@@ -507,7 +507,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -539,7 +539,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -575,7 +575,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -607,7 +607,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -646,7 +646,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -680,7 +680,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -721,7 +721,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
@@ -761,7 +761,7 @@ class TestLogIngestionWorker:
|
||||
raise asyncio.CancelledError()
|
||||
|
||||
with patch.dict(os.environ, {"DECNET_INGEST_LOG_FILE": log_file}):
|
||||
with patch("decnet.web.ingester.asyncio.sleep", side_effect=fake_sleep):
|
||||
with patch("decnet.web.ingester._sleep", side_effect=fake_sleep):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await log_ingestion_worker(mock_repo)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user