Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Clean up tests from runtime.block_on() and moving around Runtime and Handle #12941

Merged
merged 13 commits into from
Dec 16, 2022

Conversation

dmitry-markin
Copy link
Contributor

This is a follow-up PR to #12646. It aims to clean up tests from moving around tokio::runtime::Runtime and tokio::runtime::Handle, and replace multiple runtime.block_on() with #[tokio::test].

@dmitry-markin dmitry-markin added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Dec 15, 2022
Copy link
Contributor

@acatangiu acatangiu left a comment

Choose a reason for hiding this comment

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

Nice PR! Tests look so much better this way! I haven't finished the review, but have a preliminary question below:


let api = Arc::new(two_validators::TestApi {});
let beefy_peers = peers.iter().enumerate().map(|(id, key)| (id, key, api.clone())).collect();
runtime.spawn(initialize_beefy(&mut net, beefy_peers, min_block_delta));
tokio::spawn(initialize_beefy(&mut net, beefy_peers, min_block_delta));
Copy link
Contributor

@acatangiu acatangiu Dec 15, 2022

Choose a reason for hiding this comment

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

What happens with this task when the test ends, is it also killed because test is marked as #[tokio::test]?
I mean, when multiple #[tokio::test]s are run concurrently does tokio have some magic under the hood to know which tokio::spawn()ed task belongs to which test?

Old version of the code had clear task lifetime -> the lifetime of runtime; with this code, I'm not sure. If you know the answer, please also add a code-comment explanation for others like me 😄

Copy link
Contributor Author

@dmitry-markin dmitry-markin Dec 15, 2022

Choose a reason for hiding this comment

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

#[tokio::test] is equivalent to something like Runtime::new().unwrap().block_on(async {...}), so every test gets its own runtime that is dropped at the end. A little more accurate definition is in the docs.

@acatangiu I'm afraid there are too many tokio::spawn() across the PR to add comments (I counted 44 =)

Copy link
Contributor

Choose a reason for hiding this comment

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

thank you! tokio::test and tokio::spawn documentation clearly supports what you are saying, sorry for not researching it before asking. It's fine to skip the comments.

Comment on lines +47 to 54
futures::future::poll_fn::<(), _>(|cx| {
net.poll(cx);
for peer in 0..3 {
if net.peer(peer).num_peers() != 2 {
return Poll::Pending
}
}
Poll::Ready(())
Copy link
Contributor

Choose a reason for hiding this comment

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

I still find it suspicious that for a simple test like this we need 2 threads.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just put flavor = "multi_thread" into every test in sync.rs to not spend too much time on thinking, which tests need it and which doesn't, and also to make the file consistent. In fact, only the following tests hang on a single-threaded runtime:

sync::block_announce_data_is_propagated
sync::multiple_requests_are_accepted_as_long_as_they_are_not_fulfilled
sync::sync_blocks_when_block_announce_validator_says_it_is_new_best
sync::sync_to_tip_when_we_sync_together_with_multiple_peers
sync::syncs_header_only_forks
sync::wait_until_deferred_block_announce_validation_is_ready

The others run fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, why do all these sync tests now need multi-threaded runtime? they should work in single thread too right? (also previous code version looks like it was single threaded runtime)

Copy link
Contributor Author

@dmitry-markin dmitry-markin Dec 16, 2022

Choose a reason for hiding this comment

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

They should, but they don't due to some nasty blocking/uneven polling/etc. issues. Previously, the runtime created explicitly was multi-threaded (by default), and these issues were not obvious. I tried investigating why, but didn't succeed. As we discussed with @bkchr and @altonen, it doesn't worth putting more time into it, so I just put flavor = "multi_thread" there.

client/finality-grandpa/src/tests.rs Show resolved Hide resolved
Copy link
Contributor

@acatangiu acatangiu left a comment

Choose a reason for hiding this comment

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

Changes look good

Comment on lines +47 to 54
futures::future::poll_fn::<(), _>(|cx| {
net.poll(cx);
for peer in 0..3 {
if net.peer(peer).num_peers() != 2 {
return Poll::Pending
}
}
Poll::Ready(())
Copy link
Contributor

Choose a reason for hiding this comment

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

+1, why do all these sync tests now need multi-threaded runtime? they should work in single thread too right? (also previous code version looks like it was single threaded runtime)

@altonen altonen self-requested a review December 16, 2022 13:04
ltfschoen pushed a commit to ltfschoen/substrate that referenced this pull request Feb 22, 2023
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants