aboutsummaryrefslogtreecommitdiff
path: root/vendor/weezl/tests/async.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/weezl/tests/async.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/weezl/tests/async.rs')
-rw-r--r--vendor/weezl/tests/async.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/vendor/weezl/tests/async.rs b/vendor/weezl/tests/async.rs
new file mode 100644
index 0000000..5df95b0
--- /dev/null
+++ b/vendor/weezl/tests/async.rs
@@ -0,0 +1,48 @@
+use std::{env, fs};
+use tokio::io::BufReader;
+use tokio::net::{TcpListener, TcpStream};
+use tokio_util::compat::TokioAsyncReadCompatExt as _;
+use weezl::{decode, encode, BitOrder};
+
+async fn pair() -> (TcpStream, TcpStream) {
+ let listener = TcpListener::bind("localhost:0")
+ .await
+ .expect("No loop tcp for testing");
+ let addr = listener.local_addr().expect("No address for listener");
+
+ let connect = TcpStream::connect(addr);
+ let accept = listener.accept();
+
+ let (a, (b, _)) = tokio::try_join!(connect, accept).expect("Can connect");
+ (a, b)
+}
+
+async fn assert_send_through(data: &[u8], send: &mut TcpStream, recv: &mut TcpStream) {
+ let mut send = send.compat();
+ let mut recv = BufReader::new(recv).compat();
+
+ let mut encoder = encode::Encoder::new(BitOrder::Lsb, 8);
+ let encode = encoder.into_async(&mut send).encode_all(data);
+
+ let mut recv_buffer = vec![];
+ let mut decoder = decode::Decoder::new(BitOrder::Lsb, 8);
+ let decode = decoder.into_async(&mut recv_buffer).decode_all(&mut recv);
+
+ let (encode, decode) = tokio::join!(encode, decode);
+ encode.status.expect("Could send/encoded data");
+ decode.status.expect("Could recv/decode data");
+
+ assert_eq!(recv_buffer, data);
+}
+
+#[test]
+fn with_streams() {
+ let file = env::args().next().unwrap();
+ let data = fs::read(file).unwrap();
+
+ let rt = tokio::runtime::Runtime::new().expect("runtime");
+ let _enter = rt.enter();
+
+ let (mut send, mut recv) = rt.block_on(pair());
+ rt.block_on(assert_send_through(&data, &mut send, &mut recv));
+}