Skip to content

Commit 25685be

Browse files
committed
Add tests for the new ChannelRegistry methods
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 0f35949 commit 25685be

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

tests/actor/test_channel_registry.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33

44
"""Tests for the ChannelRegistry."""
55

6+
import pytest
7+
from frequenz.channels import ReceiverError, SenderError
8+
69
from frequenz.sdk.actor import ChannelRegistry
710

811

912
async def test_channel_registry() -> None:
1013
"""Tests for ChannelRegistry, with string as key type."""
1114
reg = ChannelRegistry(name="test-registry")
1215

13-
sender20 = reg.new_sender("20-hello")
14-
receiver20 = reg.new_receiver("20-hello")
16+
assert "20-hello" not in reg
17+
assert "21-hello" not in reg
18+
19+
chan20 = reg.get_or_create(int, "20-hello")
20+
assert "20-hello" in reg
21+
assert reg.message_type("20-hello") == int
22+
23+
with pytest.raises(ValueError):
24+
reg.get_or_create(str, "20-hello")
25+
26+
sender20 = chan20.new_sender()
27+
receiver20 = chan20.new_receiver()
1528

16-
sender21 = reg.new_sender("21-hello")
17-
receiver21 = reg.new_receiver("21-hello")
29+
assert "21-hello" not in reg
30+
31+
chan21 = reg.get_or_create(int, "21-hello")
32+
assert "21-hello" in reg
33+
assert reg.message_type("21-hello") == int
34+
35+
sender21 = chan21.new_sender()
36+
receiver21 = chan21.new_receiver()
1837

1938
await sender20.send(30)
2039
await sender21.send(31)
@@ -24,3 +43,27 @@ async def test_channel_registry() -> None:
2443

2544
rcvd = await receiver20.receive()
2645
assert rcvd == 30
46+
47+
await reg.close_and_remove("20-hello")
48+
assert "20-hello" not in reg
49+
assert chan20._closed # pylint: disable=protected-access
50+
with pytest.raises(SenderError):
51+
await sender20.send(30)
52+
with pytest.raises(ReceiverError):
53+
await receiver20.receive()
54+
with pytest.raises(KeyError):
55+
reg.message_type("20-hello")
56+
with pytest.raises(KeyError):
57+
await reg.close_and_remove("20-hello")
58+
59+
await reg.close_and_remove("21-hello")
60+
assert "21-hello" not in reg
61+
assert chan21._closed # pylint: disable=protected-access
62+
with pytest.raises(SenderError):
63+
await sender21.send(30)
64+
with pytest.raises(ReceiverError):
65+
await receiver21.receive()
66+
with pytest.raises(KeyError):
67+
reg.message_type("21-hello")
68+
with pytest.raises(KeyError):
69+
await reg.close_and_remove("21-hello")

0 commit comments

Comments
 (0)