File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ # License: MIT
2+ # Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+ """Tests for the LatestValueCache implementation."""
5+
6+ import asyncio
7+
8+ import pytest
9+
10+ from frequenz .channels import Broadcast , LatestValueCache
11+
12+
13+ @pytest .mark .integration
14+ async def test_latest_value_cache () -> None :
15+ """Ensure LatestValueCache always gives out the latest value."""
16+ channel = Broadcast [int ](name = "lvc_test" )
17+
18+ cache = LatestValueCache (channel .new_receiver ())
19+ sender = channel .new_sender ()
20+
21+ assert not cache .has_value ()
22+ with pytest .raises (ValueError , match = "No value has been received yet." ):
23+ cache .get ()
24+
25+ await sender .send (5 )
26+ await sender .send (6 )
27+ await asyncio .sleep (0 )
28+
29+ assert cache .has_value ()
30+ assert cache .get () == 6
31+ assert cache .get () == 6
32+
33+ await sender .send (12 )
34+ await asyncio .sleep (0 )
35+
36+ assert cache .get () == 12
37+ assert cache .get () == 12
38+ assert cache .get () == 12
39+
40+ await sender .send (15 )
41+ await sender .send (18 )
42+ await sender .send (19 )
43+ await asyncio .sleep (0 )
44+
45+ assert cache .get () == 19
You can’t perform that action at this time.
0 commit comments