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(clitools): revive run_inprocess() #3891

Merged
merged 2 commits into from
Jun 18, 2024

Conversation

rami3l
Copy link
Member

@rami3l rami3l commented Jun 18, 2024

This PR re-enables testing via the run_inprocess() function, which was originally introduced in #2367 but temporarily disabled in #3868.

Please note that this is a dirty fix as so far all in-process tests are still synchronous, and a tokio runtime builder has been re-introduced to handle the async-ness of rustup_init::main() as well as the need of initializing an otel subscriber in an async context.

@rami3l rami3l force-pushed the feat/inprocess-testing branch from bee5aee to 8a77505 Compare June 18, 2024 02:54
@rami3l rami3l force-pushed the feat/inprocess-testing branch from 8a77505 to 2261e26 Compare June 18, 2024 02:57
@rami3l rami3l requested a review from djc June 18, 2024 03:06
@rami3l rami3l added this to the 1.28.0 milestone Jun 18, 2024
@rami3l rami3l requested a review from rbtcollins June 18, 2024 03:11
@djc
Copy link
Contributor

djc commented Jun 18, 2024

Please note that this is a dirty fix as so far all in-process tests are still synchronous,

What do you mean? It seems pretty mucht right to me.

@rami3l
Copy link
Member Author

rami3l commented Jun 18, 2024

Please note that this is a dirty fix as so far all in-process tests are still synchronous,

What do you mean? It seems pretty mucht right to me.

@djc The previous implementation used a unique runtime and a static TRACER that is initialized in the former for all in-process tests. However once process is involved that seems no longer possible.

@djc
Copy link
Contributor

djc commented Jun 18, 2024

What do you mean? It seems pretty mucht right to me.

@djc The previous implementation used a unique runtime and a static TRACER that is initialized in the former for all in-process tests. However once process is involved that seems no longer possible.

Do you mean because I've tied the DefaultGuard into the TestContext? That's something we can just change if we want to. I suppose the issue here is that we have different kinds of tests that need the subscriber, these in-process tests but also "simpler"/more unit-style tests that benefit from the current setup. However we could for example add a third variant to Process for this maybe?

@rami3l
Copy link
Member Author

rami3l commented Jun 18, 2024

Do you mean because I've tied the DefaultGuard into the TestContext? That's something we can just change if we want to. I suppose the issue here is that we have different kinds of tests that need the subscriber, these in-process tests but also "simpler"/more unit-style tests that benefit from the current setup. However we could for example add a third variant to Process for this maybe?

@djc Indeed, that would be interesting.

One trap I've encountered here is that currently TestContext cannot be constructed without an async context (or it'll panic), and that's why a big async block is required in the code above. Not surprising to me, but it's still a bit easy for me to accidentally make the mistake of initializing it at a random place and produce a panic (x_x)

@djc
Copy link
Contributor

djc commented Jun 18, 2024

Do you mean because I've tied the DefaultGuard into the TestContext? That's something we can just change if we want to. I suppose the issue here is that we have different kinds of tests that need the subscriber, these in-process tests but also "simpler"/more unit-style tests that benefit from the current setup. However we could for example add a third variant to Process for this maybe?

@djc Indeed, that would be interesting.

One trap I've encountered here is that currently TestContext cannot be constructed without an async context (or it'll panic), and that's why a big async block is required in the code above. Not surprising to me, but it's still a bit easy for me to accidentally make the mistake of initializing it at a random place and produce a panic (x_x)

We could explicitly stick a Runtime in it, but it feels like overkill? Ideally in my mind we would not have the Runtime construction in the code and instead annotate all tests that need it with #[tokio::test], which seems like the idiomatic approach to me.

@rami3l
Copy link
Member Author

rami3l commented Jun 18, 2024

Ideally in my mind we would not have the Runtime construction in the code and instead annotate all tests that need it with #[tokio::test], which seems like the idiomatic approach to me.

@djc That's also where I want it to be, however the current state is still far from it.

@djc
Copy link
Contributor

djc commented Jun 18, 2024

Ideally in my mind we would not have the Runtime construction in the code and instead annotate all tests that need it with #[tokio::test], which seems like the idiomatic approach to me.

@djc That's also where I want it to be, however the current state is still far from it.

Why far? If we just remove the explicit runtime builder and add #[tokio::test] to the tests that need it, what breaks?

@rami3l
Copy link
Member Author

rami3l commented Jun 18, 2024

Ideally in my mind we would not have the Runtime construction in the code and instead annotate all tests that need it with #[tokio::test], which seems like the idiomatic approach to me.

@djc That's also where I want it to be, however the current state is still far from it.

Why far? If we just remove the explicit runtime builder and add #[tokio::test] to the tests that need it, what breaks?

@djc Nothing in theory. However, as I've explained already, it's just that run_inprocess() is a bit deep down already in the call chain, and it's only there we start to observe asyncness (meaning only its callees can be async), so as for the current solution all its callers like test() and .run() don't require any changes.

OTOH, actually making the tests async (and adding #[tokio::test] to them) requires a nontrivial amount of effort to migrate those helper functions to async as well (which is worked around in this PR by pushing back the starting point of asyncness). But maybe you can have a try after this PR? I could be doing it wrong from the beginning, I'm not quite sure...

Anyway, I think this PR is ready to go as a first step. Please feel free to propose improvements in the subsequent ones.

@rami3l rami3l added this pull request to the merge queue Jun 18, 2024
Merged via the queue into rust-lang:master with commit fd201e4 Jun 18, 2024
26 checks passed
@rami3l rami3l deleted the feat/inprocess-testing branch June 18, 2024 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants