Skip to content

Commit 19f503c

Browse files
committed
Bidirectional: unify client_id and service_id
This is only used to create the underlying `Broadcast` channel's names, so instead of using 2 separate strings, just use a plain `name` as with other channels. Also like with other channels, make the `name` optional and default to a more readable `id(self)` representation (using `_` separators). Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 3d14012 commit 19f503c

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

src/frequenz/channels/_bidirectional.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,19 @@ def consume(self) -> W:
112112
err.__cause__ = this_chan_error
113113
raise err
114114

115-
def __init__(self, *, client_id: str, service_id: str) -> None:
115+
def __init__(self, *, name: str | None = None) -> None:
116116
"""Create a `Bidirectional` instance.
117117
118118
Args:
119-
client_id: A name for the client, used to name the channels.
120-
service_id: A name for the service end of the channels.
119+
name: A name for the client, used to name the channels.
121120
"""
122-
self._client_id: str = client_id
121+
self._name: str = f"{id(self):_}" if name is None else name
123122
"""The name for the client, used to name the channels."""
124123

125-
self._request_channel: Broadcast[T] = Broadcast(
126-
name=f"req_{service_id}_{client_id}"
127-
)
124+
self._request_channel: Broadcast[T] = Broadcast(name=f"{self._name}:request")
128125
"""The channel to send requests."""
129126

130-
self._response_channel: Broadcast[U] = Broadcast(
131-
name=f"resp_{service_id}_{client_id}"
132-
)
127+
self._response_channel: Broadcast[U] = Broadcast(name=f"{self._name}:response")
133128
"""The channel to send responses."""
134129

135130
self._client_handle: Bidirectional.Handle[T, U] = Bidirectional.Handle(

tests/test_bidirectional.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
async def test_request_response() -> None:
2020
"""Ensure bi-directional communication is possible."""
21-
req_resp: Bidirectional[int, str] = Bidirectional(
22-
client_id="test_client", service_id="test_service"
23-
)
21+
req_resp: Bidirectional[int, str] = Bidirectional(name="test_service")
2422

2523
async def service(handle: Bidirectional.Handle[str, int]) -> None:
2624
while True:
@@ -54,9 +52,7 @@ async def service(handle: Bidirectional.Handle[str, int]) -> None:
5452

5553
async def test_sender_error_chaining() -> None:
5654
"""Ensure bi-directional communication is possible."""
57-
req_resp: Bidirectional[int, str] = Bidirectional(
58-
client_id="test_client", service_id="test_service"
59-
)
55+
req_resp: Bidirectional[int, str] = Bidirectional(name="test_service")
6056

6157
await req_resp._response_channel.close() # pylint: disable=protected-access
6258

@@ -72,9 +68,7 @@ async def test_sender_error_chaining() -> None:
7268

7369
async def test_consume_error_chaining() -> None:
7470
"""Ensure bi-directional communication is possible."""
75-
req_resp: Bidirectional[int, str] = Bidirectional(
76-
client_id="test_client", service_id="test_service"
77-
)
71+
req_resp: Bidirectional[int, str] = Bidirectional(name="test_service")
7872

7973
await req_resp._request_channel.close() # pylint: disable=protected-access
8074

0 commit comments

Comments
 (0)