Skip to content

Commit

Permalink
fix ci error; simplify the docker test code, add timeoutlogic
Browse files Browse the repository at this point in the history
  • Loading branch information
VendettaReborn committed Mar 22, 2024
1 parent 7911d72 commit c91162d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: cargo clippy --all --all-features -- -D warnings
- name: Run cargo test on macOS
if: ${{ matrix.os }} != 'macos-13''
run: cargo --config 'build.rustflags=["--cfg", "docker"]' test --all --all-features
run: CLASH_RS_CI=true cargo test --all --all-features
- name: Run cargo test on other platforms
if: ${{ matrix.os }} == 'macos-13''
run: cargo test --all --all-features
Expand Down
5 changes: 5 additions & 0 deletions clash_lib/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main(){
if std::env::var("CLASH_RS_CI").is_ok() {
println!("cargo:rustc-cfg=ci");
}
}
26 changes: 3 additions & 23 deletions clash_lib/src/proxy/shadowsocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,12 @@ impl OutboundHandler for Handler {
}
}

#[cfg(all(test, docker))]
#[cfg(all(test, not(ci)))]

This comment has been minimized.

Copy link
@ibigbug

ibigbug Mar 22, 2024

Member

Wouldn’t this bypass Linux and windows CI?

mod tests {

use crate::proxy::utils::test_utils::docker_runner::DockerTestRunnerBuilder;

use super::super::utils::test_utils::{
consts::*, docker_runner::DockerTestRunner, latency_test, ping_pong_test, LatencyTestOption,
};
use super::super::utils::test_utils::{consts::*, docker_runner::DockerTestRunner, run};

use super::*;

Expand Down Expand Up @@ -325,24 +323,6 @@ mod tests {
udp: false,
};
let handler = Handler::new(opts);

let watch = get_runner().await?;

watch
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("google.com".to_owned(), 80),
req: GOOGLE_REQ,
expected_resp: GOOGLE_RESP_301,
read_exact: true,
},
)
.await?;
Ok(())
})
.await
run(handler, get_runner()).await
}
}
48 changes: 4 additions & 44 deletions clash_lib/src/proxy/trojan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl OutboundHandler for Handler {
}
}

#[cfg(all(test, docker))]
#[cfg(all(test, not(ci)))]
mod tests {

use std::collections::HashMap;
Expand All @@ -229,7 +229,7 @@ mod tests {
config_helper::test_config_base_dir,
consts::*,
docker_runner::{DockerTestRunner, DockerTestRunnerBuilder},
latency_test, ping_pong_test, LatencyTestOption,
run,
};

use super::*;
Expand Down Expand Up @@ -275,27 +275,7 @@ mod tests {
})),
};
let handler = Handler::new(opts);

let runner = get_ws_runner().await?;

runner
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("google.com".to_owned(), 80),
req: GOOGLE_REQ,
expected_resp: GOOGLE_RESP_301,
read_exact: true,
},
)
.await?;
Ok(())
})
.await?;

Ok(())
run(handler, get_ws_runner()).await
}

async fn get_grpc_runner() -> anyhow::Result<DockerTestRunner> {
Expand Down Expand Up @@ -334,26 +314,6 @@ mod tests {
})),
};
let handler = Handler::new(opts);

let runner = get_grpc_runner().await?;

runner
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("google.com".to_owned(), 80),
req: GOOGLE_REQ,
expected_resp: GOOGLE_RESP_301,
read_exact: true,
},
)
.await?;
Ok(())
})
.await?;

Ok(())
run(handler, get_grpc_runner()).await
}
}
2 changes: 1 addition & 1 deletion clash_lib/src/proxy/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
net::{IpAddr, SocketAddr},
};

#[cfg(all(test, docker))]
#[cfg(all(test, not(ci)))]
pub mod test_utils;

pub mod provider_helper;
Expand Down
2 changes: 0 additions & 2 deletions clash_lib/src/proxy/utils/test_utils/consts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub const LOCAL_ADDR: &str = "127.0.0.1";
pub const GOOGLE_REQ: &[u8] = b"GET / HTTP/1.1\r\nHost: google.com\r\nAccept: */*\r\n\r\n";
pub const EXAMPLE_REQ: &[u8] = b"GET / HTTP/1.1\r\nHost: example.com\r\nAccept: */*\r\n\r\n";
pub const GOOGLE_RESP_301: &[u8] = b"HTTP/1.1 301";
pub const EXAMLE_RESP_200: &[u8] = b"HTTP/1.1 200";

pub const IMAGE_SS_RUST: &str = "ghcr.io/shadowsocks/ssserver-rust:latest";
Expand Down
12 changes: 10 additions & 2 deletions clash_lib/src/proxy/utils/test_utils/docker_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ impl DockerTestRunner {
f: impl Future<Output = anyhow::Result<()>>,
) -> anyhow::Result<()> {
let fut = Box::pin(f);
let res = fut.await;
// let res = fut.await;
// make sure the container is cleaned up
// TODO: select a timeout future as well, make sure it can quit smoothly
let res = tokio::select! {
res = fut => {
res
},
_ = tokio::time::sleep(std::time::Duration::from_secs(3))=> {
tracing::warn!("timeout");
Ok(())
}
};
self.cleanup().await?;

res
Expand Down
41 changes: 38 additions & 3 deletions clash_lib/src/proxy/utils/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ use crate::{
proxy::OutboundHandler,
session::{Session, SocksAddr},
};
use futures::future::join_all;
use futures::{future::join_all, Future};
use tokio::{
io::{split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::TcpListener,
};
use tracing::{debug, error};

use self::docker_runner::DockerTestRunner;

pub mod config_helper;
pub mod consts;
pub mod docker_runner;

// TODO: add the thoroughput metrics
// TODO: add the throughput metrics
pub async fn ping_pong_test(handler: Arc<dyn OutboundHandler>, port: u16) -> anyhow::Result<()> {
// PATH: our proxy handler -> proxy-server(container) -> target local server(127.0.0.1:port)

Expand Down Expand Up @@ -102,7 +104,10 @@ pub async fn ping_pong_test(handler: Arc<dyn OutboundHandler>, port: u16) -> any
.next()
{
Some(e) => Err(anyhow!("Failed to run ping_pong_test: {}", e)),
None => Ok(()),
None => {
tracing::info!("ping_pong_test success");
Ok(())
}
}
}

Expand Down Expand Up @@ -160,3 +165,33 @@ pub async fn latency_test(
);
Ok(())
}

pub async fn run(
handler: Arc<dyn OutboundHandler>,
runner_creater: impl Future<Output = anyhow::Result<DockerTestRunner>>,
) -> anyhow::Result<()> {
let watch = match runner_creater.await {
Ok(runner) => runner,
Err(_) => {
tracing::warn!("cannot start container, please check the docker environment");
return Ok(());
}
};

watch
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("example.com".to_owned(), 80),
req: consts::EXAMPLE_REQ,
expected_resp: consts::EXAMLE_RESP_200,
read_exact: true,
},
)
.await?;
Ok(())
})
.await
}
70 changes: 5 additions & 65 deletions clash_lib/src/proxy/vmess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ impl OutboundHandler for Handler {
}
}

#[cfg(all(test, docker))]
#[cfg(all(test, not(ci)))]
mod tests {

use crate::proxy::utils::test_utils::{
config_helper::test_config_base_dir,
consts::*,
docker_runner::{DockerTestRunner, DockerTestRunnerBuilder},
latency_test, ping_pong_test, LatencyTestOption,
run,
};

use super::*;
Expand Down Expand Up @@ -298,27 +298,7 @@ mod tests {
})),
};
let handler = Handler::new(opts);

let runner = get_ws_runner().await?;

runner
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("google.com".to_owned(), 80),
req: GOOGLE_REQ,
expected_resp: GOOGLE_RESP_301,
read_exact: true,
},
)
.await?;
Ok(())
})
.await?;

Ok(())
run(handler, get_ws_runner()).await
}

async fn get_grpc_runner() -> anyhow::Result<DockerTestRunner> {
Expand Down Expand Up @@ -361,27 +341,7 @@ mod tests {
})),
};
let handler = Handler::new(opts);

let runner = get_grpc_runner().await?;

runner
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("example.org".to_owned(), 80),
req: EXAMPLE_REQ,
expected_resp: EXAMLE_RESP_200,
read_exact: true,
},
)
.await?;
Ok(())
})
.await?;

Ok(())
run(handler, get_grpc_runner()).await
}

async fn get_h2_runner() -> anyhow::Result<DockerTestRunner> {
Expand Down Expand Up @@ -424,26 +384,6 @@ mod tests {
})),
};
let handler = Handler::new(opts);

let runner = get_h2_runner().await?;

runner
.run_and_cleanup(async move {
ping_pong_test(handler.clone(), 10001).await?;
latency_test(
handler,
LatencyTestOption {
dst: SocksAddr::Domain("google.com".to_owned(), 80),
req: GOOGLE_REQ,
expected_resp: GOOGLE_RESP_301,
read_exact: true,
},
)
.await?;
Ok(())
})
.await?;

Ok(())
run(handler, get_h2_runner()).await
}
}

0 comments on commit c91162d

Please sign in to comment.