E2E test: wait until i am the last task properly

This commit is contained in:
Igor Katson 2024-08-21 17:39:21 +01:00
parent d5ddf4d294
commit 6bfb8f9e15
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 45 additions and 7 deletions

View file

@ -2,9 +2,10 @@ use std::{
io::Write,
path::Path,
sync::{Arc, Weak},
time::Duration,
};
use anyhow::Context;
use anyhow::{bail, Context};
use axum::{response::IntoResponse, routing::get, Router};
use librqbit_core::Id20;
use parking_lot::RwLock;
@ -165,3 +166,41 @@ impl DropChecks {
Ok(())
}
}
pub async fn wait_until(
mut cond: impl FnMut() -> anyhow::Result<()>,
timeout: Duration,
) -> anyhow::Result<()> {
let mut interval = tokio::time::interval(Duration::from_millis(10));
let mut last_err: Option<anyhow::Error> = None;
let res = tokio::time::timeout(timeout, async {
loop {
interval.tick().await;
match cond() {
Ok(()) => return Ok::<_, anyhow::Error>(()),
Err(e) => last_err = Some(e),
}
}
})
.await;
if res.is_err() {
bail!("wait_until timeout: last result = {last_err:?}")
}
Ok(())
}
pub async fn wait_until_i_am_the_last_task() {
let metrics = tokio::runtime::Handle::current().metrics();
wait_until(
|| {
let num_alive = metrics.num_alive_tasks();
if num_alive != 1 {
bail!("metrics.num_alive_tasks() = {num_alive}, expected 1")
}
Ok(())
},
Duration::from_secs(5),
)
.await
.unwrap();
}