2020-03-22 15:00:06 +02:00
use async_std ::task ;
/// Make sure two instances of ipfs can be connected.
#[ test ]
fn connect_two_nodes ( ) {
2020-03-22 16:42:47 +02:00
// env_logger::init();
2020-03-22 15:00:06 +02:00
// make sure the connection will only happen through explicit connect
let mdns = false ;
let ( tx , rx ) = futures ::channel ::oneshot ::channel ( ) ;
let node_a = task ::spawn ( async move {
let opts = ipfs ::IpfsOptions ::inmemory_with_generated_keys ( mdns ) ;
2020-03-22 16:43:55 +02:00
let ( ipfs , fut ) = ipfs ::UninitializedIpfs ::new ( opts )
. await
. start ( )
. await
. unwrap ( ) ;
2020-03-22 15:00:06 +02:00
let jh = task ::spawn ( fut ) ;
2020-03-22 16:43:55 +02:00
let ( pk , addrs ) = ipfs
. identity ( )
. await
. expect ( " failed to read identity() on node_a " ) ;
2020-03-22 16:42:47 +02:00
assert! ( ! addrs . is_empty ( ) ) ;
2020-03-22 15:00:06 +02:00
tx . send ( ( pk , addrs , ipfs , jh ) ) . unwrap ( ) ;
} ) ;
task ::block_on ( async move {
let ( other_pk , other_addrs , other_ipfs , other_jh ) = rx . await . unwrap ( ) ;
2020-03-22 15:44:35 +02:00
println! ( " got back from the other node: {:?} " , other_addrs ) ;
2020-03-22 15:00:06 +02:00
let opts = ipfs ::IpfsOptions ::inmemory_with_generated_keys ( mdns ) ;
2020-03-22 16:43:55 +02:00
let ( ipfs , fut ) = ipfs ::UninitializedIpfs ::new ( opts )
. await
. start ( )
. await
. unwrap ( ) ;
2020-03-22 15:00:06 +02:00
let jh = task ::spawn ( fut ) ;
let _other_peerid = other_pk . into_peer_id ( ) ;
let mut connected = None ;
for addr in other_addrs {
2020-03-22 15:44:35 +02:00
println! ( " trying {} " , addr ) ;
2020-03-22 15:00:06 +02:00
match ipfs . connect ( addr . clone ( ) ) . await {
Ok ( _ ) = > {
connected = Some ( addr ) ;
break ;
2020-03-22 16:43:55 +02:00
}
2020-03-22 15:00:06 +02:00
Err ( e ) = > {
2020-03-22 16:42:47 +02:00
println! ( " Failed connecting to {} : {} " , addr , e ) ;
2020-03-22 15:00:06 +02:00
}
}
}
let connected = connected . expect ( " Failed to connect to anything " ) ;
2020-03-22 16:42:47 +02:00
println! ( " connected to {} " , connected ) ;
2020-03-22 15:00:06 +02:00
other_ipfs . exit_daemon ( ) . await ;
other_jh . await ;
node_a . await ;
ipfs . exit_daemon ( ) . await ;
jh . await ;
} ) ;
}
2020-05-19 17:26:57 +03:00
/// More complicated one to the above; first node will have two listening addresses and the second
/// one should dial both of the addresses, resulting in two connections.
#[ test ]
fn connect_two_nodes_with_two_connections_doesnt_panic ( ) {
const MDNS : bool = false ;
task ::block_on ( async move {
let node_a = ipfs ::Node ::new ( MDNS ) . await ;
let node_b = ipfs ::Node ::new ( MDNS ) . await ;
node_a
. add_listening_address ( libp2p ::build_multiaddr! ( Ip4 ( [ 127 , 0 , 0 , 1 ] ) , Tcp ( 0 u16 ) ) )
. await
. unwrap ( ) ;
let addresses = node_a . addrs_local ( ) . await . unwrap ( ) ;
assert_eq! (
addresses . len ( ) ,
2 ,
" there should had been two local addresses, found {:?} " ,
addresses
) ;
for addr in addresses {
node_b . connect ( addr ) . await . unwrap ( ) ;
}
// not too sure on this, since there'll be a single peer but two connections; the return
// type is `Vec<Connection>` but it's peer with any connection.
let mut peers = node_a . peers ( ) . await . unwrap ( ) ;
assert_eq! (
peers . len ( ) ,
1 ,
" there should had been one peer, found {:?} " ,
peers
) ;
// sadly we are unable to currently verify that there exists two connections for the node_b
// peer..
node_a
. disconnect ( peers . remove ( 0 ) . address )
. await
. expect ( " failed to disconnect peer_b at peer_a " ) ;
let peers = node_a . peers ( ) . await . unwrap ( ) ;
assert! (
peers . is_empty ( ) ,
" node_b was still connected after disconnect: {:?} " ,
peers
) ;
} ) ;
}