@@ -360,57 +360,6 @@ async fn stdin_to_websockets_task(
360360 }
361361}
362362
363- #[ tokio:: test]
364- async fn test_stdin_to_websockets_task ( ) {
365- use tokio:: sync:: mpsc:: error:: TryRecvError ;
366-
367- let ( stdintx, stdinrx) = tokio:: sync:: mpsc:: channel ( 16 ) ;
368- let ( wstx, mut wsrx) = tokio:: sync:: mpsc:: channel ( 16 ) ;
369-
370- tokio:: spawn ( async move { stdin_to_websockets_task ( stdinrx, wstx) . await } ) ;
371-
372- // send characters, receive characters
373- stdintx
374- . send ( "test post please ignore" . chars ( ) . map ( |c| c as u8 ) . collect ( ) )
375- . await
376- . unwrap ( ) ;
377- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
378- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "test post please ignore" ) ;
379-
380- // don't send ctrl-a
381- stdintx. send ( "\x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
382- assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
383-
384- // the "t" here is sent "raw" because of last ctrl-a but that doesn't change anything
385- stdintx. send ( "test" . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
386- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
387- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "test" ) ;
388-
389- // ctrl-a ctrl-c = only ctrl-c sent
390- stdintx. send ( "\x01 \x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
391- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
392- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x03 " ) ;
393-
394- // same as above, across two messages
395- stdintx. send ( "\x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
396- stdintx. send ( "\x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
397- assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
398- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
399- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x03 " ) ;
400-
401- // ctrl-a ctrl-a = only ctrl-a sent
402- stdintx. send ( "\x01 \x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
403- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
404- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x01 " ) ;
405-
406- // ctrl-c on its own means exit
407- stdintx. send ( "\x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
408- assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
409-
410- // channel is closed
411- assert ! ( wsrx. recv( ) . await . is_none( ) ) ;
412- }
413-
414363async fn serial (
415364 addr : SocketAddr ,
416365 byte_offset : Option < i64 > ,
@@ -747,3 +696,70 @@ impl Drop for RawTermiosGuard {
747696 }
748697 }
749698}
699+
700+ #[ cfg( test) ]
701+ mod test {
702+ use super :: stdin_to_websockets_task;
703+
704+ #[ tokio:: test]
705+ async fn test_stdin_to_websockets_task ( ) {
706+ use tokio:: sync:: mpsc:: error:: TryRecvError ;
707+
708+ let ( stdintx, stdinrx) = tokio:: sync:: mpsc:: channel ( 16 ) ;
709+ let ( wstx, mut wsrx) = tokio:: sync:: mpsc:: channel ( 16 ) ;
710+
711+ tokio:: spawn (
712+ async move { stdin_to_websockets_task ( stdinrx, wstx) . await } ,
713+ ) ;
714+
715+ // send characters, receive characters
716+ stdintx
717+ . send ( "test post please ignore" . chars ( ) . map ( |c| c as u8 ) . collect ( ) )
718+ . await
719+ . unwrap ( ) ;
720+ let actual = wsrx. recv ( ) . await . unwrap ( ) ;
721+ assert_eq ! (
722+ String :: from_utf8( actual) . unwrap( ) ,
723+ "test post please ignore"
724+ ) ;
725+
726+ // don't send ctrl-a
727+ stdintx. send ( "\x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
728+ assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
729+
730+ // the "t" here is sent "raw" because of last ctrl-a but that doesn't change anything
731+ stdintx. send ( "test" . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
732+ let actual = wsrx. recv ( ) . await . unwrap ( ) ;
733+ assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "test" ) ;
734+
735+ // ctrl-a ctrl-c = only ctrl-c sent
736+ stdintx
737+ . send ( "\x01 \x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) )
738+ . await
739+ . unwrap ( ) ;
740+ let actual = wsrx. recv ( ) . await . unwrap ( ) ;
741+ assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x03 " ) ;
742+
743+ // same as above, across two messages
744+ stdintx. send ( "\x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
745+ stdintx. send ( "\x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
746+ assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
747+ let actual = wsrx. recv ( ) . await . unwrap ( ) ;
748+ assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x03 " ) ;
749+
750+ // ctrl-a ctrl-a = only ctrl-a sent
751+ stdintx
752+ . send ( "\x01 \x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) )
753+ . await
754+ . unwrap ( ) ;
755+ let actual = wsrx. recv ( ) . await . unwrap ( ) ;
756+ assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x01 " ) ;
757+
758+ // ctrl-c on its own means exit
759+ stdintx. send ( "\x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
760+ assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
761+
762+ // channel is closed
763+ assert ! ( wsrx. recv( ) . await . is_none( ) ) ;
764+ }
765+ }
0 commit comments