Skip to content

Commit

Permalink
fix(test): add test await timeout 2
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Nov 3, 2024
1 parent b43b9ed commit a38b8b4
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions libs/net4mqtt/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::time::{interval, sleep, timeout_at, Duration, Instant};
#[macro_export]
macro_rules! timeout_await {
($future:expr) => {
timeout_at(Instant::now() + Duration::from_millis(10), $future)
timeout_at(Instant::now() + Duration::from_secs(1), $future)
.await
.unwrap()
};
Expand Down Expand Up @@ -427,12 +427,12 @@ async fn test_tcp_echo() {
let mut buf = [0; MAX_BUFFER_SIZE];
let test_msg = b"hello, world";
socket.write_all(test_msg).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_millis(100)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(&buf[..len], test_msg);

let test_msg2 = b"hello, world2";
socket.write_all(test_msg2).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_millis(100)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(&buf[..len], test_msg2);
}

Expand Down Expand Up @@ -500,12 +500,12 @@ async fn test_kcp_add() {

let test_msg = b"1+2";
socket.write_all(test_msg).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_secs(1)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(std::str::from_utf8(&buf[..len]), Ok("3"));

let test_msg2 = b"123456+543210";
socket.write_all(test_msg2).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_secs(1)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(std::str::from_utf8(&buf[..len]), Ok("666666"));
}

Expand Down Expand Up @@ -570,12 +570,12 @@ async fn test_kcp() {
let mut buf = [0; MAX_BUFFER_SIZE];
let test_msg = b"hello, world";
socket.write_all(test_msg).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_secs(1)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(&buf[..len], test_msg);

let test_msg2 = b"hello, world2";
socket.write_all(test_msg2).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_secs(1)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(&buf[..len], test_msg2);
}

Expand Down Expand Up @@ -608,12 +608,12 @@ async fn test_kcp_echo_restart() {
let mut buf = [0; MAX_BUFFER_SIZE];
let test_msg = format!("hello, world: {}", i);
socket.write_all(test_msg.as_bytes()).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_secs(1)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(&buf[..len], test_msg.as_bytes());

let test_msg2 = format!("the end: {}", i);
socket.write_all(test_msg2.as_bytes()).await.unwrap();
let len = timeout_await!(socket.read(&mut buf), Duration::from_secs(1)).unwrap();
let len = timeout_await!(socket.read(&mut buf)).unwrap();
assert_eq!(&buf[..len], test_msg2.as_bytes());

handle.abort();
Expand Down Expand Up @@ -671,12 +671,15 @@ async fn test_socks_simple() {
.build()
.unwrap();

let res = timeout_await!(client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry("0")
))
.send())
let res = timeout_await!(
client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry("0")
))
.send(),
Duration::from_secs(5)
)
.unwrap();
assert_eq!(res.status(), reqwest::StatusCode::OK);

Expand Down Expand Up @@ -716,12 +719,15 @@ async fn test_socks_restart() {
.build()
.unwrap();

let res = timeout_await!(client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry("0")
))
.send())
let res = timeout_await!(
client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry("0")
))
.send(),
Duration::from_secs(5)
)
.unwrap();
assert_eq!(res.status(), reqwest::StatusCode::OK);

Expand All @@ -735,12 +741,15 @@ async fn test_socks_restart() {
.build()
.unwrap();

let res = timeout_await!(client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry("0")
))
.send())
let res = timeout_await!(
client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry("0")
))
.send(),
Duration::from_secs(5)
)
.unwrap();
assert_eq!(res.status(), reqwest::StatusCode::OK);

Expand Down Expand Up @@ -803,12 +812,15 @@ async fn test_socks_multiple_server() {
.build()
.unwrap();

let res = timeout_await!(client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry(&id.to_string())
))
.send())
let res = timeout_await!(
client
.get(format!(
"http://{}/",
kxdns::Kxdns::new(DOMAIN_SUFFIX.to_string()).registry(&id.to_string())
))
.send(),
Duration::from_secs(5)
)
.unwrap();
assert_eq!(res.status(), reqwest::StatusCode::OK);

Expand Down

0 comments on commit a38b8b4

Please sign in to comment.