run regression test with local docker image instead of actual server

This commit is contained in:
Max Känner 2024-10-03 02:15:57 +02:00
parent accfe7acbb
commit c2d242453e

View File

@ -144,10 +144,18 @@ fn vs_production() -> Result<(), DynError> {
fn regression() -> Result<(), DynError> {
let mut snake = run_snake(8000, Some("error"))?;
let mut prod = match run_production(8001) {
Ok(prod) => prod,
err => {
snake.kill()?;
err?;
unreachable!()
}
};
let res = try_regression();
snake.kill()?;
snake.kill().and(prod.kill())?;
let (won, games) = res?;
println!(
"\nThe local snake has won {won}/{games} games ({}%)",
@ -157,10 +165,10 @@ fn regression() -> Result<(), DynError> {
}
fn try_regression() -> Result<(usize, usize), DynError> {
const GAMES: usize = 1000;
// use only 4 threads so we don't ddos the server
const GAMES: usize = 100;
// limit the parallelism
rayon::ThreadPoolBuilder::new()
.num_threads(4)
.num_threads(std::thread::available_parallelism()?.get() / 3)
.build_global()
.unwrap();
@ -168,7 +176,6 @@ fn try_regression() -> Result<(usize, usize), DynError> {
.into_par_iter()
.map(|_| -> Option<usize> {
eprint!(".");
// let _ = stderr().flush();
let game = Command::new("./battlesnake-cli")
.current_dir(project_root())
.args([
@ -184,7 +191,7 @@ fn try_regression() -> Result<(usize, usize), DynError> {
"--name",
"production",
"--url",
"https://snake.mkaenner.de",
"http://localhost:8001",
"-g",
"duel",
])
@ -235,6 +242,30 @@ fn run_snake(port: u16, log: Option<&str>) -> Result<Child, DynError> {
}
}
fn run_production(port: u16) -> Result<Child, DynError> {
let mut snake = Command::new("docker")
.args([
"run",
"--env",
"RUST_LOG=error",
"--env",
format!("ROCKET_PORT={}", port).as_str(),
"--network=host",
"-i",
"docker.mkaenner.de/snake:latest",
])
.spawn()?;
loop {
if let Some(status) = snake.try_wait()? {
Err(format!("{status}"))?;
}
if TcpStream::connect(("127.0.0.1", port)).is_ok() {
break Ok(snake);
}
sleep(Duration::from_secs(1));
}
}
fn docker() -> Result<(), DynError> {
if !Command::new("docker")
.current_dir(project_root())