Skip to content

Commit

Permalink
protocols/autonat/tests: Ignore irrelevant events (#2450)
Browse files Browse the repository at this point in the history
Ignore `SwarmEvent::ExpiredListenAddr` in tests.
Print swarm event on panic.

Co-authored-by: Max Inden <[email protected]>
  • Loading branch information
elenaf9 and mxinden authored Jan 29, 2022
1 parent 45bb66f commit 13ded7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
38 changes: 20 additions & 18 deletions protocols/autonat/tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async fn test_auto_probe() {
assert!(peer.is_none());
assert_eq!(error, OutboundProbeError::NoAddresses);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

assert_eq!(client.behaviour().nat_status(), NatStatus::Unknown);
Expand All @@ -139,7 +139,7 @@ async fn test_auto_probe() {
assert_eq!(peer, server_id);
probe_id
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

match next_event(&mut client).await {
Expand All @@ -155,15 +155,15 @@ async fn test_auto_probe() {
OutboundProbeError::Response(ResponseError::DialError)
);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

match next_event(&mut client).await {
Event::StatusChanged { old, new } => {
assert_eq!(old, NatStatus::Unknown);
assert_eq!(new, NatStatus::Private);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

assert_eq!(client.behaviour().confidence(), 0);
Expand All @@ -186,7 +186,7 @@ async fn test_auto_probe() {
assert_eq!(peer, server_id);
probe_id
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

// Expect inbound dial from server.
Expand All @@ -198,8 +198,10 @@ async fn test_auto_probe() {
assert_eq!(peer_id, server_id);
break;
}
SwarmEvent::IncomingConnection { .. } | SwarmEvent::NewListenAddr { .. } => {}
_ => panic!("Unexpected Swarm Event"),
SwarmEvent::IncomingConnection { .. }
| SwarmEvent::NewListenAddr { .. }
| SwarmEvent::ExpiredListenAddr { .. } => {}
other => panic!("Unexpected swarm event: {:?}.", other),
}
}

Expand All @@ -208,7 +210,7 @@ async fn test_auto_probe() {
assert_eq!(peer, server_id);
assert_eq!(probe_id, id);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

// Expect to flip status to public
Expand All @@ -218,7 +220,7 @@ async fn test_auto_probe() {
assert!(matches!(new, NatStatus::Public(_)));
assert!(new.is_public());
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

assert_eq!(client.behaviour().confidence(), 0);
Expand Down Expand Up @@ -271,7 +273,7 @@ async fn test_confidence() {
assert_eq!(peer, server_id);
probe_id
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

match next_event(&mut client).await {
Expand All @@ -296,7 +298,7 @@ async fn test_confidence() {
assert_eq!(peer, server_id);
assert_eq!(probe_id, id);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

// Confidence should increase each iteration up to MAX_CONFIDENCE
Expand All @@ -315,7 +317,7 @@ async fn test_confidence() {
assert_eq!(old, NatStatus::Unknown);
assert_eq!(new.is_public(), test_public);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}
}
}
Expand Down Expand Up @@ -364,7 +366,7 @@ async fn test_throttle_server_period() {
}
Event::OutboundProbe(OutboundProbeEvent::Request { .. }) => {}
Event::OutboundProbe(OutboundProbeEvent::Response { .. }) => {}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}
}

Expand All @@ -377,7 +379,7 @@ async fn test_throttle_server_period() {
assert!(peer.is_none());
assert_eq!(error, OutboundProbeError::NoServer);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}
assert_eq!(client.behaviour().confidence(), 0);

Expand Down Expand Up @@ -423,14 +425,14 @@ async fn test_use_connected_as_server() {
Event::OutboundProbe(OutboundProbeEvent::Request { peer, .. }) => {
assert_eq!(peer, server_id);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

match next_event(&mut client).await {
Event::OutboundProbe(OutboundProbeEvent::Response { peer, .. }) => {
assert_eq!(peer, server_id);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

drop(_handle);
Expand Down Expand Up @@ -481,7 +483,7 @@ async fn test_outbound_failure() {
}
Event::OutboundProbe(OutboundProbeEvent::Request { .. }) => {}
Event::OutboundProbe(OutboundProbeEvent::Response { .. }) => {}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}
}

Expand All @@ -504,7 +506,7 @@ async fn test_outbound_failure() {
}) => {
assert!(inactive_ids.contains(&peer));
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}
}
};
Expand Down
38 changes: 20 additions & 18 deletions protocols/autonat/tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ async fn test_dial_back() {
};
break observed_client_ip;
}
SwarmEvent::IncomingConnection { .. } | SwarmEvent::NewListenAddr { .. } => {}
_ => panic!("Unexpected Swarm Event"),
SwarmEvent::IncomingConnection { .. }
| SwarmEvent::NewListenAddr { .. }
| SwarmEvent::ExpiredListenAddr { .. } => {}
other => panic!("Unexpected swarm event: {:?}.", other),
}
};
let expect_addr = Multiaddr::empty()
Expand All @@ -184,7 +186,7 @@ async fn test_dial_back() {
assert_eq!(addresses[0], expect_addr);
probe_id
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

loop {
Expand All @@ -206,8 +208,8 @@ async fn test_dial_back() {
break;
}
SwarmEvent::Dialing(peer) => assert_eq!(peer, client_id),
SwarmEvent::NewListenAddr { .. } => {}
_ => panic!("Unexpected Swarm Event"),
SwarmEvent::NewListenAddr { .. } | SwarmEvent::ExpiredListenAddr { .. } => {}
other => panic!("Unexpected swarm event: {:?}.", other),
}
}

Expand All @@ -221,7 +223,7 @@ async fn test_dial_back() {
assert_eq!(peer, client_id);
assert_eq!(address, expect_addr);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

drop(_handle);
Expand All @@ -241,7 +243,7 @@ async fn test_dial_error() {
assert_eq!(peer, client_id);
probe_id
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

loop {
Expand All @@ -252,8 +254,8 @@ async fn test_dial_error() {
break;
}
SwarmEvent::Dialing(peer) => assert_eq!(peer, client_id),
SwarmEvent::NewListenAddr { .. } => {}
_ => panic!("Unexpected Swarm Event"),
SwarmEvent::NewListenAddr { .. } | SwarmEvent::ExpiredListenAddr { .. } => {}
other => panic!("Unexpected swarm event: {:?}.", other),
}
}

Expand All @@ -267,7 +269,7 @@ async fn test_dial_error() {
assert_eq!(peer, client_id);
assert_eq!(error, InboundProbeError::Response(ResponseError::DialError));
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

drop(_handle);
Expand Down Expand Up @@ -296,7 +298,7 @@ async fn test_throttle_global_max() {
Event::InboundProbe(InboundProbeEvent::Request { peer, probe_id, .. }) => {
(probe_id, peer)
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

loop {
Expand All @@ -314,7 +316,7 @@ async fn test_throttle_global_max() {
assert_eq!(first_peer_id, peer);
assert_eq!(first_probe_id, probe_id);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};
}

Expand Down Expand Up @@ -342,15 +344,15 @@ async fn test_throttle_peer_max() {
assert_eq!(client_id, peer);
probe_id
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

match next_event(&mut server).await {
Event::InboundProbe(InboundProbeEvent::Response { peer, probe_id, .. }) => {
assert_eq!(peer, client_id);
assert_eq!(probe_id, first_probe_id);
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
}

match next_event(&mut server).await {
Expand All @@ -366,7 +368,7 @@ async fn test_throttle_peer_max() {
InboundProbeError::Response(ResponseError::DialRefused)
)
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

drop(_handle);
Expand Down Expand Up @@ -396,7 +398,7 @@ async fn test_dial_multiple_addr() {
assert_eq!(client_id, peer);
addresses
}
other => panic!("Unexpected Event: {:?}", other),
other => panic!("Unexpected behaviour event: {:?}.", other),
};

loop {
Expand All @@ -419,8 +421,8 @@ async fn test_dial_multiple_addr() {
break;
}
SwarmEvent::Dialing(peer) => assert_eq!(peer, client_id),
SwarmEvent::NewListenAddr { .. } => {}
_ => panic!("Unexpected Swarm Event"),
SwarmEvent::NewListenAddr { .. } | SwarmEvent::ExpiredListenAddr { .. } => {}
other => panic!("Unexpected swarm event: {:?}.", other),
}
}
};
Expand Down

0 comments on commit 13ded7f

Please sign in to comment.