-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing the naive implementation of sleep and adding a tokio based
alternative
- Loading branch information
Showing
5 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#[cfg(not(feature = "tokio-sleep"))] | ||
mod thread; | ||
|
||
#[cfg(not(feature = "tokio-sleep"))] | ||
pub use self::thread::{sleep, Sleep}; | ||
|
||
#[cfg(feature = "tokio-sleep")] | ||
pub use tokio::time::{sleep, Sleep}; | ||
|
||
// Unit tests | ||
#[cfg(test)] | ||
mod tests { | ||
|
||
/// Basic test that launches 10k futures and waits for them to complete | ||
/// Has a high chance of failing if there is a race condition in sleep method | ||
/// Runs quickly otherwise | ||
#[cfg(not(feature = "tokio-sleep"))] | ||
#[tokio::test] | ||
async fn test_timeout() { | ||
use super::*; | ||
use std::time::Duration; | ||
use tokio::task::JoinSet; | ||
|
||
let mut join_set = JoinSet::default(); | ||
let total = 10000; | ||
for _i in 0..total { | ||
join_set.spawn(async move { | ||
sleep(Duration::from_millis(10)).await; | ||
}); | ||
} | ||
|
||
loop { | ||
let res = | ||
tokio::time::timeout(std::time::Duration::from_secs(10), join_set.join_next()) | ||
.await; | ||
assert!(res.is_ok()); | ||
if let Ok(None) = res { | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#[cfg(feature = "tokio-fs")] | ||
pub mod fs; |