Test strike timeout

This commit is contained in:
Pierre HUBERT 2022-10-17 08:35:49 +02:00
parent 171c88f303
commit fcc7f30e10
6 changed files with 43 additions and 6 deletions

View File

@ -53,6 +53,11 @@ impl GameRules {
self self
} }
pub fn with_strike_timeout(mut self, timeout: u64) -> Self {
self.strike_timeout = Some(timeout);
self
}
/// Set the list of boats for this configuration /// Set the list of boats for this configuration
pub fn set_boats_list(&mut self, boats: &[usize]) { pub fn set_boats_list(&mut self, boats: &[usize]) {
self.boats_str = boats self.boats_str = boats

View File

@ -39,7 +39,7 @@ pub struct BotClient {
requested_rules: GameRules, requested_rules: GameRules,
layout: Option<BoatsLayout>, layout: Option<BoatsLayout>,
number_plays: usize, number_plays: usize,
server_msg_callback: Option<Box<dyn FnMut(&ServerMessage)>>, server_msg_callback: Option<Box<dyn FnMut(&mut ServerMessage)>>,
play_as_bot_type: BotType, play_as_bot_type: BotType,
} }
@ -81,7 +81,7 @@ impl BotClient {
pub fn with_server_msg_callback<F>(mut self, cb: F) -> Self pub fn with_server_msg_callback<F>(mut self, cb: F) -> Self
where where
F: FnMut(&ServerMessage) + 'static, F: FnMut(&mut ServerMessage) + 'static,
{ {
self.server_msg_callback = Some(Box::new(cb)); self.server_msg_callback = Some(Box::new(cb));
self self
@ -152,7 +152,7 @@ impl BotClient {
}; };
while let Some(chunk) = socket.next().await { while let Some(chunk) = socket.next().await {
let message = match chunk? { let mut message = match chunk? {
Message::Text(message) => { Message::Text(message) => {
log::trace!("TEXT message from server: {}", message); log::trace!("TEXT message from server: {}", message);
@ -182,7 +182,7 @@ impl BotClient {
}; };
if let Some(cb) = &mut self.server_msg_callback { if let Some(cb) = &mut self.server_msg_callback {
(cb)(&message) (cb)(&mut message)
} }
match message { match message {

View File

@ -9,7 +9,7 @@ use crate::test::play_utils::check_no_replay_on_hit;
use crate::test::{bot_client, TestPort}; use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port; use crate::utils::network_utils::wait_for_port;
fn check_strikes_are_linear(msg: &ServerMessage) { fn check_strikes_are_linear(msg: &mut ServerMessage) {
if let ServerMessage::RequestFire { status } = msg { if let ServerMessage::RequestFire { status } = msg {
let mut in_fire_location = true; let mut in_fire_location = true;
for y in 0..status.rules.map_height { for y in 0..status.rules.map_height {

View File

@ -1,7 +1,9 @@
use tokio::task; use tokio::task;
use crate::args::Args; use crate::args::Args;
use crate::consts::MIN_STRIKE_TIMEOUT;
use crate::data::{BoatsLayout, GameRules}; use crate::data::{BoatsLayout, GameRules};
use crate::human_player_ws::ServerMessage;
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client; use crate::test::bot_client;
use crate::test::bot_client::ClientEndResult; use crate::test::bot_client::ClientEndResult;
@ -201,3 +203,32 @@ async fn full_game_no_replay_on_hit() {
}) })
.await; .await;
} }
#[tokio::test]
async fn check_fire_time_out() {
let _ = env_logger::builder().is_test(true).try_init();
let local_set = task::LocalSet::new();
local_set
.run_until(async move {
task::spawn_local(start_server(Args::for_test(TestPort::RandomCheckTimeout)));
wait_for_port(TestPort::RandomCheckTimeout.port()).await;
let mut did_skip_one = false;
let res = bot_client::BotClient::new(TestPort::RandomCheckTimeout.as_url())
.with_rules(
GameRules::random_players_rules().with_strike_timeout(MIN_STRIKE_TIMEOUT),
)
.with_server_msg_callback(move |msg| {
if matches!(msg, ServerMessage::RequestFire { .. }) && !did_skip_one {
*msg = ServerMessage::OpponentReplacedByBot;
did_skip_one = true;
}
})
.run_client()
.await
.unwrap();
assert!(matches!(res, ClientEndResult::Finished { .. }));
})
.await;
}

View File

@ -10,6 +10,7 @@ enum TestPort {
RandomBotInvalidBoatsLayoutLenOfABoat, RandomBotInvalidBoatsLayoutLenOfABoat,
RandomBotFullGameMultipleRematch, RandomBotFullGameMultipleRematch,
RandomBotNoReplayOnHit, RandomBotNoReplayOnHit,
RandomCheckTimeout,
LinearBotFullGame, LinearBotFullGame,
LinearBotNoReplayOnHit, LinearBotNoReplayOnHit,
IntermediateBotFullGame, IntermediateBotFullGame,

View File

@ -1,7 +1,7 @@
use crate::human_player_ws::ServerMessage; use crate::human_player_ws::ServerMessage;
/// Make sure player can not replay after successful hit /// Make sure player can not replay after successful hit
pub fn check_no_replay_on_hit(msg: &ServerMessage) { pub fn check_no_replay_on_hit(msg: &mut ServerMessage) {
if let ServerMessage::OpponentMustFire { status } | ServerMessage::RequestFire { status } = msg if let ServerMessage::OpponentMustFire { status } | ServerMessage::RequestFire { status } = msg
{ {
let diff = let diff =