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

Make tokio::test attribute compatible with sealed_test attribute #7045

Closed
fozzbuzz opened this issue Dec 18, 2024 · 4 comments
Closed

Make tokio::test attribute compatible with sealed_test attribute #7045

fozzbuzz opened this issue Dec 18, 2024 · 4 comments
Labels
A-tokio-macros Area: The tokio-macros crate C-feature-request Category: A feature request.

Comments

@fozzbuzz
Copy link

Is your feature request related to a problem? Please describe.
I use sealed_test crate for marking tests with sealed_test attribute to isolate environments. This does not function when combined with tokio::test attribute, which is desired for tests that need an isolated environment plus tokio time manipulation.
Looking at generated code for a test marked with both tokio::test and sealed_test , it appears that tokio::test adds the #[::core::prelude::v1::test] attribute while sealed_test adds the #[test] attribute; those two attributes combined make the test run twice, which fails since the environment is supposed to be isolated and this affects socket binds due to "Address in use" error.

Describe the solution you'd like
tokio::test attribute is able to be used in combination with sealed_test crate's attribute.

Describe alternatives you've considered
No real solution I can think of; to work around, I create a tokio runtime manually and spawn/block-on async code when using sealed_test .

Additional context
N/A

@fozzbuzz fozzbuzz added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Dec 18, 2024
@Darksonn
Copy link
Contributor

You can use #[tokio::main] instead of #[tokio::test].

@Darksonn Darksonn added A-tokio-macros Area: The tokio-macros crate and removed A-tokio Area: The main tokio crate labels Dec 18, 2024
@fozzbuzz
Copy link
Author

fozzbuzz commented Dec 18, 2024

Thank you @Darksonn . Using #[tokio::main(flavor = "current_thread") allowed me to use tokio::time::pause() at the beginning of the test, instead of using #[tokio::test(start_paused = true] .

@Darksonn
Copy link
Contributor

I'm pretty sure that start_paused also works with #[tokio::main] as long as the runtime flavor is current_thread.

@fozzbuzz
Copy link
Author

You're right, attempting to use start_paused without specifying flavor = "current_thread" results in compilation error indicating your advice. #[tokio::main(start_paused = true, flavor = "current_thread")] works. Thank you for your swift advice!

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

No branches or pull requests

2 participants