Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pytest_asyncio.fixture default scope and fix broken registry tests #707

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions llama_stack/distribution/store/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
CachedDiskDistributionRegistry,
DiskDistributionRegistry,
)
from llama_stack.providers.utils.kvstore import kvstore_impl, SqliteKVStoreConfig
from llama_stack.providers.utils.kvstore import kvstore_impl
from llama_stack.providers.utils.kvstore.config import SqliteKVStoreConfig


@pytest.fixture
Expand All @@ -26,14 +27,14 @@ def config():
return config


@pytest_asyncio.fixture
@pytest_asyncio.fixture(scope="function")
async def registry(config):
registry = DiskDistributionRegistry(await kvstore_impl(config))
await registry.initialize()
return registry


@pytest_asyncio.fixture
@pytest_asyncio.fixture(scope="function")
async def cached_registry(config):
registry = CachedDiskDistributionRegistry(await kvstore_impl(config))
await registry.initialize()
Expand Down Expand Up @@ -64,8 +65,8 @@ def sample_model():
@pytest.mark.asyncio
async def test_registry_initialization(registry):
# Test empty registry
results = await registry.get("nonexistent", "nonexistent")
assert len(results) == 0
result = await registry.get("nonexistent", "nonexistent")
assert result is None


@pytest.mark.asyncio
Expand All @@ -75,18 +76,16 @@ async def test_basic_registration(registry, sample_bank, sample_model):
print(f"Registering {sample_model}")
await registry.register(sample_model)
print("Getting bank")
results = await registry.get("memory_bank", "test_bank")
assert len(results) == 1
result_bank = results[0]
result_bank = await registry.get("memory_bank", "test_bank")
assert result_bank is not None
assert result_bank.identifier == sample_bank.identifier
assert result_bank.embedding_model == sample_bank.embedding_model
assert result_bank.chunk_size_in_tokens == sample_bank.chunk_size_in_tokens
assert result_bank.overlap_size_in_tokens == sample_bank.overlap_size_in_tokens
assert result_bank.provider_id == sample_bank.provider_id

results = await registry.get("model", "test_model")
assert len(results) == 1
result_model = results[0]
result_model = await registry.get("model", "test_model")
assert result_model is not None
assert result_model.identifier == sample_model.identifier
assert result_model.provider_id == sample_model.provider_id

Expand All @@ -103,9 +102,8 @@ async def test_cached_registry_initialization(config, sample_bank, sample_model)
cached_registry = CachedDiskDistributionRegistry(await kvstore_impl(config))
await cached_registry.initialize()

results = await cached_registry.get("memory_bank", "test_bank")
assert len(results) == 1
result_bank = results[0]
result_bank = await cached_registry.get("memory_bank", "test_bank")
assert result_bank is not None
assert result_bank.identifier == sample_bank.identifier
assert result_bank.embedding_model == sample_bank.embedding_model
assert result_bank.chunk_size_in_tokens == sample_bank.chunk_size_in_tokens
Expand All @@ -129,18 +127,16 @@ async def test_cached_registry_updates(config):
await cached_registry.register(new_bank)

# Verify in cache
results = await cached_registry.get("memory_bank", "test_bank_2")
assert len(results) == 1
result_bank = results[0]
result_bank = await cached_registry.get("memory_bank", "test_bank_2")
assert result_bank is not None
assert result_bank.identifier == new_bank.identifier
assert result_bank.provider_id == new_bank.provider_id

# Verify persisted to disk
new_registry = DiskDistributionRegistry(await kvstore_impl(config))
await new_registry.initialize()
results = await new_registry.get("memory_bank", "test_bank_2")
assert len(results) == 1
result_bank = results[0]
result_bank = await new_registry.get("memory_bank", "test_bank_2")
assert result_bank is not None
assert result_bank.identifier == new_bank.identifier
assert result_bank.provider_id == new_bank.provider_id

Expand Down Expand Up @@ -170,10 +166,10 @@ async def test_duplicate_provider_registration(config):
)
await cached_registry.register(duplicate_bank)

results = await cached_registry.get("memory_bank", "test_bank_2")
assert len(results) == 1 # Still only one result
result = await cached_registry.get("memory_bank", "test_bank_2")
assert result is not None
assert (
results[0].embedding_model == original_bank.embedding_model
result.embedding_model == original_bank.embedding_model
) # Original values preserved


Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
Copy link
Contributor

@yanxi0830 yanxi0830 Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it would be a better practice to avoid this project level event loop config to use @pytest.mark.asyncio(loop_scope="function") for each test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think being explicit with scopes with fixtures is a good practice and we should keep doing it for our fixtures.

I think it should be fine to have this config. It is not so we can avoid setting explicit scopes, but just to have the default scope when the scope is not specified as pytest-asyncio will eventually change it "function" by default - in the end we get the same. This way we only explicitly say it so we don't get the warning telling us our default scope will be changed.

asyncio_default_fixture_loop_scope = "function"
Loading