forked from frequenz-floss/frequenz-channels-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_latest_value_cache_integration.py
More file actions
94 lines (68 loc) · 2.42 KB
/
Copy pathtest_latest_value_cache_integration.py
File metadata and controls
94 lines (68 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
"""Tests for the LatestValueCache implementation."""
import asyncio
import pytest
from frequenz.channels import Broadcast, LatestValueCache
@pytest.mark.integration
async def test_latest_value_cache() -> None:
"""Ensure LatestValueCache always gives out the latest value."""
channel = Broadcast[int](name="lvc_test")
cache = LatestValueCache(channel.new_receiver())
sender = channel.new_sender()
assert not cache.has_value()
with pytest.raises(ValueError, match="No value has been received yet."):
cache.get()
await sender.send(5)
await sender.send(6)
await asyncio.sleep(0)
assert cache.has_value()
assert cache.get() == 6
assert cache.get() == 6
await sender.send(12)
await asyncio.sleep(0)
assert cache.get() == 12
assert cache.get() == 12
assert cache.get() == 12
await sender.send(15)
await sender.send(18)
await sender.send(19)
await asyncio.sleep(0)
assert cache.get() == 19
@pytest.mark.integration
async def test_latest_value_cache_key() -> None:
"""Ensure LatestValueCache works with keys."""
channel = Broadcast[tuple[int, str]](name="lvc_test")
cache = LatestValueCache(channel.new_receiver(), key=lambda x: x[0])
sender = channel.new_sender()
assert not cache.has_value()
with pytest.raises(ValueError, match="No value has been received yet."):
cache.get()
with pytest.raises(ValueError, match="No value received for key: 0"):
cache.get(0)
await sender.send((5, "a"))
await sender.send((6, "b"))
await sender.send((5, "c"))
await asyncio.sleep(0)
assert cache.has_value()
assert cache.has_value(5)
assert cache.has_value(6)
assert not cache.has_value(7)
assert cache.get() == (5, "c")
assert cache.get(5) == (5, "c")
assert cache.get(6) == (6, "b")
with pytest.raises(ValueError, match="No value received for key: 7"):
cache.get(7)
await sender.send((12, "d"))
await asyncio.sleep(0)
assert cache.get() == (12, "d")
assert cache.get() == (12, "d")
assert cache.get(12) == (12, "d")
assert cache.get(12) == (12, "d")
assert cache.get(5) == (5, "c")
assert cache.get(6) == (6, "b")
await sender.send((6, "e"))
await sender.send((6, "f"))
await sender.send((6, "g"))
await asyncio.sleep(0)
assert cache.get(6) == (6, "g")