Skip to content

Commit

Permalink
Remove internal usage of SystemTime::now(), and replace it with injec…
Browse files Browse the repository at this point in the history
…ting the time directly. (#12)
  • Loading branch information
slinkydeveloper authored Sep 20, 2024
1 parent f3e51fc commit 13f26f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,17 @@ pub trait VM: Sized {

fn sys_state_clear_all(&mut self) -> VMResult<()>;

fn sys_sleep(&mut self, duration: Duration) -> VMResult<AsyncResultHandle>;
fn sys_sleep(&mut self, wake_up_time_since_unix_epoch: Duration)
-> VMResult<AsyncResultHandle>;

fn sys_call(&mut self, target: Target, input: Bytes) -> VMResult<AsyncResultHandle>;

fn sys_send(&mut self, target: Target, input: Bytes, delay: Option<Duration>) -> VMResult<()>;
fn sys_send(
&mut self,
target: Target,
input: Bytes,
execution_time_since_unix_epoch: Option<Duration>,
) -> VMResult<()>;

fn sys_awakeable(&mut self) -> VMResult<(String, AsyncResultHandle)>;

Expand Down
22 changes: 9 additions & 13 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::borrow::Cow;
use std::collections::VecDeque;
use std::fmt;
use std::mem::size_of;
use std::time::{Duration, SystemTime};
use std::time::Duration;
use strum::IntoStaticStr;
use tracing::instrument;

Expand Down Expand Up @@ -316,7 +316,8 @@ impl super::VM for CoreVM {
self.do_transition(SysCompletableEntry(
"SysSleep",
SleepEntryMessage {
wake_up_time: duration_to_wakeup_time(duration),
wake_up_time: u64::try_from(duration.as_millis())
.expect("millis since Unix epoch should fit in u64"),
..Default::default()
},
))
Expand All @@ -343,7 +344,12 @@ impl super::VM for CoreVM {
handler_name: target.handler,
key: target.key.unwrap_or_default(),
parameter: input,
invoke_time: delay.map(duration_to_wakeup_time).unwrap_or_default(),
invoke_time: delay
.map(|d| {
u64::try_from(d.as_millis())
.expect("millis since Unix epoch should fit in u64")
})
.unwrap_or_default(),
..Default::default()
},
))
Expand Down Expand Up @@ -457,16 +463,6 @@ impl super::VM for CoreVM {
}
}

fn duration_to_wakeup_time(duration: Duration) -> u64 {
u64::try_from(
(SystemTime::now() + duration)
.duration_since(SystemTime::UNIX_EPOCH)
.expect("duration since Unix epoch should be well-defined")
.as_millis(),
)
.expect("millis since Unix epoch should fit in u64")
}

const INDIFFERENT_PAD: GeneralPurposeConfig = GeneralPurposeConfig::new()
.with_decode_padding_mode(DecodePaddingMode::Indifferent)
.with_encode_padding(false);
Expand Down

0 comments on commit 13f26f5

Please sign in to comment.