refactor: helpers for match setup

This commit is contained in:
Ilion Beyst 2022-09-24 22:14:36 +02:00
parent 332b3e4a13
commit 6cc629a3b7

View file

@ -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<B: BotSpec>(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<B, F>(bot_spec: B, func: F)
where
F: FnOnce(&mut MatchCtx) -> Pin<Box<dyn '_ + Future<Output = ()>>>,
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;
}