@@ -876,3 +876,47 @@ fn send_oob_data<S: AsRawFd>(stream: &S, data: &[u8]) -> io::Result<usize> {
876
876
}
877
877
}
878
878
}
879
+
880
+ #[ test]
881
+ fn peek_readiness ( ) {
882
+ let mut buf = [ 0 ; 1024 ] ;
883
+ let ( mut poll, mut events) = init_with_poll ( ) ;
884
+
885
+ let listener = net:: TcpListener :: bind ( any_local_address ( ) ) . unwrap ( ) ;
886
+ let sockaddr = listener. local_addr ( ) . unwrap ( ) ;
887
+ let thread_handle = thread:: spawn ( move || listener. accept ( ) . unwrap ( ) ) ;
888
+ let mut stream1 = TcpStream :: connect ( sockaddr) . unwrap ( ) ;
889
+ let mut stream2 = thread_handle. join ( ) . unwrap ( ) ;
890
+
891
+ stream1. set_nonblocking ( true ) . unwrap ( ) ;
892
+ let stream1 = TcpStream :: from_std ( stream1) . unwrap ( ) ;
893
+
894
+ poll. registry ( )
895
+ . register ( & mut stream1, ID1 , Interest :: READABLE )
896
+ . unwrap ( ) ;
897
+
898
+ expect_no_events ( & mut poll, & mut events) ;
899
+ assert_would_block ( stream1. peek ( & mut buf) ) ;
900
+
901
+ assert_eq ! ( stream2. write( & [ 0 ] ) . unwrap( ) , 1 ) ;
902
+ expect_events (
903
+ & mut poll,
904
+ & mut events,
905
+ vec ! [ ExpectEvent :: new( ID1 , Readiness :: READABLE ) ] ,
906
+ ) ;
907
+ assert_eq ! ( stream1. peek( & mut buf) . unwrap( ) , 1 ) ;
908
+
909
+ assert_eq ! ( stream2. write( & [ 0 ] ) . unwrap( ) , 1 ) ;
910
+ expect_no_events ( & mut poll, & mut events) ;
911
+ assert_eq ! ( stream1. peek( & mut buf) . unwrap( ) , 2 ) ;
912
+ assert_eq ! ( stream1. read( & mut buf) . unwrap( ) , 2 ) ;
913
+ assert_would_block ( stream1. peek ( & mut buf) ) ;
914
+
915
+ drop ( stream2) ;
916
+ expect_events (
917
+ & mut poll,
918
+ & mut events,
919
+ vec ! [ ExpectEvent :: new( ID1 , Readiness :: READABLE ) ] ,
920
+ ) ;
921
+ assert_eq ! ( stream1. peek( & mut buf) . unwrap( ) , 0 ) ;
922
+ }
0 commit comments