aboutsummaryrefslogtreecommitdiff
path: root/planetwars-matchrunner
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-09-22 19:38:09 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-09-22 19:38:09 +0200
commit8f3621813e44df2dace49a6400cfe870bc3778ea (patch)
tree48558bae0fc4d1c7700e3a88787b8e3728835776 /planetwars-matchrunner
parent9643ccf6d69cc34f56fff454f1c0aa3e34a93d08 (diff)
downloadplanetwars.dev-8f3621813e44df2dace49a6400cfe870bc3778ea.tar.xz
planetwars.dev-8f3621813e44df2dace49a6400cfe870bc3778ea.zip
test docker runner timeouts
Diffstat (limited to 'planetwars-matchrunner')
-rw-r--r--planetwars-matchrunner/bots/timeout_bot.py4
-rw-r--r--planetwars-matchrunner/src/lib.rs2
-rw-r--r--planetwars-matchrunner/src/match_context.rs2
-rw-r--r--planetwars-matchrunner/tests/test_matchrunner.rs55
4 files changed, 50 insertions, 13 deletions
diff --git a/planetwars-matchrunner/bots/timeout_bot.py b/planetwars-matchrunner/bots/timeout_bot.py
new file mode 100644
index 0000000..a8364ca
--- /dev/null
+++ b/planetwars-matchrunner/bots/timeout_bot.py
@@ -0,0 +1,4 @@
+import time
+
+# I'm going to take my sweet time!
+time.sleep(1000) \ No newline at end of file
diff --git a/planetwars-matchrunner/src/lib.rs b/planetwars-matchrunner/src/lib.rs
index fcd4799..d26e810 100644
--- a/planetwars-matchrunner/src/lib.rs
+++ b/planetwars-matchrunner/src/lib.rs
@@ -16,7 +16,7 @@ use match_log::{create_log_sink, MatchLogger};
use planetwars_rules::PwConfig;
use serde::{Deserialize, Serialize};
-use self::match_context::{EventBus, PlayerHandle};
+pub use self::match_context::{EventBus, PlayerHandle};
pub struct MatchConfig {
pub map_name: String,
diff --git a/planetwars-matchrunner/src/match_context.rs b/planetwars-matchrunner/src/match_context.rs
index 1dac09b..859b11d 100644
--- a/planetwars-matchrunner/src/match_context.rs
+++ b/planetwars-matchrunner/src/match_context.rs
@@ -150,7 +150,7 @@ impl Future for Request {
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RequestError {
Timeout,
}
diff --git a/planetwars-matchrunner/tests/test_matchrunner.rs b/planetwars-matchrunner/tests/test_matchrunner.rs
index 0da1836..5ac8649 100644
--- a/planetwars-matchrunner/tests/test_matchrunner.rs
+++ b/planetwars-matchrunner/tests/test_matchrunner.rs
@@ -1,25 +1,37 @@
+use std::collections::HashMap;
use std::io::BufRead;
use std::path::PathBuf;
+use std::sync::{Arc, Mutex};
+use std::time::Duration;
+use tokio::sync::mpsc;
-use planetwars_matchrunner::{docker_runner::DockerBotSpec, run_match, MatchConfig, MatchPlayer};
+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};
const PYTHON_IMAGE: &str = "python:3.10-slim-buster";
-#[tokio::test]
-async fn match_does_run() {
- let simplebot_path = std::fs::canonicalize("bots/simplebot").unwrap();
- let simplebot_path_str = simplebot_path.as_os_str().to_str().unwrap();
+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();
- let log_file = tempfile::NamedTempFile::new().unwrap();
-
- let bot = DockerBotSpec {
+ DockerBotSpec {
image: PYTHON_IMAGE.to_string(),
- binds: Some(vec![format!("{}:{}", simplebot_path_str, "/workdir")]),
- argv: Some(vec!["python".to_string(), "simplebot.py".to_string()]),
+ binds: Some(vec![format!("{}:{}", source_dir_path_str, "/workdir")]),
+ argv: Some(vec!["python".to_string(), file_name.to_string()]),
working_dir: Some("/workdir".to_string()),
pull: false,
credentials: None,
- };
+ }
+}
+
+#[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();
+
let config = MatchConfig {
map_name: "hex".to_string(),
map_path: PathBuf::from("maps/abc.json"),
@@ -38,4 +50,25 @@ async fn match_does_run() {
let line_count = std::io::BufReader::new(log_file.as_file()).lines().count();
assert!(line_count > 0);
+
+ tokio::time::sleep(Duration::from_secs(1)).await
+}
+
+#[tokio::test]
+async fn docker_runner_timeout() {
+ let event_bus = Arc::new(Mutex::new(EventBus::new()));
+ let (logger, _rx) = mpsc::unbounded_channel();
+ let bot_spec = simple_python_docker_bot_spec("./bots", "timeout_bot.py");
+
+ let player_handle = bot_spec.run_bot(1, event_bus.clone(), logger.clone()).await;
+
+ let mut players = HashMap::new();
+ players.insert(1, player_handle);
+ let mut ctx = MatchCtx::new(event_bus, players, logger);
+
+ let resp = ctx
+ .request(1, b"sup".to_vec(), Duration::from_millis(1000))
+ .await;
+
+ assert_eq!(resp, Err(RequestError::Timeout))
}