Fix. Use rounded (seconds) timestamp for test (#7394)

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2024-03-15 14:24:22 +08:00 committed by GitHub
parent cdea968c24
commit 8e5fbc8480
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1586,7 +1586,6 @@ pub fn read_custom_client(config: &str) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use chrono::{format::StrftimeItems, Local};
use hbb_common::tokio::{ use hbb_common::tokio::{
self, self,
time::{interval, interval_at, sleep, Duration, Instant, Interval}, time::{interval, interval_at, sleep, Duration, Instant, Interval},
@ -1594,9 +1593,13 @@ mod tests {
use std::collections::HashSet; use std::collections::HashSet;
#[inline] #[inline]
fn now_time_string() -> String { fn get_timestamp_secs() -> u128 {
let format = StrftimeItems::new("%Y-%m-%d %H:%M:%S"); (std::time::SystemTime::UNIX_EPOCH
Local::now().format_with_items(format).to_string() .elapsed()
.unwrap()
.as_millis()
+ 500)
/ 1000
} }
fn interval_maker() -> Interval { fn interval_maker() -> Interval {
@ -1626,13 +1629,13 @@ mod tests {
if tokio_times.len() >= 10 && times.len() >= 10 { if tokio_times.len() >= 10 && times.len() >= 10 {
break; break;
} }
times.push(now_time_string()); times.push(get_timestamp_secs());
} }
_ = tokio_timer.tick() => { _ = tokio_timer.tick() => {
if tokio_times.len() >= 10 && times.len() >= 10 { if tokio_times.len() >= 10 && times.len() >= 10 {
break; break;
} }
tokio_times.push(now_time_string()); tokio_times.push(get_timestamp_secs());
} }
} }
} }
@ -1648,14 +1651,14 @@ mod tests {
loop { loop {
tokio::select! { tokio::select! {
_ = timer.tick() => { _ = timer.tick() => {
times.push(now_time_string()); times.push(get_timestamp_secs());
if times.len() == 5 { if times.len() == 5 {
break; break;
} }
} }
} }
} }
let times2: HashSet<String> = HashSet::from_iter(times.clone()); let times2: HashSet<u128> = HashSet::from_iter(times.clone());
assert_eq!(times.len(), times2.len() + 3); assert_eq!(times.len(), times2.len() + 3);
} }
@ -1664,14 +1667,14 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_RustDesk_interval_sleep() { async fn test_RustDesk_interval_sleep() {
let base_intervals = [interval_maker, interval_at_maker]; let base_intervals = [interval_maker, interval_at_maker];
for maker in base_intervals.into_iter() { for (i, maker) in base_intervals.into_iter().enumerate() {
let mut timer = rustdesk_interval(maker()); let mut timer = rustdesk_interval(maker());
let mut times = Vec::new(); let mut times = Vec::new();
sleep(Duration::from_secs(3)).await; sleep(Duration::from_secs(3)).await;
loop { loop {
tokio::select! { tokio::select! {
_ = timer.tick() => { _ = timer.tick() => {
times.push(now_time_string()); times.push(get_timestamp_secs());
if times.len() == 5 { if times.len() == 5 {
break; break;
} }
@ -1681,8 +1684,8 @@ mod tests {
// No mutliple ticks in the `interval` time. // No mutliple ticks in the `interval` time.
// Values in "times" are unique and are less than normal tokio interval. // Values in "times" are unique and are less than normal tokio interval.
// See previous test (test_tokio_time_interval_sleep) for comparison. // See previous test (test_tokio_time_interval_sleep) for comparison.
let times2: HashSet<String> = HashSet::from_iter(times.clone()); let times2: HashSet<u128> = HashSet::from_iter(times.clone());
assert_eq!(times.len(), times2.len()); assert_eq!(times.len(), times2.len(), "test: {}", i);
} }
} }