-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A larger buffer of values for each partition can be added on top of a single buffered value by wrapping each stream with code that prefetches the values. Thus this approach has a better composability than using an internal buffer. The tradeoff is that this may deadlock and calling code that needs to concurrently process items in both streams must be programmed in a way that ensures that waiting to consume from one stream never blocks the other stream.
- Loading branch information
Showing
4 changed files
with
154 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,46 @@ | ||
use tokio_stream::{self as stream, StreamExt}; | ||
use tokio_test::{assert_pending, assert_ready_eq, task}; | ||
|
||
mod support { | ||
pub(crate) mod mpsc; | ||
} | ||
|
||
#[tokio::test] | ||
async fn partition() { | ||
let stream = stream::iter(0..4); | ||
let (mut even, mut odd) = stream.partition(|v| v % 2 == 0); | ||
assert_eq!(Some(0), even.next().await); | ||
assert_eq!(Some(1), odd.next().await); | ||
assert_eq!(Some(2), even.next().await); | ||
assert_eq!(Some(3), odd.next().await); | ||
assert_eq!(None, even.next().await); | ||
assert_eq!(None, odd.next().await); | ||
} | ||
let stream = stream::iter(0..6); | ||
let (matches, non_matches) = stream.partition(|v| v % 2 == 0); | ||
let mut matches = task::spawn(matches); | ||
let mut non_matches = task::spawn(non_matches); | ||
|
||
#[tokio::test] | ||
async fn partition_buffers() { | ||
let stream = stream::iter(0..4); | ||
let (mut even, mut odd) = stream.partition(|v| v % 2 == 0); | ||
assert_eq!(Some(1), odd.next().await); | ||
assert_eq!(Some(3), odd.next().await); | ||
assert_eq!(None, odd.next().await); | ||
assert_eq!(Some(0), even.next().await); | ||
assert_eq!(Some(2), even.next().await); | ||
assert_eq!(None, odd.next().await); | ||
// polling matches when the next item matches returns the item from the stream. | ||
assert_ready_eq!(matches.poll_next(), Some(0)); | ||
|
||
// polling non_matches when the next item doesn't match returns the item from the stream. | ||
assert_ready_eq!(non_matches.poll_next(), Some(1)); | ||
|
||
// polling non_matches when the next item matches buffers the item. | ||
assert_pending!(non_matches.poll_next()); | ||
|
||
// polling matches when there is a bufferred match returns the buffered item. | ||
assert_ready_eq!(matches.poll_next(), Some(2)); | ||
|
||
// polling matches when the next item doesn't match buffers the item. | ||
assert_pending!(matches.poll_next()); | ||
|
||
// polling non_matches when there is a bufferred non-match returns the buffered item. | ||
assert_ready_eq!(non_matches.poll_next(), Some(3)); | ||
|
||
// polling non_matches twice when the next item matches buffers the item only once. | ||
assert_pending!(non_matches.poll_next()); | ||
assert_pending!(non_matches.poll_next()); | ||
assert_ready_eq!(matches.poll_next(), Some(4)); | ||
|
||
// polling matches twice when the next item doesn't match buffers the item only once. | ||
assert_pending!(matches.poll_next()); | ||
assert_pending!(matches.poll_next()); | ||
assert_ready_eq!(non_matches.poll_next(), Some(5)); | ||
|
||
// polling matches and non_matches when the stream is exhausted returns None. | ||
assert_ready_eq!(matches.poll_next(), None); | ||
assert_ready_eq!(non_matches.poll_next(), None); | ||
} |