Skip to content

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

Merged
rami3l merged 4 commits into
masterfrom
test/async-unit
Jun 14, 2024
Merged

refactor(test): execute all #[rustup_macros::unit_test]s within a tokio context#3868
rami3l merged 4 commits into
masterfrom
test/async-unit

Conversation

@rami3l

@rami3lrami3l commented Jun 12, 2024

Copy link
Copy Markdown
Member

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")]
staticTRACE_RUNTIME:Lazy<tokio::runtime::Runtime> =
Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
/// A tracer for the tests.
#[cfg(feature = "otel")]
staticTRACER: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 localcurrentprocess: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.

Comment threadsrc/test.rs
@rami3l
rami3l marked this pull request as ready for review June 12, 2024 11:29
@rami3l
rami3l requested review from djc and rbtcollinsJune 12, 2024 11:29
@rami3lrami3l changed the title refactor(test): execute all unit tests within a tokio contextrefactor(test): execute all #[rustup_macros::unit_test]s within a tokio contextJun 12, 2024
Comment threadsrc/test/mock/clitools.rs
@djcdjc mentioned this pull request Jun 13, 2024
Comment threadsrc/test.rs Outdated
djc
djc approved these changes Jun 13, 2024
@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 6d5f0f6Jun 14, 2024
@rami3l
rami3l deleted the test/async-unit branch June 14, 2024 02:55
@rami3lrami3l added this to the 1.28.0 milestone Jun 14, 2024
Sign up for freeto 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

@rami3l@djc