-
Notifications
You must be signed in to change notification settings - Fork 237
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
Extra test for rust-libp2p ping protocol #3242
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(ping) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
add_shadow_tests(BASENAME libp2p-ping | ||
SHADOW_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/ping.yaml" | ||
POST_CMD "${CMAKE_CURRENT_SOURCE_DIR}/verify.sh" | ||
CONFIGURATIONS extra) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
general: | ||
stop_time: 5 min | ||
model_unblocked_syscall_latency: true | ||
network: | ||
graph: | ||
type: 1_gbit_switch | ||
hosts: | ||
peer1: | ||
network_node_id: 0 | ||
ip_addr: 1.1.1.1 | ||
processes: | ||
- path: ../../../target/debug/test_libp2p_ping | ||
args: "9001" | ||
start_time: 1 | ||
expected_final_state: running | ||
peer2: | ||
network_node_id: 0 | ||
ip_addr: 1.1.1.2 | ||
processes: | ||
- path: ../../../target/debug/test_libp2p_ping | ||
args: ['9001', '/ip4/1.1.1.1/tcp/9001'] | ||
start_time: 5 | ||
expected_final_state: running |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use futures::prelude::*; | ||
use libp2p::{multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr}; | ||
use std::{error::Error, time::Duration}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
// Usage: ./test_libp2p_ping [listen-port] [peer-multiaddr] | ||
|
||
let mut swarm = libp2p::SwarmBuilder::with_new_identity() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For those of us who aren't familiar with libp2p, can you add a few more comments about what the program is doing, and what things like |
||
.with_tokio() | ||
.with_tcp( | ||
tcp::Config::default(), | ||
noise::Config::new, | ||
yamux::Config::default, | ||
)? | ||
.with_behaviour(|_| ping::Behaviour::default())? | ||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) | ||
.build(); | ||
|
||
// Set the listening port to the one given as the first command-line | ||
// argument or zero if it is not given. | ||
let mut addr: Multiaddr = "/ip4/0.0.0.0".parse()?; | ||
let port = match std::env::args().nth(1) { | ||
Some(port) => port.parse()?, | ||
None => 0, | ||
}; | ||
addr.push(Protocol::Tcp(port)); | ||
|
||
// Listen for the incoming peers. | ||
swarm.listen_on(addr)?; | ||
|
||
// Dial the peer identified by the multi-address given as the second | ||
// command-line argument, if any. | ||
if let Some(addr) = std::env::args().nth(2) { | ||
let remote: Multiaddr = addr.parse()?; | ||
swarm.dial(remote)?; | ||
println!("Dialed {addr}") | ||
} | ||
|
||
loop { | ||
match swarm.select_next_some().await { | ||
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), | ||
SwarmEvent::Behaviour(event) => println!("{event:?}"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the |
||
_ => {} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
echo "Test libp2p ping" | ||
|
||
text="Dialed /ip4/1.1.1.1/tcp/9001" | ||
cat ./hosts/peer2/*.stdout | grep "$text" | ||
text="Listening on \"/ip4/127.0.0.1/tcp/9001\"" | ||
cat ./hosts/peer1/*.stdout | grep "$text" | ||
cat ./hosts/peer2/*.stdout | grep "$text" | ||
text="Listening on \"/ip4/1.1.1.1/tcp/9001\"" | ||
cat ./hosts/peer1/*.stdout | grep "$text" | ||
text="Listening on \"/ip4/1.1.1.2/tcp/9001\"" | ||
cat ./hosts/peer2/*.stdout | grep "$text" | ||
|
||
expected_ping_count=20 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the expected count 20? Can you add a comment? |
||
regex="Event { peer: PeerId(.*), connection: ConnectionId(.*), result: Ok(.*) }" | ||
|
||
actual_ping_count=$(cat ./hosts/peer1/*.stdout | grep "$regex" | wc -l) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the shell lint check:
(And for the other |
||
if [[ "${actual_ping_count}" != "${expected_ping_count}" ]]; then | ||
printf "Peer1 doesn't have the expected number of pings" | ||
exit 1 | ||
fi | ||
actual_ping_count=$(cat ./hosts/peer2/*.stdout | grep "$regex" | wc -l) | ||
if [[ "${actual_ping_count}" != "${expected_ping_count}" ]]; then | ||
printf "Peer2 doesn't have the expected number of pings" | ||
exit 1 | ||
fi | ||
|
||
echo "Verification succeeded" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the "full" feature needed, or is there a smaller set of features that could be used instead?