-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: Cancel task if background task fails
Cancel the foreground task as soon as the background task fails and raise the exception of the background task. With that, background task failures can be noticed immediately, not only after stopping the process.
- Loading branch information
Showing
5 changed files
with
154 additions
and
76 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
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
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,26 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
import not_my_board._util as util | ||
|
||
|
||
async def test_background_task(): | ||
async with util.background_task(blocking_task()) as task: | ||
assert not task.done() | ||
assert task.cancelled() | ||
|
||
|
||
async def test_background_task_failed(): | ||
with pytest.raises(RuntimeError) as execinfo: | ||
async with util.background_task(failing_task()): | ||
await asyncio.sleep(3) | ||
assert "Dummy Error" in str(execinfo.value) | ||
|
||
|
||
async def blocking_task(): | ||
await asyncio.Event().wait() | ||
|
||
|
||
async def failing_task(): | ||
raise RuntimeError("Dummy Error") |