Skip to content

Commit 62d1d30

Browse files
committed
DRY: ProcessCreateTime::now() was an almost-copy of SystemTime and only used in one test, we can move the unit test so it doesn't require special access functions
1 parent 22fb7b4 commit 62d1d30

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

src/system/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -978,14 +978,6 @@ mod tests {
978978
assert!(super::Process::tty_device_id(WithProcess::Current).is_ok());
979979
}
980980

981-
#[test]
982-
fn get_process_start_time() {
983-
let time = super::Process::starting_time(WithProcess::Current).unwrap();
984-
let now = super::ProcessCreateTime::now().unwrap();
985-
assert!(time.secs() > now.secs() - 24 * 60 * 60);
986-
assert!(time < now);
987-
}
988-
989981
#[test]
990982
fn pgid_test() {
991983
use super::{getpgid, setpgid};

src/system/time.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,6 @@ impl ProcessCreateTime {
118118
}
119119
}
120120

121-
#[cfg(test)]
122-
pub(super) fn now() -> std::io::Result<ProcessCreateTime> {
123-
let mut spec = MaybeUninit::<libc::timespec>::uninit();
124-
// SAFETY: valid pointer is passed to clock_gettime
125-
crate::cutils::cerr(unsafe {
126-
libc::clock_gettime(
127-
if cfg!(target_os = "freebsd") {
128-
libc::CLOCK_REALTIME
129-
} else {
130-
libc::CLOCK_BOOTTIME
131-
},
132-
spec.as_mut_ptr(),
133-
)
134-
})?;
135-
// SAFETY: The `libc::clock_gettime` will correctly initialize `spec`,
136-
// otherwise it will return early with the `?` operator.
137-
let spec = unsafe { spec.assume_init() };
138-
139-
// the below conversion is not as useless as clippy thinks, on 32bit systems
140-
#[allow(clippy::useless_conversion)]
141-
Ok(ProcessCreateTime::new(
142-
spec.tv_sec.into(),
143-
spec.tv_nsec.into(),
144-
))
145-
}
146-
147-
#[cfg(test)]
148-
pub(super) fn secs(&self) -> i64 {
149-
self.secs
150-
}
151-
152121
pub(super) fn encode(&self, target: &mut impl Write) -> std::io::Result<()> {
153122
let secs = self.secs.to_ne_bytes();
154123
let nsecs = self.nsecs.to_ne_bytes();
@@ -203,4 +172,18 @@ mod tests {
203172
SystemTime::new(6, 500_000_000)
204173
);
205174
}
175+
176+
#[test]
177+
fn get_process_start_time() {
178+
use crate::system::{Process, WithProcess};
179+
let time = Process::starting_time(WithProcess::Current).unwrap();
180+
181+
let now = {
182+
let super::SystemTime { secs, nsecs } = super::SystemTime::now().unwrap();
183+
super::ProcessCreateTime { secs, nsecs }
184+
};
185+
186+
assert!(time.secs > now.secs - 24 * 60 * 60);
187+
assert!(time < now);
188+
}
206189
}

0 commit comments

Comments
 (0)