Skip to content
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

Better handle the exceptions from child coroutines in runTest #2995

Conversation

dkhalanskyjb
Copy link
Collaborator

Fixes #1910

@dkhalanskyjb
Copy link
Collaborator Author

dkhalanskyjb commented Oct 22, 2021 via email

} catch (e: UncompletedCoroutinesError) {
// it's normal that some jobs are not completed if the test body has failed, won't clutter the output
} catch (e: Throwable) {
e.printStackTrace()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a leftover or deliberate code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tricky question! I deliberately left it as is so we could discuss it.

cleanupTestCoroutines, too, prints a bunch of exceptions to the console if there's more than one, only throwing a single one. It would be consistent here to print the only thrown exception, too, if it turns out that there's a more important exception (the one with which the testScope completed).

If I understood correctly, you advise to, instead, get the exception with which testScope completed, add a suppressed exception from the cleanup, and then just throw it, right? But then, for consistency, we would also need the other exceptions from cleanupTestCoroutines to be suppressed and not printed, right? This would somewhat complicate the API, and I'm not sure what would the benefits be, other than the ability to better test this test module (validating printed exceptions is a pain).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main benefits here are programmatic access for any test frameworks -- exception aggregation, logging on test failures on CI and so on.

Printing the error straight to the console seems neither controllable nor usable.

@dkhalanskyjb dkhalanskyjb changed the title Cancel the child coroutines if the test body has thrown Fix the handling of exceptions in child coroutines in runTest Oct 25, 2021
@dkhalanskyjb dkhalanskyjb changed the title Fix the handling of exceptions in child coroutines in runTest Better handle the exceptions from child coroutines in runTest Oct 25, 2021
@dkhalanskyjb dkhalanskyjb force-pushed the coroutines-test-exception-handling branch from d651dec to f158e36 Compare October 26, 2021 07:39
@dkhalanskyjb dkhalanskyjb force-pushed the coroutines-test-cancel-on-failure branch from f184e40 to eee46c5 Compare October 26, 2021 08:47
@@ -169,16 +179,16 @@ public fun runTest(
): TestResult {
if (context[RunningInRunTest] != null)
throw IllegalStateException("Calls to `runTest` can't be nested. Please read the docs on `TestResult` for details.")
val testScope = createTestCoroutineScope(context + RunningInRunTest())
val testScope = TestBodyCoroutine<Unit>(createTestCoroutineScope(context + RunningInRunTest()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] RunningInRunTest probably can be object with prettified toString

} catch (e: UncompletedCoroutinesError) {
// it's normal that some jobs are not completed if the test body has failed, won't clutter the output
} catch (e: Throwable) {
e.printStackTrace()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main benefits here are programmatic access for any test frameworks -- exception aggregation, logging on test failures on CI and so on.

Printing the error straight to the console seems neither controllable nor usable.

@qwwdfsad qwwdfsad self-requested a review October 26, 2021 12:03
@dkhalanskyjb dkhalanskyjb force-pushed the coroutines-test-cancel-on-failure branch from eee46c5 to 48bf565 Compare October 27, 2021 08:36
@dkhalanskyjb dkhalanskyjb changed the base branch from coroutines-test-exception-handling to coroutines-test-await-other-dispatchers October 27, 2021 08:37
@dkhalanskyjb dkhalanskyjb merged commit ba0cccd into coroutines-test-await-other-dispatchers Oct 27, 2021
@dkhalanskyjb dkhalanskyjb deleted the coroutines-test-cancel-on-failure branch October 27, 2021 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants