forked from libp2p/rust-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-tokio.rs
130 lines (113 loc) · 4.3 KB
/
use-tokio.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.use futures::StreamExt;
use futures::future::Either;
use libp2p_mdns::{tokio::Behaviour, Config, Event};
use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_swarm_test::SwarmExt as _;
use std::time::Duration;
use tracing_subscriber::EnvFilter;
#[tokio::test]
async fn test_discovery_tokio_ipv4() {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();
run_discovery_test(Config::default()).await
}
#[tokio::test]
async fn test_discovery_tokio_ipv6() {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();
let config = Config {
enable_ipv6: true,
..Default::default()
};
run_discovery_test(config).await
}
#[tokio::test]
async fn test_expired_tokio() {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();
let config = Config {
ttl: Duration::from_secs(1),
query_interval: Duration::from_secs(10),
..Default::default()
};
let mut a = create_swarm(config.clone()).await;
let a_peer_id = *a.local_peer_id();
let mut b = create_swarm(config).await;
let b_peer_id = *b.local_peer_id();
loop {
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
Either::Left((Event::Expired(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == b_peer_id) {
return;
}
}
Either::Right((Event::Expired(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == a_peer_id) {
return;
}
}
_ => {}
}
}
}
async fn run_discovery_test(config: Config) {
let mut a = create_swarm(config.clone()).await;
let a_peer_id = *a.local_peer_id();
let mut b = create_swarm(config).await;
let b_peer_id = *b.local_peer_id();
let mut discovered_a = false;
let mut discovered_b = false;
while !discovered_a && !discovered_b {
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
Either::Left((Event::Discovered(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == b_peer_id) {
discovered_b = true;
}
}
Either::Right((Event::Discovered(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == a_peer_id) {
discovered_a = true;
}
}
_ => {}
}
}
}
async fn create_swarm(config: Config) -> Swarm<Behaviour> {
let mut swarm =
Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap());
// Manually listen on all interfaces because mDNS only works for non-loopback addresses.
let expected_listener_id = swarm
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
swarm
.wait(|e| match e {
SwarmEvent::NewListenAddr { listener_id, .. } => {
(listener_id == expected_listener_id).then_some(())
}
_ => None,
})
.await;
swarm
}