Hello Rustaceans! 
I’m currently writing a unit test for a simple async function that performs some string manipulation. When I run the function in my actual application using cargo run
, it completes in about 1 second — as expected.
However, when I run the same function in a unit test (using #[tokio::test]
or block_on
), the test takes around 40 seconds to complete. I’m puzzled as to why there’s such a huge difference in runtime between the normal execution and the test environment.
Look for things that could be different:
- different number of worker threads specified in
tokio::test
and tokio::main
(although this wouldn't matter if you're not spawning tasks)
- specifying
--release
when invoking cargo
Another thing is that cargo test
will run multiple tests in parallel, so to give a fair comparison you should run only the one unit test.
Thank you for replying.
I'm trying to run one test case at a time, and I also tried using cargo test --release
, but it didn't help — the test case still takes too long to run.
Please let me know if there's anything I can improve in this.
I don't have any other ideas. If you show the code and commands for both ways you're running the test (as a test and not as a test), then hopefully someone here can identify the cause.