From b6c91d138b2d4078c2ba08b436256b4d035d59ab Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 19 May 2022 17:30:37 +0200 Subject: [PATCH] Read exactly 11 bytes Reading a stream to end is a tricky thing to do and may hang forever in case the stream doesn't get closed by the other party. We know exactly the message we want to receive so we can read exactly the number of bytes. With an upcoming refactoring where we introduce an opaque stream type, the current code - for some reason - hangs forever :( --- muxers/mplex/tests/async_write.rs | 4 ++-- muxers/mplex/tests/two_peers.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/muxers/mplex/tests/async_write.rs b/muxers/mplex/tests/async_write.rs index 96b608a68add..2069bf0b8f69 100644 --- a/muxers/mplex/tests/async_write.rs +++ b/muxers/mplex/tests/async_write.rs @@ -64,8 +64,8 @@ fn async_write() { .await .unwrap(); - let mut buf = Vec::new(); - outbound.read_to_end(&mut buf).await.unwrap(); + let mut buf = vec![0u8; 11]; + outbound.read_exact(&mut buf).await.unwrap(); assert_eq!(buf, b"hello world"); }); diff --git a/muxers/mplex/tests/two_peers.rs b/muxers/mplex/tests/two_peers.rs index 77e1a09997bd..004071320401 100644 --- a/muxers/mplex/tests/two_peers.rs +++ b/muxers/mplex/tests/two_peers.rs @@ -140,8 +140,8 @@ fn client_to_server_inbound() { } }; - let mut buf = Vec::new(); - inbound.read_to_end(&mut buf).await.unwrap(); + let mut buf = vec![0u8; 11]; + inbound.read_exact(&mut buf).await.unwrap(); assert_eq!(buf, b"hello world"); }); @@ -200,8 +200,8 @@ fn protocol_not_match() { .await .unwrap(); - let mut buf = Vec::new(); - outbound.read_to_end(&mut buf).await.unwrap(); + let mut buf = vec![0u8; 11]; + outbound.read_exact(&mut buf).await.unwrap(); assert_eq!(buf, b"hello world"); });