Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/frequenz/channels/_latest_value_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import asyncio
import typing
from collections.abc import Set

from ._receiver import Receiver

Expand Down Expand Up @@ -145,6 +146,13 @@ def unique_id(self) -> str:
"""The unique identifier of this instance."""
return self._unique_id

def keys(self) -> Set[HashableT]:
"""Return the set of keys for which values have been received.

If no key function is provided, this will return an empty set.
"""
return self._latest_value_by_key.keys()

def get(self, key: HashableT | Sentinel = NO_KEY) -> T_co:
"""Return the latest value that has been received.

Expand Down
8 changes: 8 additions & 0 deletions tests/test_latest_value_cache_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ async def test_latest_value_cache() -> None:

assert cache.get() == 19

assert cache.keys() == set()


@pytest.mark.integration
async def test_latest_value_cache_key() -> None:
Expand All @@ -59,6 +61,8 @@ async def test_latest_value_cache_key() -> None:
with pytest.raises(ValueError, match="No value received for key: 0"):
cache.get(0)

assert cache.keys() == set()

await sender.send((5, "a"))
await sender.send((6, "b"))
await sender.send((5, "c"))
Expand All @@ -73,6 +77,8 @@ async def test_latest_value_cache_key() -> None:
assert cache.get(5) == (5, "c")
assert cache.get(6) == (6, "b")

assert cache.keys() == {5, 6}

with pytest.raises(ValueError, match="No value received for key: 7"):
cache.get(7)

Expand All @@ -92,3 +98,5 @@ async def test_latest_value_cache_key() -> None:
await asyncio.sleep(0)

assert cache.get(6) == (6, "g")

assert cache.keys() == {5, 6, 12}
Loading