-
Notifications
You must be signed in to change notification settings - Fork 0
Add async-stream example and tests #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Async Stream Example | ||
|
|
||
| This example demonstrates generating a `Response::Stream` using `async-stream`. | ||
| It returns five frames to illustrate the pattern. | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| actor User | ||
| participant Main as main() | ||
| participant StreamResp as stream_response() | ||
| participant Stream as Stream<Frame> | ||
| User->>Main: Run example | ||
| Main->>StreamResp: Call stream_response() | ||
| StreamResp-->>Main: Return Response::Stream | ||
| Main->>Stream: Iterate stream.next() (5 times) | ||
| Stream-->>Main: Yield Frame(n) | ||
| Main->>User: Print received frame: Frame(n) | ||
| ``` | ||
|
|
||
| ```mermaid | ||
| classDiagram | ||
| class Frame { | ||
| +u32 0 | ||
| +Debug | ||
| +PartialEq | ||
| +bincode::Encode | ||
| +bincode::BorrowDecode | ||
| } | ||
| class Response { | ||
| <<generic>> | ||
| +Stream(Pin<Box<dyn Stream<Item=Result<T, E>>>>) | ||
| } | ||
| Frame <.. Response : used as generic | ||
| class stream_response { | ||
| +stream_response() Response<Frame> | ||
| } | ||
| stream_response ..> Response : returns | ||
| stream_response ..> Frame : yields | ||
| ``` |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //! Demonstrates generating `Response::Stream` values using `async-stream`. | ||
| //! | ||
| //! The `stream_response` function yields five sequential frames using | ||
| //! `async_stream::try_stream`. It returns a `Response` that can be | ||
| //! consumed by a `ConnectionActor`. | ||
|
|
||
| use async_stream::try_stream; | ||
| use futures::StreamExt; | ||
| use wireframe::response::Response; | ||
|
|
||
| #[derive(bincode::Encode, bincode::BorrowDecode, Debug, PartialEq)] | ||
| struct Frame(u32); | ||
|
|
||
| fn stream_response() -> Response<Frame> { | ||
| let frames = try_stream! { | ||
| for n in 0..5u32 { | ||
| yield Frame(n); | ||
| } | ||
| }; | ||
| Response::Stream(Box::pin(frames)) | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let Response::Stream(mut stream) = stream_response() else { | ||
| return; | ||
| }; | ||
| while let Some(Ok(frame)) = stream.next().await { | ||
| println!("received frame: {frame:?}"); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //! Tests for streams generated with the `async-stream` crate. | ||
| //! | ||
| //! These ensure that a `ConnectionActor` correctly drains frames from an | ||
| //! async-stream based `FrameStream`. | ||
|
|
||
| use async_stream::try_stream; | ||
| use rstest::rstest; | ||
| use tokio_util::sync::CancellationToken; | ||
| use wireframe::{ | ||
| connection::ConnectionActor, | ||
| push::PushQueues, | ||
| response::{FrameStream, WireframeError}, | ||
| }; | ||
|
|
||
| fn frame_stream() -> impl futures::Stream<Item = Result<u8, WireframeError>> { | ||
| try_stream! { | ||
| for n in 0u8..3 { | ||
| yield n; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[tokio::test] | ||
| async fn async_stream_frames_processed_in_order() { | ||
| let (queues, handle) = PushQueues::<u8>::bounded(8, 8); | ||
| let shutdown = CancellationToken::new(); | ||
| let stream: FrameStream<u8> = Box::pin(frame_stream()); | ||
|
|
||
| let mut actor = ConnectionActor::new(queues, handle, Some(stream), shutdown); | ||
| let mut out = Vec::new(); | ||
| actor.run(&mut out).await.unwrap(); | ||
| assert_eq!(out, vec![0, 1, 2]); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.