-
Notifications
You must be signed in to change notification settings - Fork 0
Add wireframe_testing crate for test helpers #112
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
Closed
Closed
Changes from all commits
Commits
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 was deleted.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "wireframe_testing" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| tokio = { version = "1", features = ["macros", "rt"] } | ||
| wireframe = { path = ".." } | ||
| bincode = "2" | ||
| bytes = "1" | ||
|
|
||
| [dev-dependencies] | ||
| rstest = "0.18" | ||
|
|
||
| [lib] | ||
| path = "src/lib.rs" |
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,139 @@ | ||
| use bincode::Encode; | ||
| use bytes::BytesMut; | ||
| use tokio::io::{self, AsyncReadExt, AsyncWriteExt, DuplexStream, duplex}; | ||
| use wireframe::{ | ||
| app::{Packet, WireframeApp}, | ||
| frame::FrameProcessor, | ||
| serializer::Serializer, | ||
| }; | ||
|
|
||
| const DEFAULT_CAPACITY: usize = 4096; | ||
|
|
||
| /// Feed a single frame into `app` using an in-memory duplex stream. | ||
| pub async fn drive_with_frame<S, C, E>( | ||
| app: WireframeApp<S, C, E>, | ||
| frame: Vec<u8>, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync + 'static, | ||
| C: Send + 'static, | ||
| E: Packet, | ||
| { | ||
| drive_with_frame_with_capacity(app, frame, DEFAULT_CAPACITY).await | ||
| } | ||
|
|
||
| /// Drive `app` with multiple frames, returning all bytes written by the app. | ||
| pub async fn drive_with_frames<S, C, E>( | ||
| app: WireframeApp<S, C, E>, | ||
| frames: Vec<Vec<u8>>, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync + 'static, | ||
| C: Send + 'static, | ||
| E: Packet, | ||
| { | ||
| drive_with_frames_with_capacity(app, frames, DEFAULT_CAPACITY).await | ||
| } | ||
|
|
||
| /// Feed `app` a single frame with a custom duplex buffer capacity. | ||
| pub async fn drive_with_frame_with_capacity<S, C, E>( | ||
| app: WireframeApp<S, C, E>, | ||
| frame: Vec<u8>, | ||
| capacity: usize, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync + 'static, | ||
| C: Send + 'static, | ||
| E: Packet, | ||
| { | ||
| drive_with_frames_with_capacity(app, vec![frame], capacity).await | ||
| } | ||
|
|
||
| /// Drive `app` with multiple frames using a duplex buffer of `capacity` bytes. | ||
| pub async fn drive_with_frames_with_capacity<S, C, E>( | ||
| app: WireframeApp<S, C, E>, | ||
| frames: Vec<Vec<u8>>, | ||
| capacity: usize, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync + 'static, | ||
| C: Send + 'static, | ||
| E: Packet, | ||
| { | ||
| let (mut client, server) = duplex(capacity); | ||
| let server_task = tokio::spawn(async move { | ||
| app.handle_connection(server).await; | ||
| }); | ||
|
|
||
| send_frames(&mut client, &frames).await?; | ||
| client.shutdown().await?; | ||
|
|
||
| let mut buf = Vec::new(); | ||
| client.read_to_end(&mut buf).await?; | ||
|
|
||
| server_task.await.expect("app task panicked"); | ||
| Ok(buf) | ||
| } | ||
|
|
||
| /// Borrow `app` mutably and feed it a single frame. | ||
| pub async fn drive_with_frame_mut<S, C, E>( | ||
| app: &mut WireframeApp<S, C, E>, | ||
| frame: Vec<u8>, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync, | ||
| C: Send, | ||
| E: Packet, | ||
| { | ||
| drive_with_frames_mut(app, vec![frame]).await | ||
| } | ||
|
|
||
| /// Borrow `app` mutably and feed it multiple frames. | ||
| pub async fn drive_with_frames_mut<S, C, E>( | ||
| app: &mut WireframeApp<S, C, E>, | ||
| frames: Vec<Vec<u8>>, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync, | ||
| C: Send, | ||
| E: Packet, | ||
| { | ||
| let (mut client, server) = duplex(DEFAULT_CAPACITY); | ||
|
|
||
| send_frames(&mut client, &frames).await?; | ||
| client.shutdown().await?; | ||
|
|
||
| app.handle_connection(server).await; | ||
|
|
||
| let mut buf = Vec::new(); | ||
| client.read_to_end(&mut buf).await?; | ||
|
|
||
| Ok(buf) | ||
| } | ||
|
|
||
| /// Encode `msg` using `bincode` and drive the app with the resulting frame. | ||
| pub async fn drive_with_bincode<S, C, E, M>( | ||
| app: WireframeApp<S, C, E>, | ||
| msg: M, | ||
| ) -> io::Result<Vec<u8>> | ||
| where | ||
| S: Serializer + Send + Sync + 'static, | ||
| C: Send + 'static, | ||
| E: Packet, | ||
| M: Encode, | ||
| { | ||
| let bytes = bincode::encode_to_vec(msg, bincode::config::standard()) | ||
| .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; | ||
| let mut framed = BytesMut::with_capacity(4 + bytes.len()); | ||
| wireframe::frame::LengthPrefixedProcessor::default() | ||
| .encode(&bytes, &mut framed) | ||
| .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; | ||
| drive_with_frame(app, framed.to_vec()).await | ||
| } | ||
|
|
||
| async fn send_frames(stream: &mut DuplexStream, frames: &[Vec<u8>]) -> io::Result<()> { | ||
| for frame in frames { | ||
| stream.write_all(frame).await?; | ||
| } | ||
| Ok(()) | ||
| } |
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,11 @@ | ||
| pub mod helpers; | ||
|
|
||
| pub use helpers::{ | ||
| drive_with_bincode, | ||
| drive_with_frame, | ||
| drive_with_frame_mut, | ||
| drive_with_frame_with_capacity, | ||
| drive_with_frames, | ||
| drive_with_frames_mut, | ||
| drive_with_frames_with_capacity, | ||
| }; | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (review_instructions): Module 'wireframe_testing' is missing a containing item comment (
//!).Please add a module-level doc comment at the top of this file using
//!to describe the purpose of the module.Review instructions:
Path patterns:
**/*.rsInstructions:
All modules MUST have a containing item comment (
//!)