Skip to content

Commit 53e477d

Browse files
authoredFeb 2, 2023
refactor(interop): update ping to conform to the new interop spec (#3423)
1 parent babf7e3 commit 53e477d

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed
 

‎interop-tests/src/bin/ping.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::env;
22
use std::str::FromStr;
3-
use std::time::Duration;
3+
use std::time::{Duration, Instant};
44

5-
use anyhow::{Context, Result};
5+
use anyhow::{bail, Context, Result};
66
use either::Either;
77
use env_logger::{Env, Target};
88
use futures::{future, AsyncRead, AsyncWrite, StreamExt};
@@ -32,11 +32,11 @@ async fn main() -> Result<()> {
3232
.unwrap_or_else(|_| "true".into())
3333
.parse::<bool>()?;
3434

35-
let test_timeout = env::var("test_timeout")
35+
let test_timeout = env::var("test_timeout_seconds")
3636
.unwrap_or_else(|_| "10".into())
37-
.parse::<usize>()?;
37+
.parse::<u64>()?;
3838

39-
let redis_addr = env::var("REDIS_ADDR")
39+
let redis_addr = env::var("redis_addr")
4040
.map(|addr| format!("redis://{addr}"))
4141
.unwrap_or_else(|_| "redis://redis:6379".into());
4242

@@ -105,26 +105,31 @@ async fn main() -> Result<()> {
105105
// retrieved via `listenAddr` key over the redis connection. Or wait to be pinged and have
106106
// `dialerDone` key ready on the redis connection.
107107
if is_dialer {
108-
let result: Vec<String> = conn.blpop("listenerAddr", test_timeout).await?;
108+
let result: Vec<String> = conn.blpop("listenerAddr", test_timeout as usize).await?;
109109
let other = result
110110
.get(1)
111111
.context("Failed to wait for listener to be ready")?;
112112

113+
let handshake_start = Instant::now();
114+
113115
swarm.dial(other.parse::<Multiaddr>()?)?;
114116
log::info!("Test instance, dialing multiaddress on: {}.", other);
115117

116-
loop {
118+
let rtt = loop {
117119
if let Some(SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
118120
peer: _,
119121
result: Ok(ping::Success::Ping { rtt }),
120122
}))) = swarm.next().await
121123
{
122124
log::info!("Ping successful: {rtt:?}");
123-
break;
125+
break rtt.as_millis() as f32;
124126
}
125-
}
127+
};
126128

127-
conn.rpush("dialerDone", "").await?;
129+
let handshake_plus_ping = handshake_start.elapsed().as_millis() as f32;
130+
println!(
131+
r#"{{"handshakePlusOneRTTMillis": {handshake_plus_ping:.1}, "pingRTTMilllis": {rtt:.1}}}"#
132+
);
128133
} else {
129134
loop {
130135
if let Some(SwarmEvent::NewListenAddr {
@@ -149,11 +154,8 @@ async fn main() -> Result<()> {
149154
swarm.next().await;
150155
}
151156
});
152-
153-
let done: Vec<String> = conn.blpop("dialerDone", test_timeout).await?;
154-
done.get(1)
155-
.context("Failed to wait for dialer conclusion")?;
156-
log::info!("Ping successful");
157+
tokio::time::sleep(Duration::from_secs(test_timeout)).await;
158+
bail!("Test should have been killed by the test runner!");
157159
}
158160

159161
Ok(())

0 commit comments

Comments
 (0)
Please sign in to comment.