diff --git a/planetwars-matchrunner/tests/test_matchrunner.rs b/planetwars-matchrunner/tests/test_matchrunner.rs index 5d2fc83..5b96406 100644 --- a/planetwars-matchrunner/tests/test_matchrunner.rs +++ b/planetwars-matchrunner/tests/test_matchrunner.rs @@ -1,6 +1,8 @@ +use futures::{Future, FutureExt}; use std::collections::HashMap; use std::io::BufRead; use std::path::PathBuf; +use std::pin::Pin; use std::sync::{Arc, Mutex}; use std::time::Duration; use tokio::sync::mpsc; @@ -54,22 +56,39 @@ async fn match_does_run() { tokio::time::sleep(Duration::from_secs(1)).await } -#[tokio::test] -async fn docker_runner_timeout() { +async fn match_ctx_from_bot_spec(bot_spec: B) -> MatchCtx { 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); + 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)); +/// creates a simple match ctx which only holds a single bot +async fn with_bot_match_ctx(bot_spec: B, func: F) +where + F: FnOnce(&mut MatchCtx) -> Pin>>, + B: BotSpec, +{ + let mut ctx = match_ctx_from_bot_spec(bot_spec).await; + func(&mut ctx).await; ctx.shutdown().await; } + +#[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; +}