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

test: Helpful panic messages for tokio_test::io #7212

Merged
merged 2 commits into from
Mar 12, 2025

Conversation

vi
Copy link
Contributor

@vi vi commented Mar 11, 2025

Introduce tokio_test::io::Builder::name to configure name of the mock object, to include in panic messages.

Also show number of remaining actions or action index in some cases to help debugging failed tests.

Motivation

When using tokio_test::io::Mock, it is tricky to debug problems, especially if multiple mocks are used. For example, if it panics with "unexpected write", it is hard to determine which of the mock objects faced the unexpected write and at what position of the scenario.

Solution

Builder gained name method to set make Mock remember a string.

Panic messages are adjusted to include that name (if set), also some additional information (like the number of remaining actions) to aid hack-try-print-repeat-based debugging sessions.

Before:

unexpected write

After:

unexpected write (name my_mock_name, 0 actions remain)

Introduce tokio_test::io::Builder::name to configure
name of the mock object, to include in panic messages.

Also show number of remaining actions or action index
in some cases to help debugging failed tests.
@mox692 mox692 added the A-tokio-test Area: The tokio-test crate label Mar 12, 2025
Copy link
Member

@mox692 mox692 left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@mox692 mox692 merged commit 72c87a7 into tokio-rs:master Mar 12, 2025
82 checks passed
@vi
Copy link
Contributor Author

vi commented Mar 12, 2025

Shall I submit further additions to io::Mock (e.g. ability to ignore unexpected things after certain points or explicitly inject EOF in reading part while still expecting more writes to come) or it would better suit in a separate crate?

@mox692
Copy link
Member

mox692 commented Mar 14, 2025

If that feature can be implemented outside of tokio, then it would generally be better to implement it elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-test Area: The tokio-test crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants