Skip to content

Commit 84a0c2f

Browse files
channel: net: framed: eof tests (#927)
Summary: Pull Request resolved: #927 add tests for `FrameReader` `EOF` behavior. test boundary `EOF` (returns `Ok(None)` after a complete frame) and mid-frame `EOF` (returns `UnexpectedEof` when body incomplete). Reviewed By: mariusae Differential Revision: D80546448 fbshipit-source-id: 6faa8ef04d5316af20f6dab6efba274d247ba913
1 parent 8ee4446 commit 84a0c2f

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

hyperactor/src/channel/net/framed.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ impl<W: AsyncWrite + Unpin> FrameWrite<W> {
211211
mod tests {
212212
use rand::Rng;
213213
use rand::thread_rng;
214+
use tokio::io::AsyncWriteExt;
214215

215216
use super::*;
216217

@@ -242,8 +243,6 @@ mod tests {
242243
}
243244
}
244245

245-
// todo: test cancellation, frame size
246-
247246
#[tokio::test]
248247
async fn test_write_frame_smoke() {
249248
let (a, b) = tokio::io::duplex(4096);
@@ -265,4 +264,49 @@ mod tests {
265264
assert_eq!(f1.as_ref(), b"hello");
266265
assert_eq!(f2.as_ref(), b"world");
267266
}
267+
268+
#[tokio::test]
269+
async fn test_reader_eof_at_boundary() {
270+
let (a, b) = tokio::io::duplex(4096);
271+
let (r, _wu) = tokio::io::split(a);
272+
let (_ru, mut w) = tokio::io::split(b);
273+
let mut reader = FrameReader::new(r, 1024);
274+
275+
// Write a complete frame.
276+
w = FrameWrite::write_frame(w, Bytes::from_static(b"done"))
277+
.await
278+
.unwrap();
279+
// Now, shutdown the writer so the peer gets an EOF.
280+
w.shutdown().await.unwrap();
281+
drop(w);
282+
assert_eq!(
283+
reader.next().await.unwrap(),
284+
Some(Bytes::from_static(b"done"))
285+
);
286+
// Boundary EOF.
287+
assert!(reader.next().await.unwrap().is_none());
288+
}
289+
290+
#[tokio::test]
291+
async fn test_reader_eof_mid_frame() {
292+
let (a, b) = tokio::io::duplex(4096);
293+
let (r, _wu) = tokio::io::split(a);
294+
let (_ru, mut w) = tokio::io::split(b);
295+
let mut reader = FrameReader::new(r, 1024);
296+
297+
// Start a frame of length 5.
298+
let mut len = bytes::BytesMut::with_capacity(8);
299+
len.put_u64(5);
300+
w.write_all(&len.freeze()).await.unwrap();
301+
// Write only 2 bytes of the body.
302+
w.write_all(b"he").await.unwrap();
303+
// Shutdown the writer so there's an EOF mid frame.
304+
w.shutdown().await.unwrap();
305+
306+
// Reading back the frame will manifest an error.
307+
let err = reader.next().await.unwrap_err();
308+
assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
309+
}
310+
311+
// todo: test cancellation, frame size
268312
}

0 commit comments

Comments
 (0)