Skip to content

Commit 4ab7b21

Browse files
committed
Updated example to be consistent; added timing measurements to throttle
1 parent 6990c14 commit 4ab7b21

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

examples/throttle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
use std::time::Duration;
1212

1313
// emit value every 1 second
14-
let s = stream::interval(Duration::from_nanos(1000000)).enumerate();
14+
let s = stream::interval(Duration::from_secs(1)).enumerate();
1515

1616
// throttle for 2 seconds
1717
let s = s.throttle(Duration::from_secs(2));

src/stream/stream/mod.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,32 @@ extension_trait! {
325325
#
326326
use async_std::prelude::*;
327327
use async_std::stream;
328-
use std::time::Duration;
328+
use std::time::{Duration, Instant};
329329
330-
// emit value every 1 second
331-
let s = stream::interval(Duration::from_millis(5)).enumerate().take(3);
330+
// emit value every 5 milliseconds
331+
let s = stream::interval(Duration::from_millis(5))
332+
.enumerate()
333+
.take(3);
332334
333-
// throttle for 2 seconds
335+
// throttle for 10 milliseconds
334336
let mut s = s.throttle(Duration::from_millis(10));
335337
338+
let start = Instant::now();
336339
assert_eq!(s.next().await, Some((0, ())));
340+
let duration_ms = start.elapsed().as_millis();
341+
assert!(duration_ms >= 5 && duration_ms < 15);
342+
337343
assert_eq!(s.next().await, Some((1, ())));
344+
let duration_ms = start.elapsed().as_millis();
345+
assert!(duration_ms >= 15 && duration_ms < 25);
346+
338347
assert_eq!(s.next().await, Some((2, ())));
348+
let duration_ms = start.elapsed().as_millis();
349+
assert!(duration_ms >= 25 && duration_ms < 35);
350+
339351
assert_eq!(s.next().await, None);
340-
// with a pause of 2 seconds between each print
352+
let duration_ms = start.elapsed().as_millis();
353+
assert!(duration_ms >= 35 && duration_ms < 45);
341354
#
342355
# }) }
343356
```

0 commit comments

Comments
 (0)