From 6cc629a3b7576d454589ffb38f49869d30926d02 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sat, 24 Sep 2022 22:14:36 +0200 Subject: refactor: helpers for match setup --- planetwars-matchrunner/tests/test_matchrunner.rs | 39 ++++++++++++++++++------ 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'planetwars-matchrunner/tests/test_matchrunner.rs') 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); - - let resp = ctx - .request(1, b"sup".to_vec(), Duration::from_millis(1000)) - .await; + MatchCtx::new(event_bus, players, logger) +} - 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; +} -- cgit v1.2.3