Files
tokio/tokio-timer/tests/throttle.rs
T

69 lines
1.4 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2019-06-30 08:48:53 -07:00
#![cfg(feature = "async-traits")]
2018-11-20 00:04:55 +01:00
2019-06-30 08:48:53 -07:00
use tokio_sync::mpsc;
use tokio_test::task::MockTask;
use tokio_test::{assert_pending, assert_ready_eq, clock};
use tokio_timer::throttle::Throttle;
2018-11-20 00:04:55 +01:00
2019-06-30 08:48:53 -07:00
use futures_core::Stream;
use std::time::Duration;
macro_rules! poll {
($task:ident, $stream:ident) => {{
use std::pin::Pin;
$task.enter(|cx| Pin::new(&mut $stream).poll_next(cx))
}};
}
2018-11-20 00:04:55 +01:00
#[test]
fn throttle() {
2019-06-30 08:48:53 -07:00
let mut t = MockTask::new();
clock::mock(|clock| {
let (mut tx, rx) = mpsc::unbounded_channel();
let mut stream = Throttle::new(rx, ms(1));
2018-11-20 00:04:55 +01:00
2019-06-30 08:48:53 -07:00
assert_pending!(poll!(t, stream));
2018-11-20 00:04:55 +01:00
for i in 0..3 {
2019-06-30 08:48:53 -07:00
tx.try_send(i).unwrap();
2018-11-20 00:04:55 +01:00
}
2019-06-30 08:48:53 -07:00
2018-11-20 00:04:55 +01:00
for i in 0..3 {
2019-06-30 08:48:53 -07:00
assert_ready_eq!(poll!(t, stream), Some(i));
assert_pending!(poll!(t, stream));
2018-11-20 00:04:55 +01:00
2019-06-30 08:48:53 -07:00
clock.advance(ms(1));
2018-11-20 00:04:55 +01:00
}
2019-06-30 08:48:53 -07:00
assert_pending!(poll!(t, stream));
2018-11-20 00:04:55 +01:00
});
}
#[test]
fn throttle_dur_0() {
2019-06-30 08:48:53 -07:00
let mut t = MockTask::new();
clock::mock(|_| {
let (mut tx, rx) = mpsc::unbounded_channel();
let mut stream = Throttle::new(rx, ms(0));
2018-11-20 00:04:55 +01:00
2019-06-30 08:48:53 -07:00
assert_pending!(poll!(t, stream));
2018-11-20 00:04:55 +01:00
for i in 0..3 {
2019-06-30 08:48:53 -07:00
tx.try_send(i).unwrap();
2018-11-20 00:04:55 +01:00
}
2019-06-30 08:48:53 -07:00
2018-11-20 00:04:55 +01:00
for i in 0..3 {
2019-06-30 08:48:53 -07:00
assert_ready_eq!(poll!(t, stream), Some(i));
2018-11-20 00:04:55 +01:00
}
2019-06-30 08:48:53 -07:00
assert_pending!(poll!(t, stream));
2018-11-20 00:04:55 +01:00
});
}
2019-06-30 08:48:53 -07:00
fn ms(n: u64) -> Duration {
Duration::from_millis(n)
}