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