Skip to content

Commit d0d87e3

Browse files
committed
Add a ReceiverInvalidatedError
This is raised when the receiver was invalidated, for example when it was converted into a Peekable. Fixes #52. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 74d5a93 commit d0d87e3

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
`Receiver.consume()` doesn't raise any exceptions.
1414

15-
Receivers raising `EOFError` now raise `ReceiverError` instead.
15+
Receivers raising `EOFError` now raise `ReceiverInvalidatedError` instead.
1616

1717
* For channels which senders raise an error when the channel is closed or which receivers stop receiving when the channel is closed, the `SenderError` and `ReceiverStoppedError` are chained with a `__cause__` that is a `ChannelClosedError` with the channel that was closed.
1818

@@ -30,6 +30,8 @@
3030

3131
* `ReceiverClosedError`: Raised when a receiver don't have more messages to receive.
3232

33+
* `ReceiverInvalidatedError`: Raised when a receiver was invalidated (for example it was converted into a `Peekable`).
34+
3335
## Bug Fixes
3436

3537
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

src/frequenz/channels/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
5656
* [ReceiverStoppedError][frequenz.channels.ReceiverStoppedError]: A receiver
5757
stopped producing messages.
58+
59+
* [ReceiverInvalidatedError][frequenz.channels.ReceiverInvalidatedError]:
60+
A receiver is not longer valid (for example if it was converted into
61+
a peekable.
5862
"""
5963

6064
from . import util
@@ -67,6 +71,7 @@
6771
ChannelError,
6872
Error,
6973
ReceiverError,
74+
ReceiverInvalidatedError,
7075
ReceiverStoppedError,
7176
SenderError,
7277
)
@@ -81,6 +86,7 @@
8186
"Peekable",
8287
"Receiver",
8388
"ReceiverError",
89+
"ReceiverInvalidatedError",
8490
"ReceiverStoppedError",
8591
"Sender",
8692
"SenderError",

src/frequenz/channels/_broadcast.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from ._base_classes import T
1919
from ._exceptions import (
2020
ChannelClosedError,
21-
ReceiverError,
21+
ReceiverInvalidatedError,
2222
ReceiverStoppedError,
2323
SenderError,
2424
)
@@ -262,10 +262,14 @@ async def ready(self) -> None:
262262
263263
Raises:
264264
ReceiverStoppedError: if there is some problem with the receiver.
265-
ReceiverError: if the receiver is not longer active.
265+
ReceiverInvalidatedError: if the receiver was converted into
266+
a peekable.
266267
"""
267268
if not self._active:
268-
raise ReceiverError("This receiver is no longer active.", self)
269+
raise ReceiverInvalidatedError(
270+
"This receiver was converted into a Peekable so it is not longer valid.",
271+
self,
272+
)
269273

270274
# Use a while loop here, to handle spurious wakeups of condition variables.
271275
#

src/frequenz/channels/_exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ def __init__(self, receiver: _base_classes.Receiver[T]):
104104
error happened.
105105
"""
106106
super().__init__(f"Receiver {receiver} was stopped", receiver)
107+
108+
109+
class ReceiverInvalidatedError(ReceiverError[T]):
110+
"""The [Receiver][frequenz.channels.Receiver] was invalidated.
111+
112+
This happens when the Receiver is converted
113+
[into][frequenz.channels.Receiver.into_peekable]
114+
a [Peekable][frequenz.channels.Peekable].
115+
"""

tests/test_broadcast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Broadcast,
1313
ChannelClosedError,
1414
Receiver,
15-
ReceiverError,
15+
ReceiverInvalidatedError,
1616
ReceiverStoppedError,
1717
Sender,
1818
SenderError,
@@ -178,7 +178,7 @@ async def test_broadcast_peek() -> None:
178178
peekable = receiver.into_peekable()
179179
sender = bcast.new_sender()
180180

181-
with pytest.raises(ReceiverError, match=r"This receiver is no longer active."):
181+
with pytest.raises(ReceiverInvalidatedError):
182182
await receiver.receive()
183183

184184
assert peekable.peek() is None

0 commit comments

Comments
 (0)