Skip to content

Commit

Permalink
add Framed test
Browse files Browse the repository at this point in the history
  • Loading branch information
sinui0 committed Mar 4, 2024
1 parent 95fe1e5 commit 22e4057
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions serio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bincode = { workspace = true, optional = true }
futures.workspace = true
serde = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["full"] }
tokio-util = { workspace = true, features = ["codec"] }

[[example]]
name = "tokio_codec"
Expand Down
42 changes: 42 additions & 0 deletions serio/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,45 @@ where
Poll::Ready(Some(item))
}
}

#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
use tokio::io::duplex;
use tokio_util::codec::LengthDelimitedCodec;

use crate::{SinkExt, StreamExt};

use super::*;

#[derive(Serialize, Deserialize)]
struct Ping;

#[derive(Serialize, Deserialize)]
struct Pong;

#[test]
fn test_framed() {
let (a, b) = duplex(1024);

let a = LengthDelimitedCodec::builder().new_framed(a);
let b = LengthDelimitedCodec::builder().new_framed(b);

let mut a = Framed::new(a, Bincode::default());
let mut b = Framed::new(b, Bincode::default());

let a = async {
a.send(Ping).await.unwrap();
a.next::<Pong>().await.unwrap().unwrap();
};

let b = async {
b.next::<Ping>().await.unwrap().unwrap();
b.send(Pong).await.unwrap();
};

futures::executor::block_on(async {
futures::join!(a, b);
});
}
}

0 comments on commit 22e4057

Please sign in to comment.