time: prevent panicking in sleep() with large durations (#4495)

This commit is contained in:
Jonathan Johnson 2022-02-15 01:49:41 -08:00 committed by GitHub
parent 37917b821d
commit 0826f763e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 17 deletions

View File

@ -129,7 +129,7 @@ impl ClockTime {
.unwrap_or_else(|| Duration::from_secs(0));
let ms = dur.as_millis();
ms.try_into().expect("Duration too far into the future")
ms.try_into().unwrap_or(u64::MAX)
}
pub(self) fn tick_to_duration(&self, t: u64) -> Duration {

View File

@ -235,22 +235,6 @@ async fn long_sleeps() {
assert!(tokio::time::Instant::now() <= deadline + Duration::from_millis(1));
}
#[tokio::test]
#[should_panic(expected = "Duration too far into the future")]
async fn very_long_sleeps() {
tokio::time::pause();
// Some platforms (eg macos) can't represent times this far in the future
if let Some(deadline) = tokio::time::Instant::now().checked_add(Duration::from_secs(1u64 << 62))
{
tokio::time::sleep_until(deadline).await;
} else {
// make it pass anyway (we can't skip/ignore the test based on the
// result of checked_add)
panic!("Duration too far into the future (test ignored)")
}
}
#[tokio::test]
async fn reset_after_firing() {
let timer = tokio::time::sleep(std::time::Duration::from_millis(1));