mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-27 18:56:49 +00:00
Updated example to be consistent; added timing measurements to throttle
This commit is contained in:
parent
6990c1403f
commit
4ab7b213de
2 changed files with 19 additions and 6 deletions
|
@ -11,7 +11,7 @@ fn main() {
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
// emit value every 1 second
|
// emit value every 1 second
|
||||||
let s = stream::interval(Duration::from_nanos(1000000)).enumerate();
|
let s = stream::interval(Duration::from_secs(1)).enumerate();
|
||||||
|
|
||||||
// throttle for 2 seconds
|
// throttle for 2 seconds
|
||||||
let s = s.throttle(Duration::from_secs(2));
|
let s = s.throttle(Duration::from_secs(2));
|
||||||
|
|
|
@ -325,19 +325,32 @@ extension_trait! {
|
||||||
#
|
#
|
||||||
use async_std::prelude::*;
|
use async_std::prelude::*;
|
||||||
use async_std::stream;
|
use async_std::stream;
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
// emit value every 1 second
|
// emit value every 5 milliseconds
|
||||||
let s = stream::interval(Duration::from_millis(5)).enumerate().take(3);
|
let s = stream::interval(Duration::from_millis(5))
|
||||||
|
.enumerate()
|
||||||
|
.take(3);
|
||||||
|
|
||||||
// throttle for 2 seconds
|
// throttle for 10 milliseconds
|
||||||
let mut s = s.throttle(Duration::from_millis(10));
|
let mut s = s.throttle(Duration::from_millis(10));
|
||||||
|
|
||||||
|
let start = Instant::now();
|
||||||
assert_eq!(s.next().await, Some((0, ())));
|
assert_eq!(s.next().await, Some((0, ())));
|
||||||
|
let duration_ms = start.elapsed().as_millis();
|
||||||
|
assert!(duration_ms >= 5 && duration_ms < 15);
|
||||||
|
|
||||||
assert_eq!(s.next().await, Some((1, ())));
|
assert_eq!(s.next().await, Some((1, ())));
|
||||||
|
let duration_ms = start.elapsed().as_millis();
|
||||||
|
assert!(duration_ms >= 15 && duration_ms < 25);
|
||||||
|
|
||||||
assert_eq!(s.next().await, Some((2, ())));
|
assert_eq!(s.next().await, Some((2, ())));
|
||||||
|
let duration_ms = start.elapsed().as_millis();
|
||||||
|
assert!(duration_ms >= 25 && duration_ms < 35);
|
||||||
|
|
||||||
assert_eq!(s.next().await, None);
|
assert_eq!(s.next().await, None);
|
||||||
// with a pause of 2 seconds between each print
|
let duration_ms = start.elapsed().as_millis();
|
||||||
|
assert!(duration_ms >= 35 && duration_ms < 45);
|
||||||
#
|
#
|
||||||
# }) }
|
# }) }
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue