summaryrefslogtreecommitdiffstats
path: root/tokio/tests/time_throttle.rs
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-11-12 15:23:40 -0800
committerGitHub <noreply@github.com>2019-11-12 15:23:40 -0800
commit27e5b41067d01c0c9fac230c5addb58034201a63 (patch)
treef9bd8333dfe1853dfe1d8710b4dc966bd8555d54 /tokio/tests/time_throttle.rs
parente3df2eafd32e6f813d08617f0e2cd7abbc05c2b1 (diff)
reorganize modules (#1766)
This patch started as an effort to make `time::Timer` private. However, in an effort to get the build compiling again, more and more changes were made. This probably should have been broken up, but here we are. I will attempt to summarize the changes here. * Feature flags are reorganized to make clearer. `net-driver` becomes `io-driver`. `rt-current-thread` becomes `rt-core`. * The `Runtime` can be created without any executor. This replaces `enter`. It also allows creating I/O / time drivers that are standalone. * `tokio::timer` is renamed to `tokio::time`. This brings it in line with `std`. * `tokio::timer::Timer` is renamed to `Driver` and made private. * The `clock` module is removed. Instead, an `Instant` type is provided. This type defaults to calling `std::time::Instant`. A `test-util` feature flag can be used to enable hooking into time. * The `blocking` module is moved to the top level and is cleaned up. * The `task` module is moved to the top level. * The thread-pool's in-place blocking implementation is cleaned up. * `runtime::Spawner` is renamed to `runtime::Handle` and can be used to "enter" a runtime context.
Diffstat (limited to 'tokio/tests/time_throttle.rs')
-rw-r--r--tokio/tests/time_throttle.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/tokio/tests/time_throttle.rs b/tokio/tests/time_throttle.rs
new file mode 100644
index 00000000..0431a4f2
--- /dev/null
+++ b/tokio/tests/time_throttle.rs
@@ -0,0 +1,68 @@
+#![warn(rust_2018_idioms)]
+
+use tokio::sync::mpsc;
+use tokio::time::throttle::Throttle;
+use tokio::time::Instant;
+use tokio_test::{assert_pending, assert_ready_eq};
+
+use futures::future::poll_fn;
+use futures::StreamExt;
+use std::task::Poll;
+use std::time::Duration;
+
+#[tokio::test]
+async fn throttle() {
+ let (mut tx, rx) = mpsc::unbounded_channel();
+ let mut stream = Throttle::new(rx, ms(1));
+
+ poll_fn(|cx| {
+ assert_pending!(stream.poll_next_unpin(cx));
+ Poll::Ready(())
+ })
+ .await;
+
+ for i in 0..3 {
+ tx.try_send(i).unwrap();
+ }
+
+ drop(tx);
+
+ let mut now = Instant::now();
+
+ while let Some(_) = stream.next().await {
+ assert!(Instant::now() >= now);
+ now += ms(1);
+ }
+}
+
+#[tokio::test]
+async fn throttle_dur_0() {
+ let (mut tx, rx) = mpsc::unbounded_channel();
+ let mut stream = Throttle::new(rx, ms(0));
+
+ poll_fn(|cx| {
+ assert_pending!(stream.poll_next_unpin(cx));
+
+ for i in 0..3 {
+ tx.try_send(i).unwrap();
+ }
+
+ Poll::Ready(())
+ })
+ .await;
+
+ poll_fn(|cx| {
+ for i in 0..3 {
+ assert_ready_eq!(stream.poll_next_unpin(cx), Some(i), "i = {}", i);
+ }
+
+ assert_pending!(stream.poll_next_unpin(cx));
+
+ Poll::Ready(())
+ })
+ .await;
+}
+
+fn ms(n: u64) -> Duration {
+ Duration::from_millis(n)
+}