-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix subscriber not properly unsubscribing when an exception is raised…
… inside the context manager (#112) * add testcases for unsubsribe * fix the context manager of the function "subscribe" not removing the associated queue from the channel in case of a raised exception inside the context manager (e.g. being used inside a generator that gets closed raising GeneratorExit) * activate broadcaster via context manager --------- Co-authored-by: alex.oleshkevich <[email protected]>
- Loading branch information
1 parent
de6ef40
commit eeba6a7
Showing
2 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
import pytest | ||
from broadcaster import Broadcast | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_unsubscribe(): | ||
"""The queue should be removed when the context manager is left.""" | ||
async with Broadcast("memory://") as broadcast: | ||
async with broadcast.subscribe("chatroom"): | ||
pass | ||
|
||
assert "chatroom" not in broadcast._subscribers | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_unsubscribe_w_exception(): | ||
"""In case an exception is raised inside the context manager, the queue should be removed.""" | ||
async with Broadcast("memory://") as broadcast: | ||
try: | ||
async with broadcast.subscribe("chatroom"): | ||
raise RuntimeError("MyException") | ||
except RuntimeError: | ||
pass | ||
|
||
assert "chatroom" not in broadcast._subscribers |