2022-10-24 11:54:44 +11:00
|
|
|
use futures::{future, StreamExt};
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_core::multiaddr::Protocol;
|
2022-10-24 11:54:44 +11:00
|
|
|
use libp2p_core::transport::MemoryTransport;
|
|
|
|
use libp2p_core::upgrade::Version;
|
|
|
|
use libp2p_core::Transport;
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_swarm::{keep_alive, Swarm, SwarmEvent};
|
2022-10-24 11:54:44 +11:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn can_establish_connection() {
|
|
|
|
let mut swarm1 = make_swarm();
|
|
|
|
let mut swarm2 = make_swarm();
|
|
|
|
|
|
|
|
let listen_address = {
|
|
|
|
let expected_listener_id = swarm1.listen_on(Protocol::Memory(0).into()).unwrap();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match swarm1.next().await.unwrap() {
|
|
|
|
SwarmEvent::NewListenAddr {
|
|
|
|
address,
|
|
|
|
listener_id,
|
|
|
|
} if listener_id == expected_listener_id => break address,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
swarm2.dial(listen_address).unwrap();
|
|
|
|
|
|
|
|
let await_inbound_connection = async {
|
|
|
|
loop {
|
|
|
|
match swarm1.next().await.unwrap() {
|
|
|
|
SwarmEvent::ConnectionEstablished { peer_id, .. } => break peer_id,
|
|
|
|
SwarmEvent::IncomingConnectionError { error, .. } => {
|
2022-12-14 16:45:04 +01:00
|
|
|
panic!("Incoming connection failed: {error}")
|
2022-10-24 11:54:44 +11:00
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let await_outbound_connection = async {
|
|
|
|
loop {
|
|
|
|
match swarm2.next().await.unwrap() {
|
|
|
|
SwarmEvent::ConnectionEstablished { peer_id, .. } => break peer_id,
|
|
|
|
SwarmEvent::OutgoingConnectionError { error, .. } => {
|
2022-12-14 16:45:04 +01:00
|
|
|
panic!("Failed to dial: {error}")
|
2022-10-24 11:54:44 +11:00
|
|
|
}
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (inbound_peer_id, outbound_peer_id) =
|
|
|
|
future::join(await_inbound_connection, await_outbound_connection).await;
|
|
|
|
|
|
|
|
assert_eq!(&inbound_peer_id, swarm2.local_peer_id());
|
|
|
|
assert_eq!(&outbound_peer_id, swarm1.local_peer_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_swarm() -> Swarm<keep_alive::Behaviour> {
|
2022-12-13 07:58:01 +11:00
|
|
|
let identity = libp2p_core::identity::Keypair::generate_ed25519();
|
2022-10-24 11:54:44 +11:00
|
|
|
|
|
|
|
let transport = MemoryTransport::default()
|
|
|
|
.upgrade(Version::V1)
|
|
|
|
.authenticate(libp2p_tls::Config::new(&identity).unwrap())
|
2022-12-13 07:58:01 +11:00
|
|
|
.multiplex(libp2p_yamux::YamuxConfig::default())
|
2022-10-24 11:54:44 +11:00
|
|
|
.boxed();
|
|
|
|
|
2022-11-15 15:26:03 +01:00
|
|
|
Swarm::without_executor(
|
2022-10-24 11:54:44 +11:00
|
|
|
transport,
|
|
|
|
keep_alive::Behaviour,
|
|
|
|
identity.public().to_peer_id(),
|
|
|
|
)
|
|
|
|
}
|