diff --git a/zenoh/tests/interceptors.rs b/zenoh/tests/interceptors.rs index da8e971218..3a9c1a1afc 100644 --- a/zenoh/tests/interceptors.rs +++ b/zenoh/tests/interceptors.rs @@ -29,21 +29,29 @@ fn downsampling() { // declare subscriber let zenoh_sub = zenoh::open(config).res().unwrap(); - let count_r100 = Arc::new(Mutex::new(0_i64)); - let count_r100_clone = count_r100.clone(); - - let count_r50 = Arc::new(Mutex::new(0_i64)); - let count_r50_clone = count_r50.clone(); + let last_time_r100 = Arc::new(Mutex::new( + std::time::Instant::now() - std::time::Duration::from_millis(100), + )); + let last_time_r50 = Arc::new(Mutex::new( + std::time::Instant::now() - std::time::Duration::from_millis(50), + )); let _sub = zenoh_sub .declare_subscriber("test/downsamples/*") .callback(move |sample| { + let curr_time = std::time::Instant::now(); if sample.key_expr.as_str() == "test/downsamples/r100" { - let mut count = count_r100_clone.lock().unwrap(); - *count += 1; + let mut last_time = last_time_r100.lock().unwrap(); + let interval = (curr_time - *last_time).as_millis(); + *last_time = curr_time; + println!("interval 100: {}", interval); + assert!(interval >= 100); } else if sample.key_expr.as_str() == "test/downsamples/r50" { - let mut count = count_r50_clone.lock().unwrap(); - *count += 1; + let mut last_time = last_time_r50.lock().unwrap(); + let interval = (curr_time - *last_time).as_millis(); + *last_time = curr_time; + println!("interval 50: {}", interval); + assert!(interval >= 50); } }) .res() @@ -61,7 +69,6 @@ fn downsampling() { .unwrap(); let interval = std::time::Duration::from_millis(1); - let start_time = std::time::Instant::now(); for i in 0..1000 { println!("message {}", i); publisher_r100.put(format!("message {}", i)).res().unwrap(); @@ -69,23 +76,4 @@ fn downsampling() { std::thread::sleep(interval); } - - // check result count of queries (with an error of 20%) - let full_elapsed_time = std::time::Instant::now() - start_time; - - let expected_count_r100 = (full_elapsed_time.as_millis() / 100) as i64; - println!( - "check {} vs {}", - *count_r100.lock().unwrap(), - expected_count_r100 - ); - assert!((*count_r100.lock().unwrap() - expected_count_r100).abs() <= expected_count_r100 / 5); - - let expected_count_r50 = (full_elapsed_time.as_millis() / 50) as i64; - println!( - "check {} vs {}", - *count_r50.lock().unwrap(), - expected_count_r50 - ); - assert!((*count_r50.lock().unwrap() - expected_count_r50).abs() <= expected_count_r50 / 5); }