@@ -25,7 +25,7 @@ extern crate parking_lot;
2525
2626#[ macro_use] extern crate log;
2727
28- #[ cfg( test) ] extern crate tokio_core ;
28+ #[ cfg( test) ] extern crate tokio ;
2929#[ cfg( test) ] extern crate tokio_io;
3030#[ cfg( test) ] extern crate ethcore_logger;
3131
@@ -323,12 +323,10 @@ impl MetaExtractor<SocketMetadata> for PeerMetaExtractor {
323323#[ cfg( test) ]
324324mod tests {
325325 use super :: * ;
326- use std:: net:: SocketAddr ;
326+ use std:: net:: { SocketAddr , Shutdown } ;
327327 use std:: sync:: Arc ;
328328
329- use tokio_core:: reactor:: { Core , Timeout } ;
330- use tokio_core:: net:: TcpStream ;
331- use tokio_io:: io;
329+ use tokio:: { io, runtime:: Runtime , timer:: timeout:: { self , Timeout } , net:: TcpStream } ;
332330 use jsonrpc_core:: futures:: { Future , future} ;
333331
334332 use ethcore_logger:: init_log;
@@ -342,23 +340,23 @@ mod tests {
342340 }
343341
344342 fn dummy_request ( addr : & SocketAddr , data : & str ) -> Vec < u8 > {
345- let mut core = Core :: new ( ) . expect ( "Tokio Core should be created with no errors" ) ;
346- let mut buffer = vec ! [ 0u8 ; 2048 ] ;
343+ let mut runtime = Runtime :: new ( ) . expect ( "Tokio Runtime should be created with no errors" ) ;
347344
348345 let mut data_vec = data. as_bytes ( ) . to_vec ( ) ;
349346 data_vec. extend ( b"\n " ) ;
350347
351- let stream = TcpStream :: connect ( addr, & core . handle ( ) )
352- . and_then ( |stream| {
353- io:: write_all ( stream, & data_vec)
348+ let stream = TcpStream :: connect ( addr)
349+ . and_then ( move |stream| {
350+ io:: write_all ( stream, data_vec)
354351 } )
355352 . and_then ( |( stream, _) | {
356- io:: read ( stream, & mut buffer)
353+ stream. shutdown ( Shutdown :: Write ) . unwrap ( ) ;
354+ io:: read_to_end ( stream, Vec :: with_capacity ( 2048 ) )
357355 } )
358- . and_then ( |( _ , read_buf, len ) | {
359- future:: ok ( read_buf[ 0 ..len ] . to_vec ( ) )
356+ . and_then ( |( _stream , read_buf) | {
357+ future:: ok ( read_buf)
360358 } ) ;
361- let result = core . run ( stream) . expect ( "Core should run with no errors" ) ;
359+ let result = runtime . block_on ( stream) . expect ( "Runtime should run with no errors" ) ;
362360
363361 result
364362 }
@@ -417,7 +415,7 @@ mod tests {
417415 }
418416
419417 #[ test]
420- fn receives_initial_paylaod ( ) {
418+ fn receives_initial_payload ( ) {
421419 let addr = "127.0.0.1:19975" . parse ( ) . unwrap ( ) ;
422420 let _stratum = Stratum :: start ( & addr, DummyManager :: new ( ) , None ) . expect ( "There should be no error starting stratum" ) ;
423421 let request = r#"{"jsonrpc": "2.0", "method": "mining.subscribe", "params": [], "id": 2}"# ;
@@ -460,40 +458,43 @@ mod tests {
460458 . to_vec ( ) ;
461459 auth_request. extend ( b"\n " ) ;
462460
463- let mut core = Core :: new ( ) . expect ( "Tokio Core should be created with no errors" ) ;
464- let timeout1 = Timeout :: new ( :: std:: time:: Duration :: from_millis ( 100 ) , & core. handle ( ) )
465- . expect ( "There should be a timeout produced in message test" ) ;
466- let timeout2 = Timeout :: new ( :: std:: time:: Duration :: from_millis ( 100 ) , & core. handle ( ) )
467- . expect ( "There should be a timeout produced in message test" ) ;
468- let mut buffer = vec ! [ 0u8 ; 2048 ] ;
469- let mut buffer2 = vec ! [ 0u8 ; 2048 ] ;
470- let stream = TcpStream :: connect ( & addr, & core. handle ( ) )
471- . and_then ( |stream| {
472- io:: write_all ( stream, & auth_request)
461+ let auth_response = "{\" jsonrpc\" :\" 2.0\" ,\" result\" :true,\" id\" :1}\n " ;
462+
463+ let mut runtime = Runtime :: new ( ) . expect ( "Tokio Runtime should be created with no errors" ) ;
464+ let read_buf0 = vec ! [ 0u8 ; auth_response. len( ) ] ;
465+ let read_buf1 = Vec :: with_capacity ( 2048 ) ;
466+ let stream = TcpStream :: connect ( & addr)
467+ . and_then ( move |stream| {
468+ io:: write_all ( stream, auth_request)
473469 } )
474470 . and_then ( |( stream, _) | {
475- io:: read ( stream, & mut buffer )
471+ io:: read_exact ( stream, read_buf0 )
476472 } )
477- . and_then ( |( stream, _, _) | {
473+ . map_err ( |err| panic ! ( "{:?}" , err) )
474+ . and_then ( move |( stream, read_buf0) | {
475+ assert_eq ! ( String :: from_utf8( read_buf0) . unwrap( ) , auth_response) ;
478476 trace ! ( target: "stratum" , "Received authorization confirmation" ) ;
479- timeout1 . join ( future:: ok ( stream) )
477+ Timeout :: new ( future:: ok ( stream) , :: std :: time :: Duration :: from_millis ( 100 ) )
480478 } )
481- . and_then ( |( _, stream) | {
479+ . map_err ( |err : timeout:: Error < ( ) > | panic ! ( "Timeout: {:?}" , err) )
480+ . and_then ( move |stream| {
482481 trace ! ( target: "stratum" , "Pusing work to peers" ) ;
483482 stratum. push_work_all ( r#"{ "00040008", "100500" }"# . to_owned ( ) )
484483 . expect ( "Pushing work should produce no errors" ) ;
485- timeout2 . join ( future:: ok ( stream) )
484+ Timeout :: new ( future:: ok ( stream) , :: std :: time :: Duration :: from_millis ( 100 ) )
486485 } )
487- . and_then ( |( _, stream) | {
486+ . map_err ( |err : timeout:: Error < ( ) > | panic ! ( "Timeout: {:?}" , err) )
487+ . and_then ( |stream| {
488488 trace ! ( target: "stratum" , "Ready to read work from server" ) ;
489- io:: read ( stream, & mut buffer2)
489+ stream. shutdown ( Shutdown :: Write ) . unwrap ( ) ;
490+ io:: read_to_end ( stream, read_buf1)
490491 } )
491- . and_then ( |( _, read_buf , len ) | {
492+ . and_then ( |( _, read_buf1 ) | {
492493 trace ! ( target: "stratum" , "Received work from server" ) ;
493- future:: ok ( read_buf [ 0 ..len ] . to_vec ( ) )
494+ future:: ok ( read_buf1 )
494495 } ) ;
495496 let response = String :: from_utf8 (
496- core . run ( stream) . expect ( "Core should run with no errors" )
497+ runtime . block_on ( stream) . expect ( "Runtime should run with no errors" )
497498 ) . expect ( "Response should be utf-8" ) ;
498499
499500 assert_eq ! (
0 commit comments