-
Notifications
You must be signed in to change notification settings - Fork 9
Implement a NopReceiver
#419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """A receiver that will never receive a message. | ||
| It is useful as a place-holder receiver for use in contexts where a receiver is | ||
| necessary, but one is not available. | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from typing_extensions import override | ||
|
|
||
| from frequenz.channels import Receiver, ReceiverError, ReceiverMessageT_co | ||
| from frequenz.channels._receiver import ReceiverStoppedError | ||
|
|
||
|
|
||
| class NopReceiver(Receiver[ReceiverMessageT_co]): | ||
| """A place-holder receiver that will never receive a message.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| """Initialize this instance.""" | ||
| self._closed: bool = False | ||
|
|
||
| @override | ||
| async def ready(self) -> bool: | ||
| """Wait for ever unless the receiver is closed. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forever |
||
| Returns: | ||
| Whether the receiver is still active. | ||
| """ | ||
| if self._closed: | ||
| return False | ||
| await asyncio.Future() | ||
| return False | ||
|
|
||
| @override | ||
| def consume(self) -> ReceiverMessageT_co: # noqa: DOC503 (raised indirectly) | ||
| """Raise `ReceiverError` unless the NopReceiver is closed. | ||
| If the receiver is closed, then raise `ReceiverStoppedError`. | ||
| Returns: | ||
| The next message received. | ||
| Raises: | ||
| ReceiverStoppedError: If the receiver stopped producing messages. | ||
| ReceiverError: If there is some problem with the underlying receiver. | ||
| """ | ||
| if self._closed: | ||
| raise ReceiverStoppedError(self) | ||
| raise ReceiverError("`consume()` must be preceded by a call to `ready()`", self) | ||
|
|
||
| @override | ||
| def close(self) -> None: | ||
| """Stop the receiver.""" | ||
| self._closed = True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Tests for the NopReceiver.""" | ||
|
|
||
| import asyncio | ||
| from contextlib import closing | ||
|
|
||
| import pytest | ||
|
|
||
| from frequenz.channels import ReceiverError | ||
| from frequenz.channels._receiver import ReceiverStoppedError | ||
| from frequenz.channels.experimental import NopReceiver | ||
|
|
||
|
|
||
| async def test_never_ready() -> None: | ||
| """Test that the receiver is never ready.""" | ||
| # When it is not closed, `ready()` should never return. | ||
| with closing(NopReceiver[int]()) as receiver: | ||
| with pytest.raises(TimeoutError): | ||
| await asyncio.wait_for(receiver.ready(), timeout=0.1) | ||
|
|
||
| # When it is closed, `ready()` should return False. | ||
| receiver = NopReceiver[int]() | ||
| receiver.close() | ||
| assert await asyncio.wait_for(receiver.ready(), timeout=0.1) is False | ||
|
|
||
|
|
||
| async def test_consuming_raises() -> None: | ||
| """Test that consume raises an error.""" | ||
| # When it is not closed, `consume()` should raise a ReceiverError. | ||
| with closing(NopReceiver[int]()) as receiver: | ||
| with pytest.raises(ReceiverError): | ||
| receiver.consume() | ||
|
|
||
| # When it is closed, `consume()` should raise a ReceiverStoppedError. | ||
| receiver = NopReceiver[int]() | ||
| receiver.close() | ||
| with pytest.raises(ReceiverStoppedError): | ||
| receiver.consume() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider sorting the all list alphabetically to improve readability and maintainability.