mdns: update if-watch to 3.0.0 (#3096)

This commit is contained in:
João Oliveira
2022-11-18 01:12:23 +00:00
committed by GitHub
parent 7803524a76
commit 08510dd523
12 changed files with 173 additions and 111 deletions

View File

@ -21,7 +21,7 @@
use futures::StreamExt;
use libp2p::{
identity,
mdns::{Mdns, MdnsConfig, MdnsEvent},
mdns::{async_io::Behaviour, Config, Event},
swarm::{Swarm, SwarmEvent},
PeerId,
};
@ -30,12 +30,12 @@ use std::time::Duration;
#[async_std::test]
async fn test_discovery_async_std_ipv4() -> Result<(), Box<dyn Error>> {
run_discovery_test(MdnsConfig::default()).await
run_discovery_test(Config::default()).await
}
#[async_std::test]
async fn test_discovery_async_std_ipv6() -> Result<(), Box<dyn Error>> {
let config = MdnsConfig {
let config = Config {
enable_ipv6: true,
..Default::default()
};
@ -45,7 +45,7 @@ async fn test_discovery_async_std_ipv6() -> Result<(), Box<dyn Error>> {
#[async_std::test]
async fn test_expired_async_std() -> Result<(), Box<dyn Error>> {
env_logger::try_init().ok();
let config = MdnsConfig {
let config = Config {
ttl: Duration::from_secs(1),
query_interval: Duration::from_secs(10),
..Default::default()
@ -57,17 +57,17 @@ async fn test_expired_async_std() -> Result<(), Box<dyn Error>> {
.map_err(|e| Box::new(e) as Box<dyn Error>)
}
async fn create_swarm(config: MdnsConfig) -> Result<Swarm<Mdns>, Box<dyn Error>> {
async fn create_swarm(config: Config) -> Result<Swarm<Behaviour>, Box<dyn Error>> {
let id_keys = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(id_keys.public());
let transport = libp2p::development_transport(id_keys).await?;
let behaviour = Mdns::new(config)?;
let behaviour = Behaviour::new(config)?;
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, peer_id);
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
Ok(swarm)
}
async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
async fn run_discovery_test(config: Config) -> Result<(), Box<dyn Error>> {
env_logger::try_init().ok();
let mut a = create_swarm(config.clone()).await?;
let mut b = create_swarm(config).await?;
@ -75,7 +75,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
let mut discovered_b = false;
loop {
futures::select! {
ev = a.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) = ev {
ev = a.select_next_some() => if let SwarmEvent::Behaviour(Event::Discovered(peers)) = ev {
for (peer, _addr) in peers {
if peer == *b.local_peer_id() {
if discovered_a {
@ -86,7 +86,7 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
}
}
},
ev = b.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) = ev {
ev = b.select_next_some() => if let SwarmEvent::Behaviour(Event::Discovered(peers)) = ev {
for (peer, _addr) in peers {
if peer == *a.local_peer_id() {
if discovered_b {
@ -101,20 +101,20 @@ async fn run_discovery_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
}
}
async fn run_peer_expiration_test(config: MdnsConfig) -> Result<(), Box<dyn Error>> {
async fn run_peer_expiration_test(config: Config) -> Result<(), Box<dyn Error>> {
let mut a = create_swarm(config.clone()).await?;
let mut b = create_swarm(config).await?;
loop {
futures::select! {
ev = a.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Expired(peers)) = ev {
ev = a.select_next_some() => if let SwarmEvent::Behaviour(Event::Expired(peers)) = ev {
for (peer, _addr) in peers {
if peer == *b.local_peer_id() {
return Ok(());
}
}
},
ev = b.select_next_some() => if let SwarmEvent::Behaviour(MdnsEvent::Expired(peers)) = ev {
ev = b.select_next_some() => if let SwarmEvent::Behaviour(Event::Expired(peers)) = ev {
for (peer, _addr) in peers {
if peer == *a.local_peer_id() {
return Ok(());