Skip to content

Commit eb63946

Browse files
author
Xinye
committed
address comment
Signed-off-by: Xinye <xinye.tao@metabit-trading.com>
1 parent e6b4645 commit eb63946

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/lib.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ macro_rules! fail_point {
867867
#[cfg(feature = "async")]
868868
mod async_imp {
869869
use super::*;
870-
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a, Global>>;
870+
type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
871871

872872
#[derive(Clone)]
873873
pub(crate) struct AsyncCallback(
@@ -997,9 +997,16 @@ mod async_imp {
997997
match task {
998998
Task::Off => {}
999999
Task::Return(s) => return Some(s),
1000-
Task::Sleep(_) => panic!(
1001-
"fail does not support async sleep, please use a async closure to sleep."
1002-
),
1000+
Task::Sleep(t) => {
1001+
let not = Arc::new(tokio::sync::Notify::new());
1002+
let not_for_thread = not.clone();
1003+
let handle = std::thread::spawn(move || {
1004+
std::thread::sleep(Duration::from_millis(t));
1005+
not_for_thread.notify_waiters();
1006+
});
1007+
not.notified().await;
1008+
handle.join().unwrap();
1009+
}
10031010
Task::Panic(msg) => match msg {
10041011
Some(ref msg) => panic!("{}", msg),
10051012
None => panic!("failpoint {} panic", name),
@@ -1010,9 +1017,11 @@ mod async_imp {
10101017
},
10111018
Task::Pause => unreachable!(),
10121019
Task::Yield => thread::yield_now(),
1013-
Task::Delay(_) => panic!(
1014-
"fail does not support async delay, please use a async closure to delay."
1015-
),
1020+
Task::Delay(_) => {
1021+
let timer = Instant::now();
1022+
let timeout = Duration::from_millis(t);
1023+
while timer.elapsed() < timeout {}
1024+
}
10161025
Task::Callback(f) => {
10171026
f.run();
10181027
}
@@ -1279,5 +1288,21 @@ mod tests {
12791288
.await
12801289
.unwrap();
12811290
handle.await.unwrap();
1291+
1292+
cfg("sleep", "sleep(500)").unwrap();
1293+
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
1294+
let handle = tokio::spawn(async move {
1295+
tx.send(()).await.unwrap();
1296+
async_fail_point!("sleep");
1297+
tx.send(()).await.unwrap();
1298+
});
1299+
rx.recv().await.unwrap();
1300+
tokio::time::timeout(Duration::from_millis(300), rx.recv())
1301+
.await
1302+
.unwrap_err();
1303+
tokio::time::timeout(Duration::from_millis(300), rx.recv())
1304+
.await
1305+
.unwrap();
1306+
handle.await.unwrap();
12821307
}
12831308
}

0 commit comments

Comments
 (0)