@@ -189,15 +189,14 @@ async fn test_auto_probe() {
189
189
other => panic ! ( "Unexpected behaviour event: {:?}." , other) ,
190
190
} ;
191
191
192
- let mut has_received_response = false ;
193
- // Expect inbound dial from server.
192
+ let mut had_connection_event = false ;
194
193
loop {
195
194
match client. select_next_some ( ) . await {
196
195
SwarmEvent :: ConnectionEstablished {
197
196
endpoint, peer_id, ..
198
197
} if endpoint. is_listener ( ) => {
199
198
assert_eq ! ( peer_id, server_id) ;
200
- break ;
199
+ had_connection_event = true ;
201
200
}
202
201
SwarmEvent :: Behaviour ( Event :: OutboundProbe ( OutboundProbeEvent :: Response {
203
202
probe_id,
@@ -206,7 +205,13 @@ async fn test_auto_probe() {
206
205
} ) ) => {
207
206
assert_eq ! ( peer, server_id) ;
208
207
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 ;
210
215
}
211
216
SwarmEvent :: IncomingConnection { .. }
212
217
| SwarmEvent :: NewListenAddr { .. }
@@ -215,30 +220,20 @@ async fn test_auto_probe() {
215
220
}
216
221
}
217
222
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 {
219
227
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) ;
227
232
}
228
233
other => panic ! ( "Unexpected swarm event: {:?}." , other) ,
229
234
}
230
235
}
231
236
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
-
242
237
assert_eq ! ( client. behaviour( ) . confidence( ) , 0 ) ;
243
238
assert ! ( client. behaviour( ) . nat_status( ) . is_public( ) ) ;
244
239
assert ! ( client. behaviour( ) . public_address( ) . is_some( ) ) ;
0 commit comments