Skip to content

Commit

Permalink
fix: Improve tests to swap scripts for p1 and p2
Browse files Browse the repository at this point in the history
  • Loading branch information
hampfh committed Jul 14, 2024
1 parent fb16355 commit ec26e2a
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 137 deletions.
2 changes: 2 additions & 0 deletions apps/server/src/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Game {
program1,
PlayerType::Flipped,
false,
self.config.bot_initialization_timeout,
) {
Ok(_) => (),
Err(err) => {
Expand All @@ -61,6 +62,7 @@ impl Game {
program2,
PlayerType::Regular,
false,
self.config.bot_initialization_timeout,
) {
Ok(_) => (),
Err(err) => {
Expand Down
10 changes: 8 additions & 2 deletions apps/server/src/game/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ impl Game {
std: String,
config: GameConfig,
) -> Game {
let p1_lua = Lua::new();
let p2_lua = Lua::new();

p1_lua.sandbox(true).unwrap();
p2_lua.sandbox(true).unwrap();

return Game {
config,
logger: Vec::new(),
Expand All @@ -98,8 +104,8 @@ impl Game {
player_one,
player_two,
walls,
player_one_sandbox: Arc::new(Mutex::new(Lua::new())),
player_two_sandbox: Arc::new(Mutex::new(Lua::new())),
player_one_sandbox: Arc::new(Mutex::new(p1_lua)),
player_two_sandbox: Arc::new(Mutex::new(p2_lua)),
player_one_turn: true,
last_move: None,
std,
Expand Down
11 changes: 9 additions & 2 deletions apps/server/src/game/sandbox/sandbox_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) fn execute_lua_in_sandbox(
lua_script_to_run: String,
active_player_type: PlayerType,
expect_return: bool,
timeout: Duration,
) -> Result<String, ErrorType> {
let (tx, rx) = std::sync::mpsc::channel::<Result<String, mlua::Error>>();
// Sandbox execution of script
Expand All @@ -42,7 +43,7 @@ pub(crate) fn execute_lua_in_sandbox(
});

// Second time we either get the result or a timeout error
let player_move = match rx.recv_timeout(Duration::from_millis(500)) {
let player_move = match rx.recv_timeout(timeout) {
Ok(returned) => match returned {
Ok(move_string) => move_string,
Err(error) => {
Expand Down Expand Up @@ -133,6 +134,8 @@ pub(crate) fn assert_lua_core_functions(

#[cfg(test)]
mod tests {
use std::time::Duration;

use crate::game::{
game_state::{ErrorType, Game, GameConfig},
player::PlayerType,
Expand Down Expand Up @@ -174,6 +177,7 @@ mod tests {
.to_string(),
PlayerType::Flipped,
false,
Duration::from_millis(500),
)
.unwrap();

Expand All @@ -183,7 +187,8 @@ mod tests {
game.get_active_sandbox(),
inject.clone(),
game.get_active_player_type(),
true
true,
Duration::from_millis(500)
)
.is_ok());

Expand All @@ -203,6 +208,7 @@ mod tests {
.to_string(),
PlayerType::Flipped,
false,
Duration::from_millis(500),
)
.unwrap();

Expand All @@ -211,6 +217,7 @@ mod tests {
inject,
game.get_active_player_type(),
true,
Duration::from_millis(500),
) {
Err(ErrorType::TurnTimeout {
fault: Some(PlayerType::Flipped),
Expand Down
Loading

0 comments on commit ec26e2a

Please sign in to comment.