improve tests

This commit is contained in:
phact 2025-09-12 11:54:28 -04:00
parent 2ef560ca7f
commit e23ed258c9
3 changed files with 172 additions and 130 deletions

View file

@ -297,6 +297,29 @@ class AppClients:
return self return self
async def close(self):
"""Close all client connections"""
try:
if hasattr(self, 'opensearch') and self.opensearch:
await self.opensearch.close()
self.opensearch = None
except Exception as e:
logger.warning("Error closing OpenSearch client", error=str(e))
try:
if hasattr(self, 'langflow_http_client') and self.langflow_http_client:
await self.langflow_http_client.aclose()
self.langflow_http_client = None
except Exception as e:
logger.warning("Error closing Langflow HTTP client", error=str(e))
try:
if hasattr(self, 'patched_async_client') and self.patched_async_client:
await self.patched_async_client.close()
self.patched_async_client = None
except Exception as e:
logger.warning("Error closing OpenAI client", error=str(e))
async def ensure_langflow_client(self): async def ensure_langflow_client(self):
"""Ensure Langflow client exists; try to generate key and create client lazily.""" """Ensure Langflow client exists; try to generate key and create client lazily."""
if self.langflow_client is not None: if self.langflow_client is not None:

View file

@ -60,6 +60,8 @@ async def test_upload_and_search_endpoint(tmp_path: Path, disable_langflow_inges
await clients.initialize() await clients.initialize()
try: try:
await clients.opensearch.indices.delete(index=INDEX_NAME) await clients.opensearch.indices.delete(index=INDEX_NAME)
# Wait for deletion to complete
await asyncio.sleep(1)
except Exception: except Exception:
pass pass
@ -67,7 +69,17 @@ async def test_upload_and_search_endpoint(tmp_path: Path, disable_langflow_inges
# Manually run startup tasks since httpx ASGI transport here doesn't manage lifespan # Manually run startup tasks since httpx ASGI transport here doesn't manage lifespan
await startup_tasks(app.state.services) await startup_tasks(app.state.services)
# Verify index is truly empty after startup
try:
count_response = await clients.opensearch.count(index=INDEX_NAME)
doc_count = count_response.get('count', 0)
assert doc_count == 0, f"Index should be empty after startup but contains {doc_count} documents"
except Exception as e:
# If count fails, the index might not exist yet, which is fine
pass
transport = httpx.ASGITransport(app=app) transport = httpx.ASGITransport(app=app)
try:
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client: async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
# Wait for app + OpenSearch readiness using existing endpoints # Wait for app + OpenSearch readiness using existing endpoints
await wait_for_service_ready(client) await wait_for_service_ready(client)
@ -124,13 +136,11 @@ async def test_upload_and_search_endpoint(tmp_path: Path, disable_langflow_inges
text = top.get("text") or top.get("content") text = top.get("text") or top.get("content")
assert isinstance(text, str) assert isinstance(text, str)
assert "testing" in text.lower() assert "testing" in text.lower()
finally:
# Explicitly close global clients to avoid aiohttp warnings # Explicitly close global clients to avoid aiohttp warnings
from src.config.settings import clients from src.config.settings import clients
try: try:
if getattr(clients, "opensearch", None): await clients.close()
await clients.opensearch.close()
if getattr(clients, "langflow_http_client", None):
await clients.langflow_http_client.aclose()
except Exception: except Exception:
pass pass
@ -159,12 +169,24 @@ async def test_router_upload_ingest_traditional(tmp_path: Path, disable_langflow
await clients.initialize() await clients.initialize()
try: try:
await clients.opensearch.indices.delete(index=INDEX_NAME) await clients.opensearch.indices.delete(index=INDEX_NAME)
# Wait for deletion to complete
await asyncio.sleep(1)
except Exception: except Exception:
pass pass
app = await create_app() app = await create_app()
await startup_tasks(app.state.services) await startup_tasks(app.state.services)
# Verify index is truly empty after startup
try:
count_response = await clients.opensearch.count(index=INDEX_NAME)
doc_count = count_response.get('count', 0)
assert doc_count == 0, f"Index should be empty after startup but contains {doc_count} documents"
except Exception as e:
# If count fails, the index might not exist yet, which is fine
pass
transport = httpx.ASGITransport(app=app) transport = httpx.ASGITransport(app=app)
try:
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client: async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
await wait_for_service_ready(client) await wait_for_service_ready(client)
@ -183,11 +205,9 @@ async def test_router_upload_ingest_traditional(tmp_path: Path, disable_langflow
data = resp.json() data = resp.json()
assert resp.status_code == 201, resp.text assert resp.status_code == 201, resp.text
assert isinstance(data.get("task_id"), str) assert isinstance(data.get("task_id"), str)
finally:
from src.config.settings import clients from src.config.settings import clients
try: try:
if getattr(clients, "opensearch", None): await clients.close()
await clients.opensearch.close()
if getattr(clients, "langflow_http_client", None):
await clients.langflow_http_client.aclose()
except Exception: except Exception:
pass pass

View file

@ -70,6 +70,7 @@ async def test_startup_ingest_creates_task(disable_langflow_ingest: bool):
await startup_tasks(app.state.services) await startup_tasks(app.state.services)
transport = httpx.ASGITransport(app=app) transport = httpx.ASGITransport(app=app)
try:
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client: async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
await wait_for_ready(client) await wait_for_ready(client)
@ -103,12 +104,10 @@ async def test_startup_ingest_creates_task(disable_langflow_ingest: bool):
newest = tasks[0] newest = tasks[0]
assert "task_id" in newest assert "task_id" in newest
assert newest.get("total_files") == expected_files assert newest.get("total_files") == expected_files
finally:
# Explicitly close global clients to avoid aiohttp warnings # Explicitly close global clients to avoid aiohttp warnings
from src.config.settings import clients from src.config.settings import clients
try: try:
if getattr(clients, "opensearch", None): await clients.close()
await clients.opensearch.close()
if getattr(clients, "langflow_http_client", None):
await clients.langflow_http_client.aclose()
except Exception: except Exception:
pass pass