You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Great to see you're tackling this key aspect. You're absolutely right — handling exceptions from trio.nursery tasks in unit tests is crucial for ensuring reliable test outcomes, especially when it comes to things like SwarmException from SecurityUpgradeFailure and MuxerUpgradeFailure.
✅ Here's a quick Suggestion and an experiment we can try:
To ensure exceptions propagate correctly and cause the test to fail, there are a few steps we can take:
1. Catch Exceptions Inside the Task, but Propagate Them to the Parent:
In trio, exceptions raised in child tasks should generally propagate to the parent (like the nursery) unless handled otherwise. If you're catching exceptions inside the task handler but not allowing them to propagate, that could be why they're not showing up in our tests.
We can add proper exception handling inside the nursery tasks to ensure exceptions bubble up correctly. Here's an example:
asyncdefhandler():
try:
# Code that could raise exceptionsexceptExceptionasexc:
raiseSwarmException(f"Exception in handler: {str(exc)}") fromexc
This ensures that the exception gets raised back up to the nursery, allowing the unit test to catch it.
2. Ensure That Exceptions Are Caught in Test Framework:
If exceptions aren't propagating as expected, the test framework itself might need to be adjusted to catch these.
In the test case, we could wrap the nursery or the specific background tasks inside trio.testing, using trio.testing.expect_timeout or trio.testing.raises to catch any unexpected errors during test execution.
Example:
withtrio.testing.assertRaises(SwarmException):
# Run the code that should trigger the exceptionawaitsome_async_function()
Since PR Fix exception handling in TCPListener.listen() #593 aims to catch these exceptions, testing this properly will involve ensuring that the task you are testing correctly handles the exception. If the exceptions are internal to the task and not passed back up, it’s important to mock or simulate the behavior of these tasks in your unit tests.
Additionally, make sure your tests simulate the conditions where exceptions should arise (e.g., SecurityUpgradeFailure and MuxerUpgradeFailure) and ensure they are captured as part of the test assertions.
4. Ensure Your Tasks Are Running in the Right Context:
Sometimes, exceptions don’t propagate if they are running in a context that’s disconnected from the main event loop. Make sure that your background tasks are executed in the correct nursery scope and are correctly linked to the event loop during tests.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Reza,
Great to see you're tackling this key aspect. You're absolutely right — handling exceptions from
trio.nursery
tasks in unit tests is crucial for ensuring reliable test outcomes, especially when it comes to things likeSwarmException
fromSecurityUpgradeFailure
andMuxerUpgradeFailure
.✅ Here's a quick Suggestion and an experiment we can try:
To ensure exceptions propagate correctly and cause the test to fail, there are a few steps we can take:
1. Catch Exceptions Inside the Task, but Propagate Them to the Parent:
trio
, exceptions raised in child tasks should generally propagate to the parent (like the nursery) unless handled otherwise. If you're catching exceptions inside the task handler but not allowing them to propagate, that could be why they're not showing up in our tests.This ensures that the exception gets raised back up to the nursery, allowing the unit test to catch it.
2. Ensure That Exceptions Are Caught in Test Framework:
trio.testing
, usingtrio.testing.expect_timeout
ortrio.testing.raises
to catch any unexpected errors during test execution.Example:
3. Improving Test Coverage with PR #593:
TCPListener.listen()
#593 aims to catch these exceptions, testing this properly will involve ensuring that the task you are testing correctly handles the exception. If the exceptions are internal to the task and not passed back up, it’s important to mock or simulate the behavior of these tasks in your unit tests.SecurityUpgradeFailure
andMuxerUpgradeFailure
) and ensure they are captured as part of the test assertions.4. Ensure Your Tasks Are Running in the Right Context:
Beta Was this translation helpful? Give feedback.
All reactions