@@ -543,57 +543,115 @@ fn client_pooled_socket_disconnected() {
543
543
}
544
544
*/
545
545
546
- #[ test]
547
- fn drop_body_before_eof_closes_connection ( ) {
548
- // https://github.com/hyperium/hyper/issues/1353
546
+ mod dispatch_impl {
547
+ use super :: * ;
549
548
use std:: io:: { self , Read , Write } ;
550
549
use std:: sync:: Arc ;
551
550
use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
551
+ use std:: thread;
552
552
use std:: time:: Duration ;
553
+
554
+ use futures:: { self , Future } ;
555
+ use futures:: sync:: oneshot;
553
556
use tokio_core:: reactor:: { Timeout } ;
554
557
use tokio_core:: net:: TcpStream ;
555
558
use tokio_io:: { AsyncRead , AsyncWrite } ;
559
+
556
560
use hyper:: client:: HttpConnector ;
557
561
use hyper:: server:: Service ;
558
- use hyper:: Uri ;
562
+ use hyper:: { Client , Uri } ;
563
+ use hyper;
559
564
560
- let _ = pretty_env_logger:: init ( ) ;
561
565
562
- let server = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
563
- let addr = server. local_addr ( ) . unwrap ( ) ;
564
- let mut core = Core :: new ( ) . unwrap ( ) ;
565
- let handle = core. handle ( ) ;
566
- let closes = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
567
- let client = Client :: configure ( )
568
- . connector ( DebugConnector ( HttpConnector :: new ( 1 , & core. handle ( ) ) , closes. clone ( ) ) )
569
- . no_proto ( )
570
- . build ( & handle) ;
571
566
572
- let ( tx1, rx1) = oneshot:: channel ( ) ;
567
+ #[ test]
568
+ fn drop_body_before_eof_closes_connection ( ) {
569
+ // https://github.com/hyperium/hyper/issues/1353
570
+ let _ = pretty_env_logger:: init ( ) ;
573
571
574
- thread:: spawn ( move || {
575
- let mut sock = server. accept ( ) . unwrap ( ) . 0 ;
576
- sock. set_read_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) . unwrap ( ) ;
577
- sock. set_write_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) . unwrap ( ) ;
578
- let mut buf = [ 0 ; 4096 ] ;
579
- sock. read ( & mut buf) . expect ( "read 1" ) ;
580
- let body = vec ! [ b'x' ; 1024 * 128 ] ;
581
- write ! ( sock, "HTTP/1.1 200 OK\r \n Content-Length: {}\r \n \r \n " , body. len( ) ) . expect ( "write head" ) ;
582
- let _ = sock. write_all ( & body) ;
583
- let _ = tx1. send ( ( ) ) ;
584
- } ) ;
572
+ let server = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
573
+ let addr = server. local_addr ( ) . unwrap ( ) ;
574
+ let mut core = Core :: new ( ) . unwrap ( ) ;
575
+ let handle = core. handle ( ) ;
576
+ let closes = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
577
+ let client = Client :: configure ( )
578
+ . connector ( DebugConnector ( HttpConnector :: new ( 1 , & core. handle ( ) ) , closes. clone ( ) ) )
579
+ . no_proto ( )
580
+ . build ( & handle) ;
581
+
582
+ let ( tx1, rx1) = oneshot:: channel ( ) ;
583
+
584
+ thread:: spawn ( move || {
585
+ let mut sock = server. accept ( ) . unwrap ( ) . 0 ;
586
+ sock. set_read_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) . unwrap ( ) ;
587
+ sock. set_write_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) . unwrap ( ) ;
588
+ let mut buf = [ 0 ; 4096 ] ;
589
+ sock. read ( & mut buf) . expect ( "read 1" ) ;
590
+ let body = vec ! [ b'x' ; 1024 * 128 ] ;
591
+ write ! ( sock, "HTTP/1.1 200 OK\r \n Content-Length: {}\r \n \r \n " , body. len( ) ) . expect ( "write head" ) ;
592
+ let _ = sock. write_all ( & body) ;
593
+ let _ = tx1. send ( ( ) ) ;
594
+ } ) ;
585
595
586
- let uri = format ! ( "http://{}/a" , addr) . parse ( ) . unwrap ( ) ;
596
+ let uri = format ! ( "http://{}/a" , addr) . parse ( ) . unwrap ( ) ;
587
597
588
- let res = client. get ( uri) . and_then ( move |res| {
589
- assert_eq ! ( res. status( ) , hyper:: StatusCode :: Ok ) ;
590
- Timeout :: new ( Duration :: from_secs ( 1 ) , & handle) . unwrap ( )
591
- . from_err ( )
592
- } ) ;
593
- let rx = rx1. map_err ( |_| hyper:: Error :: Io ( io:: Error :: new ( io:: ErrorKind :: Other , "thread panicked" ) ) ) ;
594
- core. run ( res. join ( rx) . map ( |r| r. 0 ) ) . unwrap ( ) ;
598
+ let res = client. get ( uri) . and_then ( move |res| {
599
+ assert_eq ! ( res. status( ) , hyper:: StatusCode :: Ok ) ;
600
+ Timeout :: new ( Duration :: from_secs ( 1 ) , & handle) . unwrap ( )
601
+ . from_err ( )
602
+ } ) ;
603
+ let rx = rx1. map_err ( |_| hyper:: Error :: Io ( io:: Error :: new ( io:: ErrorKind :: Other , "thread panicked" ) ) ) ;
604
+ core. run ( res. join ( rx) . map ( |r| r. 0 ) ) . unwrap ( ) ;
605
+
606
+ assert_eq ! ( closes. load( Ordering :: Relaxed ) , 1 ) ;
607
+ }
608
+
609
+ #[ test]
610
+ fn drop_client_closes_connection ( ) {
611
+ // https://github.com/hyperium/hyper/issues/1353
612
+ let _ = pretty_env_logger:: init ( ) ;
595
613
596
- assert_eq ! ( closes. load( Ordering :: Relaxed ) , 1 ) ;
614
+ let server = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
615
+ let addr = server. local_addr ( ) . unwrap ( ) ;
616
+ let mut core = Core :: new ( ) . unwrap ( ) ;
617
+ let handle = core. handle ( ) ;
618
+ let closes = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
619
+
620
+ let ( tx1, rx1) = oneshot:: channel ( ) ;
621
+
622
+ thread:: spawn ( move || {
623
+ let mut sock = server. accept ( ) . unwrap ( ) . 0 ;
624
+ sock. set_read_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) . unwrap ( ) ;
625
+ sock. set_write_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) . unwrap ( ) ;
626
+ let mut buf = [ 0 ; 4096 ] ;
627
+ sock. read ( & mut buf) . expect ( "read 1" ) ;
628
+ let body =[ b'x' ; 64 ] ;
629
+ write ! ( sock, "HTTP/1.1 200 OK\r \n Content-Length: {}\r \n \r \n " , body. len( ) ) . expect ( "write head" ) ;
630
+ let _ = sock. write_all ( & body) ;
631
+ let _ = tx1. send ( ( ) ) ;
632
+ } ) ;
633
+
634
+ let uri = format ! ( "http://{}/a" , addr) . parse ( ) . unwrap ( ) ;
635
+
636
+ let res = {
637
+ let client = Client :: configure ( )
638
+ . connector ( DebugConnector ( HttpConnector :: new ( 1 , & handle) , closes. clone ( ) ) )
639
+ . no_proto ( )
640
+ . build ( & handle) ;
641
+ client. get ( uri) . and_then ( move |res| {
642
+ assert_eq ! ( res. status( ) , hyper:: StatusCode :: Ok ) ;
643
+ res. body ( ) . concat2 ( )
644
+ } ) . and_then ( |_| {
645
+ Timeout :: new ( Duration :: from_secs ( 1 ) , & handle) . unwrap ( )
646
+ . from_err ( )
647
+ } )
648
+ } ;
649
+ // client is dropped
650
+ let rx = rx1. map_err ( |_| hyper:: Error :: Io ( io:: Error :: new ( io:: ErrorKind :: Other , "thread panicked" ) ) ) ;
651
+ core. run ( res. join ( rx) . map ( |r| r. 0 ) ) . unwrap ( ) ;
652
+
653
+ assert_eq ! ( closes. load( Ordering :: Relaxed ) , 1 ) ;
654
+ }
597
655
598
656
599
657
0 commit comments