From 6b3727d5804bbe08f7e5230949ee809732cf1010 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Sat, 17 Dec 2022 19:06:30 +0100 Subject: [PATCH] metrics: make `num_idle_blocking_threads` test less flaky (#5302) --- tokio/tests/rt_metrics.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tokio/tests/rt_metrics.rs b/tokio/tests/rt_metrics.rs index 2a9f998f0..4b98d234c 100644 --- a/tokio/tests/rt_metrics.rs +++ b/tokio/tests/rt_metrics.rs @@ -31,6 +31,19 @@ fn num_idle_blocking_threads() { rt.block_on(async { time::sleep(Duration::from_millis(5)).await; }); + + // We need to wait until the blocking thread has become idle. Usually 5ms is + // enough for this to happen, but not always. When it isn't enough, sleep + // for another second. We don't always wait for a whole second since we want + // the test suite to finish quickly. + // + // Note that the timeout for idle threads to be killed is 10 seconds. + if 0 == rt.metrics().num_idle_blocking_threads() { + rt.block_on(async { + time::sleep(Duration::from_secs(1)).await; + }); + } + assert_eq!(1, rt.metrics().num_idle_blocking_threads()); }