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

refactor(test): execute all #[rustup_macros::unit_test]s within a tokio context #3868

Merged
merged 4 commits into from
Jun 14, 2024

Conversation

rami3l
Copy link
Member

@rami3l rami3l commented Jun 12, 2024

Split from #3803.

With this PR, all tests under the #[rustup_macros::unit_test] attribute now execute within a tokio runtime, eliminating the need of static TRACER and TRACE_RUNTIME variables. Also, the setup of tracing subscribers in both bin and test targets has also been unified, to prepare for the addition of another stderr-oriented subscriber in #3803.

Rationale

Currently, the tracing subscriber registration will fail if a tokio runtime is not present. To mitigate this problem, static TRACER and TRACE_RUNTIME variables are added as an ad-hoc fallback if we're running a non-tokio unit test.

rustup/src/test.rs

Lines 230 to 237 in 3ba08da

/// A tokio runtime for the sync tests, permitting the use of tracing. This is
/// never shutdown, instead it is just dropped at end of process.
#[cfg(feature = "otel")]
static TRACE_RUNTIME: Lazy<tokio::runtime::Runtime> =
Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
/// A tracer for the tests.
#[cfg(feature = "otel")]
static TRACER: Lazy<opentelemetry_sdk::trace::Tracer> = Lazy::new(|| {

However, the fact that the subscriber state is global in tests has made some unnecessary complications especially WRT adding other tracing subscribers, such as the stderr-oriented tracing subscriber proposed in #3803 (comment), which is bound to the local currentprocess:process() context (see #3803 (comment)).

I decided to go with local contexts all the way, and it did work. However, the main downside to this approach is that #2367 has to be reverted, since run_inprocess() calls with_runtime(), and for the latter to correctly alter static PROCESS in-process, a tokio runtime builder is required, which in this case will cause a "runtime-in-runtime" error once the builder is run.

Fortunately, no obvious slowdown has been observed WRT our CI so far. Also, as now main() seems to be the only caller to with_runtime(), this gets us closer to using #[tokio::main].

@rami3l

This comment was marked as outdated.

src/test.rs Show resolved Hide resolved
@rami3l rami3l marked this pull request as ready for review June 12, 2024 11:29
@rami3l rami3l requested review from rbtcollins and djc June 12, 2024 11:29
@rami3l rami3l changed the title refactor(test): execute all unit tests within a tokio context refactor(test): execute all #[rustup_macros::unit_test]s within a tokio context Jun 12, 2024
src/test.rs Outdated Show resolved Hide resolved
@rami3l rami3l enabled auto-merge June 14, 2024 02:14
@rami3l rami3l added this pull request to the merge queue Jun 14, 2024
Merged via the queue into master with commit 6d5f0f6 Jun 14, 2024
26 checks passed
@rami3l rami3l deleted the test/async-unit branch June 14, 2024 02:55
@rami3l rami3l added this to the 1.28.0 milestone Jun 14, 2024
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