Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor ping tests #5655

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocols/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ tracing = { workspace = true }
void = "1.0"

[dev-dependencies]
async-std = "1.6.2"
libp2p-swarm = { workspace = true, features = ["macros"] }
libp2p-swarm-test = { path = "../../swarm-test" }
quickcheck = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tokio = {workspace = true, features = ["rt", "macros"]}

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
34 changes: 16 additions & 18 deletions protocols/ping/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ mod tests {
Endpoint,
};

#[test]
fn ping_pong() {
#[tokio::test]
async fn ping_pong() {
let mem_addr = multiaddr![Memory(thread_rng().gen::<u64>())];
let mut transport = MemoryTransport::new().boxed();
transport.listen_on(ListenerId::next(), mem_addr).unwrap();
Expand All @@ -101,27 +101,25 @@ mod tests {
.and_then(|ev| ev.into_new_address())
.expect("MemoryTransport not listening on an address!");

async_std::task::spawn(async move {
tokio::spawn(async move {
let transport_event = transport.next().await.unwrap();
let (listener_upgrade, _) = transport_event.into_incoming().unwrap();
let conn = listener_upgrade.await.unwrap();
recv_ping(conn).await.unwrap();
});

async_std::task::block_on(async move {
let c = MemoryTransport::new()
.dial(
listener_addr,
DialOpts {
role: Endpoint::Dialer,
port_use: PortUse::Reuse,
},
)
.unwrap()
.await
.unwrap();
let (_, rtt) = send_ping(c).await.unwrap();
assert!(rtt > Duration::from_secs(0));
});
let c = MemoryTransport::new()
.dial(
listener_addr,
DialOpts {
role: Endpoint::Dialer,
port_use: PortUse::Reuse,
},
)
.unwrap()
.await
.unwrap();
let (_, rtt) = send_ping(c).await.unwrap();
assert!(rtt > Duration::from_secs(0));
}
}
16 changes: 8 additions & 8 deletions protocols/ping/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ use libp2p_swarm_test::SwarmExt;
use quickcheck::*;
use std::{num::NonZeroU8, time::Duration};

#[test]
fn ping_pong() {
#[tokio::test]
async fn ping_pong() {
fn prop(count: NonZeroU8) {
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));

let mut swarm1 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone()));
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone()));

async_std::task::block_on(async {
tokio::spawn(async move {
swarm1.listen().with_memory_addr_external().await;
swarm2.connect(&mut swarm1).await;

Expand All @@ -61,16 +61,16 @@ fn assert_ping_rtt_less_than_50ms(e: ping::Event) {
assert!(rtt < Duration::from_millis(50))
}

#[test]
fn unsupported_doesnt_fail() {
#[tokio::test]
async fn unsupported_doesnt_fail() {
let mut swarm1 = Swarm::new_ephemeral(|_| dummy::Behaviour);
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(ping::Config::new()));

let result = async_std::task::block_on(async {
let result = {
swarm1.listen().with_memory_addr_external().await;
swarm2.connect(&mut swarm1).await;
let swarm1_peer_id = *swarm1.local_peer_id();
async_std::task::spawn(swarm1.loop_on_next());
tokio::spawn(swarm1.loop_on_next());

loop {
match swarm2.next_swarm_event().await {
Expand All @@ -89,7 +89,7 @@ fn unsupported_doesnt_fail() {
_ => {}
}
}
});
};

result.expect("node with ping should not fail connection due to unsupported protocol");
}
Loading