Skip to content

Commit 6f24b25

Browse files
authored
LatestValueCache: Expose keys for which messages have been received (#425)
2 parents 3edcb49 + 19fa33b commit 6f24b25

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/frequenz/channels/_latest_value_cache.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import asyncio
4646
import typing
47+
from collections.abc import Set
4748

4849
from ._receiver import Receiver
4950

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

149+
def keys(self) -> Set[HashableT]:
150+
"""Return the set of keys for which values have been received.
151+
152+
If no key function is provided, this will return an empty set.
153+
"""
154+
return self._latest_value_by_key.keys()
155+
148156
def get(self, key: HashableT | Sentinel = NO_KEY) -> T_co:
149157
"""Return the latest value that has been received.
150158

tests/test_latest_value_cache_integration.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ async def test_latest_value_cache() -> None:
4444

4545
assert cache.get() == 19
4646

47+
assert cache.keys() == set()
48+
4749

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

64+
assert cache.keys() == set()
65+
6266
await sender.send((5, "a"))
6367
await sender.send((6, "b"))
6468
await sender.send((5, "c"))
@@ -73,6 +77,8 @@ async def test_latest_value_cache_key() -> None:
7377
assert cache.get(5) == (5, "c")
7478
assert cache.get(6) == (6, "b")
7579

80+
assert cache.keys() == {5, 6}
81+
7682
with pytest.raises(ValueError, match="No value received for key: 7"):
7783
cache.get(7)
7884

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

94100
assert cache.get(6) == (6, "g")
101+
102+
assert cache.keys() == {5, 6, 12}

0 commit comments

Comments
 (0)