Skip to content

Commit

Permalink
Merge branch 'tokio-rs:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tglane authored Jun 4, 2024
2 parents af34861 + 8fca6f6 commit 9bd345a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tokio/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ impl Command {
&self.std
}

/// Cheaply convert to a `&mut std::process::Command` for places where the type from the
/// standard library is expected.
pub fn as_std_mut(&mut self) -> &mut StdCommand {
&mut self.std
}

/// Adds an argument to pass to the program.
///
/// Only one argument can be passed per use. So instead of:
Expand Down
14 changes: 11 additions & 3 deletions tokio/src/runtime/time/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ impl TimeSource {
pub(crate) fn instant_to_tick(&self, t: Instant) -> u64 {
// round up
let dur: Duration = t.saturating_duration_since(self.start_time);
let ms = dur.as_millis();

ms.try_into().unwrap_or(MAX_SAFE_MILLIS_DURATION)
let ms = dur
.as_millis()
.try_into()
.unwrap_or(MAX_SAFE_MILLIS_DURATION);
ms.min(MAX_SAFE_MILLIS_DURATION)
}

pub(crate) fn tick_to_duration(&self, t: u64) -> Duration {
Expand All @@ -34,4 +36,10 @@ impl TimeSource {
pub(crate) fn now(&self, clock: &Clock) -> u64 {
self.instant_to_tick(clock.now())
}

#[cfg(test)]
#[allow(dead_code)]
pub(super) fn start_time(&self) -> Instant {
self.start_time
}
}
14 changes: 14 additions & 0 deletions tokio/src/runtime/time/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,17 @@ fn poll_process_levels_targeted() {
handle.process_at_time(0, 192);
handle.process_at_time(0, 192);
}

#[test]
#[cfg(not(loom))]
fn instant_to_tick_max() {
use crate::runtime::time::entry::MAX_SAFE_MILLIS_DURATION;

let rt = rt(true);
let handle = rt.handle().inner.driver().time();

let start_time = handle.time_source.start_time();
let long_future = start_time + std::time::Duration::from_millis(MAX_SAFE_MILLIS_DURATION + 1);

assert!(handle.time_source.instant_to_tick(long_future) <= MAX_SAFE_MILLIS_DURATION);
}

0 comments on commit 9bd345a

Please sign in to comment.