planetwars.dev/planetwars-matchrunner/tests/test_matchrunner.rs

151 lines
4.5 KiB
Rust
Raw Normal View History

2022-09-24 22:14:36 +02:00
use futures::{Future, FutureExt};
2022-09-22 19:38:09 +02:00
use std::collections::HashMap;
2022-09-20 21:11:19 +02:00
use std::io::BufRead;
use std::path::PathBuf;
2022-09-24 22:14:36 +02:00
use std::pin::Pin;
2022-09-22 19:38:09 +02:00
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::mpsc;
2022-09-20 21:11:19 +02:00
2022-09-22 19:38:09 +02:00
use planetwars_matchrunner::docker_runner::DockerBotSpec;
use planetwars_matchrunner::match_context::{EventBus, MatchCtx, RequestError};
use planetwars_matchrunner::BotSpec;
use planetwars_matchrunner::{run_match, MatchConfig, MatchPlayer};
2022-09-20 21:11:19 +02:00
const PYTHON_IMAGE: &str = "python:3.10-slim-buster";
2022-09-22 19:38:09 +02:00
fn simple_python_docker_bot_spec(source_dir: &str, file_name: &str) -> DockerBotSpec {
let source_dir_path = std::fs::canonicalize(source_dir).unwrap();
let source_dir_path_str = source_dir_path.as_os_str().to_str().unwrap();
2022-09-20 21:11:19 +02:00
2022-09-22 19:38:09 +02:00
DockerBotSpec {
2022-09-20 21:11:19 +02:00
image: PYTHON_IMAGE.to_string(),
2022-09-22 19:38:09 +02:00
binds: Some(vec![format!("{}:{}", source_dir_path_str, "/workdir")]),
argv: Some(vec!["python".to_string(), file_name.to_string()]),
2022-09-20 21:11:19 +02:00
working_dir: Some("/workdir".to_string()),
pull: false,
credentials: None,
2022-09-22 19:38:09 +02:00
}
}
#[tokio::test]
async fn match_does_run() {
let bot = simple_python_docker_bot_spec("./bots/simplebot", "simplebot.py");
let log_file = tempfile::NamedTempFile::new().unwrap();
2022-09-20 21:11:19 +02:00
let config = MatchConfig {
2022-09-27 07:14:47 +02:00
map_name: "abc".to_string(),
2022-09-20 21:11:19 +02:00
map_path: PathBuf::from("maps/abc.json"),
log_path: PathBuf::from(log_file.path()),
players: vec![
MatchPlayer {
bot_spec: Box::new(bot.clone()),
},
MatchPlayer {
bot_spec: Box::new(bot.clone()),
},
],
};
run_match(config).await;
let line_count = std::io::BufReader::new(log_file.as_file()).lines().count();
assert!(line_count > 0);
2022-09-27 07:14:47 +02:00
}
#[tokio::test]
async fn player_results() {
let log_file = tempfile::NamedTempFile::new().unwrap();
let config = MatchConfig {
map_name: "abc".to_string(),
map_path: PathBuf::from("maps/abc.json"),
log_path: PathBuf::from(log_file.path()),
players: vec![
MatchPlayer {
bot_spec: Box::new(simple_python_docker_bot_spec(
"./bots/simplebot",
"simplebot.py",
)),
},
MatchPlayer {
bot_spec: Box::new(simple_python_docker_bot_spec("./bots", "crash_bot.py")),
},
],
};
2022-09-22 19:38:09 +02:00
2022-09-27 07:14:47 +02:00
let outcome = run_match(config).await;
assert_eq!(outcome.player_outcomes.len(), 2);
assert!(!outcome.player_outcomes[0].crashed);
assert!(!outcome.player_outcomes[0].had_errors);
assert!(outcome.player_outcomes[1].crashed);
assert!(!outcome.player_outcomes[1].had_errors);
2022-09-22 19:38:09 +02:00
}
2022-09-24 23:37:23 +02:00
/// creates a simple match ctx which only holds a single bot
async fn with_bot_match_ctx<B, F>(bot_spec: B, func: F)
where
F: FnOnce(&mut MatchCtx) -> Pin<Box<dyn '_ + Future<Output = ()>>>,
B: BotSpec,
{
2022-09-22 19:38:09 +02:00
let event_bus = Arc::new(Mutex::new(EventBus::new()));
let (logger, _rx) = mpsc::unbounded_channel();
let player_handle = bot_spec.run_bot(1, event_bus.clone(), logger.clone()).await;
let mut players = HashMap::new();
players.insert(1, player_handle);
2022-09-24 23:37:23 +02:00
let mut ctx = MatchCtx::new(event_bus, players, logger);
2022-09-22 19:38:09 +02:00
2022-09-24 22:14:36 +02:00
func(&mut ctx).await;
ctx.shutdown().await;
2022-09-20 21:11:19 +02:00
}
2022-09-24 22:14:36 +02:00
2022-09-24 22:21:38 +02:00
#[tokio::test]
async fn docker_runner_success() {
let bot_spec = simple_python_docker_bot_spec("./bots", "echo_bot.py");
with_bot_match_ctx(bot_spec, |ctx| {
async move {
let resp = ctx
.request(1, b"sup".to_vec(), Duration::from_millis(200))
.await;
assert_eq!(resp, Ok(b"sup\n".to_vec()));
}
.boxed()
})
.await;
}
2022-09-24 22:14:36 +02:00
#[tokio::test]
async fn docker_runner_timeout() {
let bot_spec = simple_python_docker_bot_spec("./bots", "timeout_bot.py");
with_bot_match_ctx(bot_spec, |ctx| {
async move {
let resp = ctx
.request(1, b"sup".to_vec(), Duration::from_millis(200))
.await;
assert_eq!(resp, Err(RequestError::Timeout));
}
.boxed()
})
.await;
}
2022-09-24 23:37:23 +02:00
#[tokio::test]
async fn docker_runner_crash() {
let bot_spec = simple_python_docker_bot_spec("./bots", "crash_bot.py");
with_bot_match_ctx(bot_spec, |ctx| {
async move {
let resp = ctx
.request(1, b"sup".to_vec(), Duration::from_millis(200))
.await;
assert_eq!(resp, Err(RequestError::BotTerminated));
}
.boxed()
})
.await;
}