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

Reorganize tokio::fs::file's mock tests. #3988

Merged
merged 5 commits into from
Jul 31, 2021
Merged

Conversation

asomers
Copy link
Contributor

@asomers asomers commented Jul 25, 2021

The new organization changes the tests from integration tests into unit
tests, and mocks std::fs::File with Mockall instead of custom logic.
This approach has several advantages:

  • Does not require recompiling tokio::fs's source to build the
    integration tests
  • Does not mangle modules with #[path]
  • Does not interfere with adding more modules to fs (my main motivation)
  • Requires less custom code
  • Does not require renaming JoinHandle and spawn_blocking

Motivation

The unusual design of tokio::fs's mock tests makes it very difficult to add additional submodules to tokio::fs. They would somehow have to be supplied by tokio/tests/fs_file_mocked.rs, and I couldn't figure out how to make it work.

Solution

Don't redefine src modules during the integration tests using #[path]. Instead, just run mock tests as unit tests, selectively using mock modules with #[cfg(test)]. That's how most other Rust projects do it.

The new organization changes the tests from integration tests into unit
tests, and mocks std::fs::File with Mockall instead of custom logic.
This approach has several advantages:

* Does not require recompiling tokio::fs's source to build the
  integration tests
* Does not mangle modules with #[path]
* Does not interfere with adding more modules to fs (my main motivation)
* Requires less custom code
* Does not require renaming JoinHandle and spawn_blocking
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thanks, this seems like a good idea.

tokio/src/fs/file.rs Outdated Show resolved Hide resolved
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-fs Module: tokio/fs labels Jul 26, 2021
@Darksonn
Copy link
Contributor

It seems like CI had a failure.

@asomers
Copy link
Contributor Author

asomers commented Jul 31, 2021

Anything left to do, @Darksonn ?

@Darksonn
Copy link
Contributor

No, it seems good. Thanks for doing this!

@Darksonn Darksonn merged commit cf02b3f into tokio-rs:master Jul 31, 2021
@asomers asomers deleted the mocks branch July 31, 2021 15:21
@asomers
Copy link
Contributor Author

asomers commented Jul 31, 2021

Thanks Darsonn. This will be necessary to have methods like File::write_at.

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

Successfully merging this pull request may close these issues.

2 participants