Skip to content

Commit d5a3a86

Browse files
committed
fix: use session level loop for e2e tests
1 parent 04112e8 commit d5a3a86

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

tests/conftest.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22

33
import os
44
import tempfile
5+
from collections.abc import AsyncIterator
56
from pathlib import Path
67

8+
import cocoindex.asyncio as coco_aio
79
import pytest
10+
import pytest_asyncio
811

9-
# === Environment setup BEFORE any imports ===
10-
# This must happen before cocoindex_code modules are imported
11-
12-
13-
# Use tiny model for faster tests
14-
# os.environ["COCOINDEX_CODE_EMBEDDING_MODEL"] = "sentence-transformers/paraphrase-MiniLM-L3-v2"
15-
12+
# === Environment setup BEFORE any cocoindex_code imports ===
1613
# Create test directory and set it BEFORE any module imports
1714
_TEST_DIR = Path(tempfile.mkdtemp(prefix="cocoindex_test_"))
1815
os.environ["COCOINDEX_CODE_ROOT_PATH"] = str(_TEST_DIR)
19-
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
20-
21-
# Set PyTorch to single thread to avoid potential failure on testing environment
22-
# torch.set_num_threads(1)
2316

2417

2518
@pytest.fixture(scope="session")
2619
def test_codebase_root() -> Path:
2720
"""Session-scoped test codebase directory."""
2821
return _TEST_DIR
22+
23+
24+
@pytest_asyncio.fixture(scope="session", loop_scope="session")
25+
async def coco_runtime() -> AsyncIterator[None]:
26+
"""
27+
Set up CocoIndex runtime context for the entire test session.
28+
29+
Uses session-scoped event loop to ensure CocoIndex environment
30+
persists across all tests.
31+
"""
32+
async with coco_aio.runtime():
33+
yield

tests/test_e2e.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import shutil
44
from pathlib import Path
5-
from typing import Iterator
65

7-
import cocoindex as coco
86
import pytest
97

108
from cocoindex_code.config import _discover_codebase_root
@@ -13,13 +11,6 @@
1311

1412
pytest_plugins = ("pytest_asyncio",)
1513

16-
17-
@pytest.fixture(scope="module")
18-
def coco_runtime() -> Iterator[None]:
19-
"""Set up CocoIndex runtime context shared across all tests in this module."""
20-
with coco.runtime():
21-
yield
22-
2314
# === Sample codebase files ===
2415

2516
SAMPLE_MAIN_PY = '''\
@@ -140,7 +131,7 @@ def setup_base_codebase(codebase: Path) -> None:
140131
class TestEndToEnd:
141132
"""End-to-end tests for the complete index-query workflow."""
142133

143-
@pytest.mark.asyncio
134+
@pytest.mark.asyncio(loop_scope="session")
144135
async def test_index_and_query_codebase(
145136
self, test_codebase_root: Path, coco_runtime: None
146137
) -> None:
@@ -164,7 +155,7 @@ async def test_index_and_query_codebase(
164155
assert len(results) > 0
165156
assert "database.py" in results[0].file_path
166157

167-
@pytest.mark.asyncio
158+
@pytest.mark.asyncio(loop_scope="session")
168159
async def test_incremental_update_add_file(
169160
self, test_codebase_root: Path, coco_runtime: None
170161
) -> None:
@@ -190,7 +181,7 @@ async def test_incremental_update_add_file(
190181
assert len(results) > 0
191182
assert "ml_model.py" in results[0].file_path
192183

193-
@pytest.mark.asyncio
184+
@pytest.mark.asyncio(loop_scope="session")
194185
async def test_incremental_update_modify_file(
195186
self, test_codebase_root: Path, coco_runtime: None
196187
) -> None:
@@ -210,7 +201,7 @@ async def test_incremental_update_modify_file(
210201
content_lower = results[0].content.lower()
211202
assert "authenticate" in content_lower or "login" in content_lower
212203

213-
@pytest.mark.asyncio
204+
@pytest.mark.asyncio(loop_scope="session")
214205
async def test_incremental_update_delete_file(
215206
self, test_codebase_root: Path, coco_runtime: None
216207
) -> None:

0 commit comments

Comments
 (0)