@@ -211,6 +211,7 @@ impl<W: AsyncWrite + Unpin> FrameWrite<W> {
211
211
mod tests {
212
212
use rand:: Rng ;
213
213
use rand:: thread_rng;
214
+ use tokio:: io:: AsyncWriteExt ;
214
215
215
216
use super :: * ;
216
217
@@ -242,8 +243,6 @@ mod tests {
242
243
}
243
244
}
244
245
245
- // todo: test cancellation, frame size
246
-
247
246
#[ tokio:: test]
248
247
async fn test_write_frame_smoke ( ) {
249
248
let ( a, b) = tokio:: io:: duplex ( 4096 ) ;
@@ -265,4 +264,49 @@ mod tests {
265
264
assert_eq ! ( f1. as_ref( ) , b"hello" ) ;
266
265
assert_eq ! ( f2. as_ref( ) , b"world" ) ;
267
266
}
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
268
312
}
0 commit comments