Skip to content

Commit 48598fc

Browse files
authored
protocols/autonat: Fix flaky test (#2660)
Handle in test that a `OutboundProbeEvent::Response` can be reported before the associated inbound connection event. In rare cases (that only really happen in a test setup where both peers run on the same device) the server may observe a connection and report the response back to the client, before the connection event was reported at the client.
1 parent 1d6b08b commit 48598fc

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

protocols/autonat/tests/test_client.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,14 @@ async fn test_auto_probe() {
189189
other => panic!("Unexpected behaviour event: {:?}.", other),
190190
};
191191

192-
let mut has_received_response = false;
193-
// Expect inbound dial from server.
192+
let mut had_connection_event = false;
194193
loop {
195194
match client.select_next_some().await {
196195
SwarmEvent::ConnectionEstablished {
197196
endpoint, peer_id, ..
198197
} if endpoint.is_listener() => {
199198
assert_eq!(peer_id, server_id);
200-
break;
199+
had_connection_event = true;
201200
}
202201
SwarmEvent::Behaviour(Event::OutboundProbe(OutboundProbeEvent::Response {
203202
probe_id,
@@ -206,7 +205,13 @@ async fn test_auto_probe() {
206205
})) => {
207206
assert_eq!(peer, server_id);
208207
assert_eq!(probe_id, id);
209-
has_received_response = true;
208+
}
209+
SwarmEvent::Behaviour(Event::StatusChanged { old, new }) => {
210+
// Expect to flip status to public
211+
assert_eq!(old, NatStatus::Private);
212+
assert!(matches!(new, NatStatus::Public(_)));
213+
assert!(new.is_public());
214+
break;
210215
}
211216
SwarmEvent::IncomingConnection { .. }
212217
| SwarmEvent::NewListenAddr { .. }
@@ -215,30 +220,20 @@ async fn test_auto_probe() {
215220
}
216221
}
217222

218-
if !has_received_response {
223+
// It can happen that the server observed the established connection and
224+
// returned a response before the inbound established connection was reported at the client.
225+
// In this (rare) case the `ConnectionEstablished` event occurs after the `OutboundProbeEvent::Response`.
226+
if !had_connection_event {
219227
match client.select_next_some().await {
220-
SwarmEvent::Behaviour(Event::OutboundProbe(OutboundProbeEvent::Response {
221-
probe_id,
222-
peer,
223-
..
224-
})) => {
225-
assert_eq!(peer, server_id);
226-
assert_eq!(probe_id, id);
228+
SwarmEvent::ConnectionEstablished {
229+
endpoint, peer_id, ..
230+
} if endpoint.is_listener() => {
231+
assert_eq!(peer_id, server_id);
227232
}
228233
other => panic!("Unexpected swarm event: {:?}.", other),
229234
}
230235
}
231236

232-
// Expect to flip status to public
233-
match next_event(&mut client).await {
234-
Event::StatusChanged { old, new } => {
235-
assert_eq!(old, NatStatus::Private);
236-
assert!(matches!(new, NatStatus::Public(_)));
237-
assert!(new.is_public());
238-
}
239-
other => panic!("Unexpected behaviour event: {:?}.", other),
240-
}
241-
242237
assert_eq!(client.behaviour().confidence(), 0);
243238
assert!(client.behaviour().nat_status().is_public());
244239
assert!(client.behaviour().public_address().is_some());

0 commit comments

Comments
 (0)