Skip to content

Do not panic when a test function returns Result::Err. #100451

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 1 commit into from
Oct 2, 2022

Conversation

hovinen
Copy link

@hovinen hovinen commented Aug 12, 2022

Rust's test library allows test functions to return a Result, so that the test is deemed to have failed if the function returns a Result::Err variant. Currently, this works by having Result implement the Termination trait and asserting in assert_test_result that Termination::report() indicates successful completion. This turns a Result::Err into a panic, which is caught and unwound in the test library.

This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by:

  • Compiling all code with --panic=abort to avoid having to generate unwinding tables, and
  • Running most tests in-process to avoid the overhead of spawning new processes.

This change removes the intermediate panic step and passes a Result::Err directly through to the test runner.

To do this, it modifies assert_test_result to return a Result<(), String> where the Err variant holds what was previously the panic message. It changes the types in the TestFn enum to return Result<(), String>.

This tries to minimise the changes to benchmark tests, so it calls unwrap() on the Result returned by assert_test_result, effectively keeping the same behaviour as before.

Some questions for reviewers:

  • Does the change to the return types in the enum TestFn constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable.
  • Is there a way to test this change, i.e., to test that no panic occurs if a test returns Result::Err?
  • Is there a shorter, more idiomatic way to fold Result<Result<T,E>,E> into a Result<T,E> than the fold_err function I added?

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Aug 12, 2022
@rustbot
Copy link
Collaborator

rustbot commented Aug 12, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 12, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@hovinen hovinen force-pushed the no-panic-on-result-err-in-test branch from ed1fcdc to ea2e724 Compare August 12, 2022 13:16
@Mark-Simulacrum
Copy link
Member

Does the change to the return types in the enum TestFn constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable.

"Yes", but it's generally fine to change unstable APIs, so this is fine as-is. Based on not needing to update compiletest it'll affect a very small fraction of users most likely, so I'm not worried.

Is there a way to test this change, i.e., to test that no panic occurs if a test returns Result::Err?

Hm, I'm not sure. I would have suggested -Cpanic=abort and --test would work, but it looks like rustc currently hard errors on that (without -Zpanic_abort_tests, which IIRC spawns separate processes). It's probably OK to go without a test here.

Is there a shorter, more idiomatic way to fold Result<Result<T, E>, E> into a Result<T, E> than the fold_err function I added?

Not that I'm aware of.


Running most tests in-process to avoid the overhead of spawning new processes.

Can you say more about this? Are you expecting a future patch to differentiate between Result returning tests and those that don't, and only spawn processes for the latter? It seems to me that we'd be unable to make that delta unless we're willing to accept panics in result-returning tests aborting the test harness entirely (maybe yes, but not an obvious tradeoff to me).

@Mark-Simulacrum
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2022
@hovinen
Copy link
Author

hovinen commented Aug 26, 2022

Thanks for the quick review!

(Also sorry for the late reply; I was on vacation last week.)

Running most tests in-process to avoid the overhead of spawning new processes.

Can you say more about this? Are you expecting a future patch to differentiate between Result returning tests and those that don't, and only spawn processes for the latter? It seems to me that we'd be unable to make that delta unless we're willing to accept panics in result-returning tests aborting the test harness entirely (maybe yes, but not an obvious tradeoff to me).

In our systems it is most likely that (nearly) all tests will return Result and panics will only occur in exceptional cases. We won't likely use the existing assertion macros like assert_eq! but rather represent test failures by having the test return a Result::Err. This is similar to how GoogleTest works for C++, where the ASSERT_* macros just return from the current function rather than throwing an exception or similar (see https://google.github.io/googletest/primer.html#assertions).

So no, we won't have any need to differentiate between Result-returning and non-Result-returning test functions in deciding whether to spawn separate test processes.

@hovinen hovinen force-pushed the no-panic-on-result-err-in-test branch from 1aadd16 to 67f857b Compare August 26, 2022 12:41
@Mark-Simulacrum
Copy link
Member

@rustbot ready

Putting this back in my review queue.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 9, 2022
@Mark-Simulacrum
Copy link
Member

Please squash in the fixup commits (e.g., formatting, review comments).

Looking over this again I think part of me wants to suggest we go all the way here and make it the responsibility of the test harness (or similar) to catch the panics very close to the test itself, and then have the rest of the code work purely with a Result. The code here feels pretty spread out to me though and I'm not sure there's a great place for that to happen. It doesn't feel like this patch is really improving that situation but it also doesn't feel like it makes it worse, just kind of neutral.

I think I'm inclined to move ahead -- perhaps with a possible test ala #100451 (comment) added around that comment.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 16, 2022
@hovinen hovinen force-pushed the no-panic-on-result-err-in-test branch from 67f857b to 7c4fee2 Compare September 16, 2022 08:51
@hovinen
Copy link
Author

hovinen commented Sep 16, 2022

Please squash in the fixup commits (e.g., formatting, review comments).

Done.

Looking over this again I think part of me wants to suggest we go all the way here and make it the responsibility of the test harness (or similar) to catch the panics very close to the test itself, and then have the rest of the code work purely with a Result. The code here feels pretty spread out to me though and I'm not sure there's a great place for that to happen. It doesn't feel like this patch is really improving that situation but it also doesn't feel like it makes it worse, just kind of neutral.

I agree.

I think I'm inclined to move ahead -- perhaps with a possible test ala #100451 (comment) added around that comment.

Sounds good, thanks! As mentioned in the comment above, it seems the test to which you linked does in fact catch that problem -- for static benches, anyway -- but was somehow not triggered by CI. I'd be happy to write a test to cover the dynamic benchmark case if you can point me to how to write dynamic benches.

@hovinen hovinen force-pushed the no-panic-on-result-err-in-test branch from 7c4fee2 to 6522223 Compare September 16, 2022 14:05
Rust's test library allows test functions to return a Result, so that the test is deemed to have failed if the function returns a Result::Err variant. Currently, this works by having Result implement the Termination trait and asserting in assert_test_result that Termination::report() indicates successful completion. This turns a Result::Err into a panic, which is caught and unwound in the test library.

This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by:

 * Compiling all code with --panic=abort to avoid having to generate unwinding tables, and
 * Running most tests in-process to avoid the overhead of spawning new processes.

This change removes the intermediate panic step and passes a Result::Err directly through to the test runner.

To do this, it modifies assert_test_result to return a Result<(), String> where the Err variant holds what was previously the panic message. It changes the types in the TestFn enum to return Result<(), String>.

This tries to minimise the changes to benchmark tests, so it calls unwrap() on the Result returned by assert_test_result, effectively keeping the same behaviour as before.
@hovinen hovinen force-pushed the no-panic-on-result-err-in-test branch from 6522223 to e19a98c Compare September 16, 2022 14:39
@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 1, 2022
@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 1, 2022

📌 Commit e19a98c has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 1, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Oct 2, 2022
…test, r=Mark-Simulacrum

Do not panic when a test function returns Result::Err.

Rust's test library allows test functions to return a `Result`, so that the test is deemed to have failed if the function returns a `Result::Err` variant. Currently, this works by having `Result` implement the `Termination` trait and asserting in assert_test_result that `Termination::report()` indicates successful completion. This turns a `Result::Err` into a panic, which is caught and unwound in the test library.

This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by:

 * Compiling all code with `--panic=abort` to avoid having to generate unwinding tables, and
 * Running most tests in-process to avoid the overhead of spawning new processes.

This change removes the intermediate panic step and passes a `Result::Err` directly through to the test runner.

To do this, it modifies `assert_test_result` to return a `Result<(), String>` where the `Err` variant holds what was previously the panic message. It changes the types in the `TestFn` enum to return `Result<(), String>`.

This tries to minimise the changes to benchmark tests, so it calls `unwrap()` on the `Result` returned by `assert_test_result`, effectively keeping the same behaviour as before.

Some questions for reviewers:

 * Does the change to the return types in the enum `TestFn` constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable.
 * Is there a way to test this change, i.e., to test that no panic occurs if a test returns `Result::Err`?
 * Is there a shorter, more idiomatic way to fold `Result<Result<T,E>,E>` into a `Result<T,E>` than the `fold_err` function I added?
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Oct 2, 2022
…test, r=Mark-Simulacrum

Do not panic when a test function returns Result::Err.

Rust's test library allows test functions to return a `Result`, so that the test is deemed to have failed if the function returns a `Result::Err` variant. Currently, this works by having `Result` implement the `Termination` trait and asserting in assert_test_result that `Termination::report()` indicates successful completion. This turns a `Result::Err` into a panic, which is caught and unwound in the test library.

This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by:

 * Compiling all code with `--panic=abort` to avoid having to generate unwinding tables, and
 * Running most tests in-process to avoid the overhead of spawning new processes.

This change removes the intermediate panic step and passes a `Result::Err` directly through to the test runner.

To do this, it modifies `assert_test_result` to return a `Result<(), String>` where the `Err` variant holds what was previously the panic message. It changes the types in the `TestFn` enum to return `Result<(), String>`.

This tries to minimise the changes to benchmark tests, so it calls `unwrap()` on the `Result` returned by `assert_test_result`, effectively keeping the same behaviour as before.

Some questions for reviewers:

 * Does the change to the return types in the enum `TestFn` constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable.
 * Is there a way to test this change, i.e., to test that no panic occurs if a test returns `Result::Err`?
 * Is there a shorter, more idiomatic way to fold `Result<Result<T,E>,E>` into a `Result<T,E>` than the `fold_err` function I added?
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 2, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#100451 (Do not panic when a test function returns Result::Err.)
 - rust-lang#102098 (Use fetch_update in sync::Weak::upgrade)
 - rust-lang#102538 (Give `def_span` the same SyntaxContext as `span_with_body`.)
 - rust-lang#102556 (Make `feature(const_btree_len)` implied by `feature(const_btree_new)`)
 - rust-lang#102566 (Add a known-bug test for rust-lang#102498)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 13f47f6 into rust-lang:master Oct 2, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 2, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Rollup of 5 pull requests

Successful merges:

 - rust-lang#100451 (Do not panic when a test function returns Result::Err.)
 - rust-lang#102098 (Use fetch_update in sync::Weak::upgrade)
 - rust-lang#102538 (Give `def_span` the same SyntaxContext as `span_with_body`.)
 - rust-lang#102556 (Make `feature(const_btree_len)` implied by `feature(const_btree_new)`)
 - rust-lang#102566 (Add a known-bug test for rust-lang#102498)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants