@@ -2,7 +2,7 @@ use std::fmt;
22use std:: ascii:: AsciiExt ;
33use std:: io:: { self , Read , Write , Cursor } ;
44use std:: cell:: RefCell ;
5- use std:: net:: SocketAddr ;
5+ use std:: net:: { SocketAddr , Shutdown } ;
66use std:: sync:: { Arc , Mutex } ;
77#[ cfg( feature = "timeouts" ) ]
88use std:: time:: Duration ;
@@ -21,10 +21,13 @@ use net::{NetworkStream, NetworkConnector};
2121pub struct MockStream {
2222 pub read : Cursor < Vec < u8 > > ,
2323 pub write : Vec < u8 > ,
24+ pub is_closed : bool ,
25+ pub error_on_write : bool ,
26+ pub error_on_read : bool ,
2427 #[ cfg( feature = "timeouts" ) ]
2528 pub read_timeout : Cell < Option < Duration > > ,
2629 #[ cfg( feature = "timeouts" ) ]
27- pub write_timeout : Cell < Option < Duration > >
30+ pub write_timeout : Cell < Option < Duration > > ,
2831}
2932
3033impl fmt:: Debug for MockStream {
@@ -48,7 +51,10 @@ impl MockStream {
4851 pub fn with_input ( input : & [ u8 ] ) -> MockStream {
4952 MockStream {
5053 read : Cursor :: new ( input. to_vec ( ) ) ,
51- write : vec ! [ ]
54+ write : vec ! [ ] ,
55+ is_closed : false ,
56+ error_on_write : false ,
57+ error_on_read : false ,
5258 }
5359 }
5460
@@ -57,6 +63,9 @@ impl MockStream {
5763 MockStream {
5864 read : Cursor :: new ( input. to_vec ( ) ) ,
5965 write : vec ! [ ] ,
66+ is_closed : false ,
67+ error_on_write : false ,
68+ error_on_read : false ,
6069 read_timeout : Cell :: new ( None ) ,
6170 write_timeout : Cell :: new ( None ) ,
6271 }
@@ -65,13 +74,21 @@ impl MockStream {
6574
6675impl Read for MockStream {
6776 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
68- self . read . read ( buf)
77+ if self . error_on_read {
78+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , "mock error" ) )
79+ } else {
80+ self . read . read ( buf)
81+ }
6982 }
7083}
7184
7285impl Write for MockStream {
7386 fn write ( & mut self , msg : & [ u8 ] ) -> io:: Result < usize > {
74- Write :: write ( & mut self . write , msg)
87+ if self . error_on_write {
88+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , "mock error" ) )
89+ } else {
90+ Write :: write ( & mut self . write , msg)
91+ }
7592 }
7693
7794 fn flush ( & mut self ) -> io:: Result < ( ) > {
@@ -95,6 +112,11 @@ impl NetworkStream for MockStream {
95112 self . write_timeout . set ( dur) ;
96113 Ok ( ( ) )
97114 }
115+
116+ fn close ( & mut self , _how : Shutdown ) -> io:: Result < ( ) > {
117+ self . is_closed = true ;
118+ Ok ( ( ) )
119+ }
98120}
99121
100122/// A wrapper around a `MockStream` that allows one to clone it and keep an independent copy to the
@@ -144,6 +166,10 @@ impl NetworkStream for CloneableMockStream {
144166 fn set_write_timeout ( & self , dur : Option < Duration > ) -> io:: Result < ( ) > {
145167 self . inner . lock ( ) . unwrap ( ) . set_write_timeout ( dur)
146168 }
169+
170+ fn close ( & mut self , how : Shutdown ) -> io:: Result < ( ) > {
171+ NetworkStream :: close ( & mut * self . inner . lock ( ) . unwrap ( ) , how)
172+ }
147173}
148174
149175impl CloneableMockStream {
0 commit comments