test RustDeskInterval with interval_at (#7249)
* test RustDeskInterval with interval_at Signed-off-by: fufesou <shuanglongchen@yeah.net> * Test. RustDeskInterval, to tokio interval Signed-off-by: fufesou <shuanglongchen@yeah.net> * Add comment Signed-off-by: fufesou <shuanglongchen@yeah.net> --------- Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
parent
aa6f879504
commit
3ae52dacfc
@ -1462,20 +1462,36 @@ mod tests {
|
|||||||
use chrono::{format::StrftimeItems, Local};
|
use chrono::{format::StrftimeItems, Local};
|
||||||
use hbb_common::tokio::{
|
use hbb_common::tokio::{
|
||||||
self,
|
self,
|
||||||
time::{interval, sleep, Duration},
|
time::{interval, interval_at, sleep, Duration, Instant, Interval},
|
||||||
};
|
};
|
||||||
use std::collections::HashSet;
|
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 interval_maker() -> Interval {
|
||||||
|
interval(Duration::from_secs(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn interval_at_maker() -> Interval {
|
||||||
|
interval_at(
|
||||||
|
Instant::now() + Duration::from_secs(1),
|
||||||
|
Duration::from_secs(1),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_tokio_time_interval() {
|
async fn test_tokio_time_interval() {
|
||||||
let mut timer = interval(Duration::from_secs(1));
|
let mut timer = 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() => {
|
||||||
let format = StrftimeItems::new("%Y-%m-%d %H:%M:%S");
|
times.push(now_time_string());
|
||||||
times.push(Local::now().format_with_items(format).to_string());
|
|
||||||
if times.len() == 5 {
|
if times.len() == 5 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1486,25 +1502,60 @@ mod tests {
|
|||||||
assert_eq!(times.len(), times2.len() + 3);
|
assert_eq!(times.len(), times2.len() + 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ThrottledInterval tick at the same time as tokio interval, if no sleeps
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_RustDesk_interval() {
|
async fn test_RustDesk_interval() {
|
||||||
let mut timer = rustdesk_interval(interval(Duration::from_secs(1)));
|
let base_intervals = [interval_maker, interval_at_maker];
|
||||||
let mut times = Vec::new();
|
for maker in base_intervals.into_iter() {
|
||||||
sleep(Duration::from_secs(3)).await;
|
let mut tokio_timer = maker();
|
||||||
loop {
|
let mut tokio_times = Vec::new();
|
||||||
tokio::select! {
|
let mut timer = rustdesk_interval(maker());
|
||||||
_ = timer.tick() => {
|
let mut times = Vec::new();
|
||||||
let format = StrftimeItems::new("%Y-%m-%d %H:%M:%S");
|
loop {
|
||||||
times.push(Local::now().format_with_items(format).to_string());
|
tokio::select! {
|
||||||
if times.len() == 5 {
|
_ = timer.tick() => {
|
||||||
break;
|
if tokio_times.len() >= 10 && times.len() >= 10 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
times.push(now_time_string());
|
||||||
|
}
|
||||||
|
_ = tokio_timer.tick() => {
|
||||||
|
if tokio_times.len() >= 10 && times.len() >= 10 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokio_times.push(now_time_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assert_eq!(times, tokio_times);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ThrottledInterval tick less times than tokio interval, if there're sleeps
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_RustDesk_interval_sleep() {
|
||||||
|
let base_intervals = [interval_maker, interval_at_maker];
|
||||||
|
for maker in base_intervals.into_iter() {
|
||||||
|
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());
|
||||||
|
if times.len() == 5 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// No mutliple ticks in the `interval` time.
|
||||||
|
// Values in "times" are unique and are less than normal tokio interval.
|
||||||
|
let times2: HashSet<String> = HashSet::from_iter(times.clone());
|
||||||
|
assert_eq!(times.len(), times2.len());
|
||||||
}
|
}
|
||||||
let times2: HashSet<String> = HashSet::from_iter(times.clone());
|
|
||||||
assert_eq!(times.len(), times2.len());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user