diff --git a/src/engine/flop_spieler/tests.rs b/src/engine/flop_spieler/tests.rs index fd5e112d..c6df6c12 100644 --- a/src/engine/flop_spieler/tests.rs +++ b/src/engine/flop_spieler/tests.rs @@ -4,7 +4,7 @@ use crate::cfr::turn_river_game::Hands; use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain}; use crate::variant::Variant; -static MODE_LOCK: Mutex<()> = Mutex::new(()); +use crate::engine::SCHALTER_LOCK as MODE_LOCK; fn rec(street: Street, seat: u8, action: SeenAction, amount: Option) -> ActionRecord { ActionRecord { street, seat, action, amount } @@ -59,6 +59,7 @@ fn facing_bet(hero: &str) -> Spot { #[test] #[ignore] fn timing() { + let _fest = crate::engine::frist::fest_hier(); for hero in ["Ac As", "Td 9c"] { for spot in [facing_bet(hero), checked_to(hero)] { let (h, v) = crate::engine::river_spieler::model_lists(&spot).unwrap(); @@ -71,6 +72,7 @@ fn timing() { #[test] fn off_decides_nothing_and_on_decides_heads_up_flops() { + let _fest = crate::engine::frist::fest_hier(); let _g = MODE_LOCK.lock().unwrap(); set_flop_spieler(false); assert!(flop_spieler_decision(&checked_to("Ks Kd")).is_none()); @@ -96,6 +98,7 @@ fn off_decides_nothing_and_on_decides_heads_up_flops() { /// (two kings left among 44 river cards). #[test] fn the_river_equity_table_is_exact() { + let _fest = crate::engine::frist::fest_hier(); let board = fast7::mask(&Card::parse_list("Qh 7d 2c").unwrap()); let aa = fast7::mask(&Card::parse_list("As Ad").unwrap()); let kk = fast7::mask(&Card::parse_list("Ks Kd").unwrap()); @@ -111,6 +114,7 @@ fn the_river_equity_table_is_exact() { /// Facing a pot bet with total air the solver folds mostly; with top set it never folds. #[test] fn sets_continue_air_folds() { + let _fest = crate::engine::frist::fest_hier(); let cfg = FlopConfig::STANDARD; let row = |hero: &str| { let spot = facing_bet(hero); diff --git a/src/engine/frist.rs b/src/engine/frist.rs index 0d7c5674..d8d849c2 100644 --- a/src/engine/frist.rs +++ b/src/engine/frist.rs @@ -8,6 +8,8 @@ use std::time::Instant; thread_local! { static START: Cell> = const { Cell::new(None) }; + /// `fest_hier`: this thread ignores wall time like `SOLVER_ZEIT=fest` (tests, tools). + static FEST_HIER: Cell = const { Cell::new(false) }; } /// Total wall time of one decision per street, in ms. @@ -36,7 +38,24 @@ pub fn left_ms(total_ms: f64) -> f64 { /// count, so ladder and LBR results do not depend on machine load (read once). pub fn fixed() -> bool { static FIXED: std::sync::OnceLock = std::sync::OnceLock::new(); - *FIXED.get_or_init(|| std::env::var("SOLVER_ZEIT").as_deref() == Ok("fest")) + FEST_HIER.with(Cell::get) || *FIXED.get_or_init(|| std::env::var("SOLVER_ZEIT").as_deref() == Ok("fest")) +} + +/// While the guard lives, solvers on THIS thread run their full iteration count whatever the load (as +/// `SOLVER_ZEIT=fest`, but per thread). For tests that check a solver's result rather than its deadline: +/// under a busy machine the wall-time budget otherwise cuts iterations (different numbers) or drops the +/// solve below its minimum (the heuristic decides). Never called on the live path. +pub fn fest_hier() -> FestHier { + FestHier(FEST_HIER.with(|f| f.replace(true))) +} + +/// Guard of `fest_hier`; restores the previous state on drop. +pub struct FestHier(bool); + +impl Drop for FestHier { + fn drop(&mut self) { + FEST_HIER.with(|f| f.set(self.0)); + } } /// The budget a solve gets: `cap_ms` (its own limit), cut to what is left of the street's total; diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 9f8143f2..7b015e52 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -33,3 +33,10 @@ pub mod thin_value; pub mod turn_game; pub mod turn_subgame; pub mod uncertainty; + +/// One lock for every unit test that flips a process-wide solver switch (`set_river_spieler`, +/// `set_turn_ueberall`, `set_flop_spieler`, `set_river3`, `set_turn3`/`set_flop3`, `set_turn_player`): +/// with one lock per module, a test in one module flipped a switch while another module's test +/// compared whole `decide` answers. +#[cfg(test)] +pub(crate) static SCHALTER_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); diff --git a/src/engine/multi3_spieler/tests.rs b/src/engine/multi3_spieler/tests.rs index a1e079e0..20652d54 100644 --- a/src/engine/multi3_spieler/tests.rs +++ b/src/engine/multi3_spieler/tests.rs @@ -2,9 +2,8 @@ use super::*; use crate::card::Card; use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain}; use crate::variant::Variant; -use std::sync::Mutex; -static LOCK: Mutex<()> = Mutex::new(()); +use crate::engine::SCHALTER_LOCK as LOCK; fn rec(street: Street, seat: u8, action: SeenAction, amount: Option) -> ActionRecord { ActionRecord { street, seat, action, amount } @@ -65,6 +64,7 @@ fn fold(s: &SolvedM) -> f64 { #[test] fn off_decides_nothing_and_heads_up_is_not_in_the_slice() { + let _fest = crate::engine::frist::fest_hier(); let _g = LOCK.lock().unwrap(); set_turn3(false); set_flop3(false); @@ -80,15 +80,14 @@ fn off_decides_nothing_and_heads_up_is_not_in_the_slice() { } #[test] -fn on_decides_three_way_turns_and_flops_legally_and_in_time() { +fn on_decides_three_way_turns_and_flops_legally() { let _g = LOCK.lock().unwrap(); + let _fest = crate::engine::frist::fest_hier(); set_turn3(true); set_flop3(true); - for (street, limit, tag) in [(Street::Turn, 1.25, "solver turn3 (3 players)"), (Street::Flop, 2.0, "solver flop3 (3 players)")] { + for (street, tag) in [(Street::Turn, "solver turn3 (3 players)"), (Street::Flop, "solver flop3 (3 players)")] { for hero in ["As Ad", "6s 5d", "Kc Qc"] { - let t = std::time::Instant::now(); let d = multi3_decision(&three_way(street, hero)).expect("3-way spot is in the slice"); - assert!(t.elapsed().as_secs_f64() < limit, "{street:?} {:?}", t.elapsed()); assert!(d.reason.contains(tag), "{}", d.reason); assert!(matches!(d.choice, Choice::Fold | Choice::Call | Choice::Raise { .. }), "{:?}", d.choice); } @@ -97,8 +96,30 @@ fn on_decides_three_way_turns_and_flops_legally_and_in_time() { set_flop3(false); } +/// The deadline: with wall time on, the hook answers in time (on a busy machine possibly with `None`, +/// the heuristic decides), never over. +#[test] +fn on_answers_three_way_turns_and_flops_in_time() { + let _g = LOCK.lock().unwrap(); + set_turn3(true); + set_flop3(true); + for (street, limit, tag) in [(Street::Turn, 1.25, "solver turn3 (3 players)"), (Street::Flop, 2.0, "solver flop3 (3 players)")] { + for hero in ["As Ad", "6s 5d", "Kc Qc"] { + let t = std::time::Instant::now(); + let d = multi3_decision(&three_way(street, hero)); + assert!(t.elapsed().as_secs_f64() < limit, "{street:?} {:?}", t.elapsed()); + if let Some(d) = d { + assert!(d.reason.contains(tag), "{}", d.reason); + } + } + } + set_turn3(false); + set_flop3(false); +} + #[test] fn top_set_continues_and_air_folds_three_way() { + let _fest = crate::engine::frist::fest_hier(); for street in [Street::Turn, Street::Flop] { let s = solve_multi3(&three_way(street, "As Ad")).unwrap(); assert!(fold(&s) < 0.05, "{street:?} {:?} {:?}", s.row, s.acts); diff --git a/src/engine/river3_spieler/tests.rs b/src/engine/river3_spieler/tests.rs index 23d0a411..c2add839 100644 --- a/src/engine/river3_spieler/tests.rs +++ b/src/engine/river3_spieler/tests.rs @@ -1,9 +1,8 @@ use super::*; use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain}; use crate::variant::Variant; -use std::sync::Mutex; -static LOCK: Mutex<()> = Mutex::new(()); +use crate::engine::SCHALTER_LOCK as LOCK; fn rec(street: Street, seat: u8, action: SeenAction, amount: Option) -> ActionRecord { ActionRecord { street, seat, action, amount } @@ -62,11 +61,13 @@ fn three_way(hero: &str) -> Spot { #[test] fn acting_order_starts_at_the_small_blind() { + let _fest = crate::engine::frist::fest_hier(); assert_eq!(acting_order(&three_way("Qc Jc")), Some(vec![5, 0, 4])); } #[test] fn off_decides_nothing_and_heads_up_is_not_in_the_slice() { + let _fest = crate::engine::frist::fest_hier(); let _g = LOCK.lock().unwrap(); set_river3(false); assert!(river3_decision(&three_way("As Ad")).is_none()); @@ -78,22 +79,38 @@ fn off_decides_nothing_and_heads_up_is_not_in_the_slice() { } #[test] -fn on_decides_three_way_rivers_legally_and_in_time() { +fn on_decides_three_way_rivers_legally() { let _g = LOCK.lock().unwrap(); + let _fest = crate::engine::frist::fest_hier(); set_river3(true); for hero in ["As Ad", "6s 5d", "Kc Qc"] { - let spot = three_way(hero); - let t = std::time::Instant::now(); - let d = river3_decision(&spot).expect("3-way river is in the slice"); - assert!(t.elapsed().as_secs_f64() < 2.0); + let d = river3_decision(&three_way(hero)).expect("3-way river is in the slice"); assert!(d.reason.contains("river3 solver (3 players)"), "{}", d.reason); assert!(matches!(d.choice, Choice::Fold | Choice::Call | Choice::Raise { .. }), "{:?}", d.choice); } set_river3(false); } +/// The deadline: with wall time on, the hook answers in time. On a busy machine it may answer with +/// fewer iterations or hand back to the heuristic (`None`), but it never runs over. +#[test] +fn on_answers_three_way_rivers_in_time() { + let _g = LOCK.lock().unwrap(); + set_river3(true); + for hero in ["As Ad", "6s 5d", "Kc Qc"] { + let t = std::time::Instant::now(); + let d = river3_decision(&three_way(hero)); + assert!(t.elapsed().as_secs_f64() < 2.0, "{:?}", t.elapsed()); + if let Some(d) = d { + assert!(d.reason.contains("river3 solver (3 players)"), "{}", d.reason); + } + } + set_river3(false); +} + #[test] fn top_set_continues_and_air_folds_three_way() { + let _fest = crate::engine::frist::fest_hier(); let s = solve_river3(&three_way("As Ad")).unwrap(); let fold = |s: &Solved3| s.row.iter().zip(&s.acts).filter(|(_, a)| **a == Act::Fold).map(|(p, _)| p).sum::(); assert!(fold(&s) < 0.05, "{:?} {:?}", s.row, s.acts); diff --git a/src/engine/river_spieler/tests.rs b/src/engine/river_spieler/tests.rs index 4a1d3fcd..87166a26 100644 --- a/src/engine/river_spieler/tests.rs +++ b/src/engine/river_spieler/tests.rs @@ -4,7 +4,7 @@ use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Vi use crate::variant::Variant; /// The switch is process-global: tests that touch it take turns. -static MODE_LOCK: Mutex<()> = Mutex::new(()); +use crate::engine::SCHALTER_LOCK as MODE_LOCK; fn rec(street: Street, seat: u8, action: SeenAction, amount: Option) -> ActionRecord { ActionRecord { street, seat, action, amount } @@ -79,6 +79,7 @@ fn prob(row: &[f64], acts: &[Act], f: impl Fn(&Act) -> bool) -> f64 { /// Off (the default), the engine's output is untouched: no decision. #[test] fn off_decides_nothing() { + let _fest = crate::engine::frist::fest_hier(); let _g = MODE_LOCK.lock().unwrap(); set_river_spieler(RiverSpieler::Aus); assert!(river_spieler_decision(&facing_bet("As Ad")).is_none()); @@ -87,6 +88,7 @@ fn off_decides_nothing() { /// On, a heads-up river is decided by the solver, deterministically, with a legal choice. #[test] fn on_decides_heads_up_rivers_deterministically() { + let _fest = crate::engine::frist::fest_hier(); let _g = MODE_LOCK.lock().unwrap(); set_river_spieler(RiverSpieler::Modell); for spot in [facing_bet("Qc Jc"), checked_to("Qc Jc"), facing_bet("As Ad")] { @@ -107,6 +109,7 @@ fn on_decides_heads_up_rivers_deterministically() { /// Multiway rivers stay with the heuristic. #[test] fn multiway_is_not_in_the_slice() { + let _fest = crate::engine::frist::fest_hier(); let _g = MODE_LOCK.lock().unwrap(); set_river_spieler(RiverSpieler::Modell); let mut spot = facing_bet("As Ad"); @@ -118,6 +121,7 @@ fn multiway_is_not_in_the_slice() { /// Top set never folds to a bet; six-high air facing a pot bet folds most of the time. #[test] fn the_nuts_continue_and_air_folds() { + let _fest = crate::engine::frist::fest_hier(); let (row, acts) = row_of(&facing_bet("As Ad")); assert!(prob(&row, &acts, |a| *a == Act::Fold) < 0.01, "top set folds {row:?}"); let (row, acts) = row_of(&facing_bet("6s 5s")); @@ -127,6 +131,7 @@ fn the_nuts_continue_and_air_folds() { /// Checked to in position, top set bets for value. #[test] fn top_set_bets_when_checked_to() { + let _fest = crate::engine::frist::fest_hier(); let (row, acts) = row_of(&checked_to("As Ad")); assert!(prob(&row, &acts, |a| matches!(a, Act::Bet(_))) > 0.8, "{row:?} {acts:?}"); } @@ -135,6 +140,7 @@ fn top_set_bets_when_checked_to() { /// close to equilibrium (exact best response in the game, below 1 % of the pot after `ITERS`). #[test] fn the_river_game_converges() { + let _fest = crate::engine::frist::fest_hier(); let spot = facing_bet("Qc Jc"); let (h, v) = model_lists(&spot).unwrap(); let villain = spot.live_villains().next().unwrap(); @@ -166,6 +172,7 @@ fn rows_with(spot: &Spot, r: Option<&RangeRead>) -> Vec { /// The read hook with no read, or a read without any confidence, is the plain solver bit for bit. #[test] fn a_read_without_confidence_is_bit_identical() { + let _fest = crate::engine::frist::fest_hier(); for spot in [facing_bet("Qc Jc"), checked_to("6s 5s")] { let plain = rows_with(&spot, None); let zero = rows_with(&spot, Some(&read(0.0, 0.0, 0.9))); @@ -177,6 +184,7 @@ fn a_read_without_confidence_is_bit_identical() { /// A trusted read that the villain over-folds to bets makes hero's range bet more often when checked to. #[test] fn a_trusted_over_fold_read_bets_more() { + let _fest = crate::engine::frist::fest_hier(); let spot = checked_to("6s 5s"); let bet_share = |r: Option<&RangeRead>| { let (h, v, src) = solver_lists(&spot, RiverSpieler::Modell, r).unwrap(); @@ -189,3 +197,25 @@ fn a_trusted_over_fold_read_bets_more() { let exploit = bet_share(Some(&read(0.9, 0.0, 0.9))); assert!(exploit > plain + 0.1, "plain {plain:.2} exploit {exploit:.2}"); } + +/// Why solver tests run under `frist::fest_hier`: a busy machine is a spent deadline. `decide` marks the +/// start; when most of the river's 2 s are gone before the solve starts (or the solve crawls), the +/// budget cuts it and the heuristic decides (`None`), or fewer iterations give other numbers. Under +/// `fest_hier` the same solve runs its full count: the plain rows, bit for bit. +#[test] +fn a_spent_deadline_hands_back_and_fest_hier_ignores_it() { + let spot = facing_bet("Qc Jc"); + let (h, v, src) = solver_lists(&spot, RiverSpieler::Modell, None).expect("lists"); + let full = { + let _fest = crate::engine::frist::fest_hier(); + solve_river(&spot, &h, &v, src, None).expect("solved") + }; + assert_eq!(full.iterations, ITERS); + crate::engine::frist::begin(); + std::thread::sleep(std::time::Duration::from_millis(1900)); + assert!(crate::engine::frist::left_ms(crate::engine::frist::RIVER_MS) <= 0.0); + assert!(solve_river(&spot, &h, &v, src, None).is_none(), "deadline spent: the heuristic decides"); + let _fest = crate::engine::frist::fest_hier(); + let again = solve_river(&spot, &h, &v, src, None).expect("fest: the full solve"); + assert!(full.rows.iter().zip(&again.rows).all(|(a, b)| a.to_bits() == b.to_bits())); +} diff --git a/src/engine/turn_game/tests.rs b/src/engine/turn_game/tests.rs index 02af5c21..9ced3b12 100644 --- a/src/engine/turn_game/tests.rs +++ b/src/engine/turn_game/tests.rs @@ -2,10 +2,9 @@ use super::*; use crate::card::Card; use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain}; use crate::variant::Variant; -use std::sync::Mutex; /// The switch is process-global: tests that touch it take turns. -static MODE_LOCK: Mutex<()> = Mutex::new(()); +use crate::engine::SCHALTER_LOCK as MODE_LOCK; fn rec(street: Street, seat: u8, action: SeenAction, amount: Option) -> ActionRecord { ActionRecord { street, seat, action, amount } @@ -88,6 +87,7 @@ fn cfg(iterations: u32) -> TurnConfig { #[test] fn the_decision_is_a_legal_choice_at_both_kinds_of_node() { + let _fest = crate::engine::frist::fest_hier(); let hands = ["Ac Qd", "7h 7c", "3c 4d", "Kd Qs", "9h 8h"]; for (spot_of, name) in [(facing_bet as fn(&str) -> Spot, "facing a bet"), (hero_first as fn(&str) -> Spot, "first to act")] { for hero in hands { @@ -119,6 +119,7 @@ fn the_decision_is_a_legal_choice_at_both_kinds_of_node() { #[test] fn out_of_position_hero_acts_first_with_check_or_a_bet() { + let _fest = crate::engine::frist::fest_hier(); // Hero is the big blind (seat 0) and acts first on the turn; the villain is the button (seat 4). let mut spot = s1_turn(&[], "Ac Qd", 0.0, 0.0); spot.hero_seat = 0; @@ -135,6 +136,7 @@ fn out_of_position_hero_acts_first_with_check_or_a_bet() { #[test] fn strong_hands_do_not_fold_and_air_folds_more() { + let _fest = crate::engine::frist::fest_hier(); // The villain's range: top pair or better only (aces, kings, pairs of 7 and 2). let villain: Vec<(Vec, f64)> = range(400, 21, &[]).into_iter().filter(|(c, _)| c.iter().any(|x| x.rank as u8 >= 11) || c[0].rank == c[1].rank).collect(); assert!(villain.len() > 30, "{}", villain.len()); @@ -150,6 +152,7 @@ fn strong_hands_do_not_fold_and_air_folds_more() { #[test] fn the_solve_is_deterministic_and_the_game_is_balanced_enough() { + let _fest = crate::engine::frist::fest_hier(); let spot = facing_bet("Ac Qd"); let (me, villain) = (range(150, 31, &["Ac Qd"]), range(150, 32, &[])); let a = solve_turn(&spot, &me, &villain, &cfg(200)).unwrap(); @@ -163,6 +166,7 @@ fn the_solve_is_deterministic_and_the_game_is_balanced_enough() { #[test] fn a_spot_outside_the_game_is_left_to_the_heuristic() { + let _fest = crate::engine::frist::fest_hier(); let me = range(50, 1, &["Ac Qd"]); let villain = range(50, 2, &[]); let mut river = facing_bet("Ac Qd"); @@ -177,6 +181,7 @@ fn a_spot_outside_the_game_is_left_to_the_heuristic() { #[test] fn off_means_none_and_the_engine_is_unchanged() { + let _fest = crate::engine::frist::fest_hier(); let _g = MODE_LOCK.lock().unwrap_or_else(|e| e.into_inner()); set_turn_player(TurnPlayer::Off); let spots = [facing_bet("Ac Qd"), facing_bet("7s 7c"), hero_first("3c 4d"), hero_first("Kd Qs")]; @@ -203,6 +208,7 @@ fn off_means_none_and_the_engine_is_unchanged() { #[test] fn the_public_key_ignores_hero_cards_but_not_the_line() { + let _fest = crate::engine::frist::fest_hier(); let c = TurnConfig::STANDARD; assert_eq!(public_key(&facing_bet("Ac Qd"), &c), public_key(&facing_bet("7s 7c"), &c)); assert_ne!(public_key(&facing_bet("Ac Qd"), &c), public_key(&hero_first("Ac Qd"), &c)); @@ -211,6 +217,7 @@ fn the_public_key_ignores_hero_cards_but_not_the_line() { #[test] #[ignore = "timing probe"] fn tree_sizes() { + let _fest = crate::engine::frist::fest_hier(); use crate::cfr::turn_river_game::{build, TNode, TrBets}; for (name, forced, first) in [("hero first", vec![], 0usize), ("facing bet", vec![Act::Bet(6.0)], 1), ("check-raise spot", vec![Act::Check, Act::Bet(6.0)], 0)] { let g = build(&TrBets::standard(9.1, 95.0, first), &forced); @@ -225,6 +232,7 @@ fn tree_sizes() { #[test] #[ignore = "timing probe"] fn solve_time_at_table_size() { + let _fest = crate::engine::frist::fest_hier(); for (n, cards, iters, rr) in [(160usize, 10usize, 200u32, 0usize), (160, 10, 400, 0), (160, 10, 200, 1), (100, 10, 200, 0)] { let spot = facing_bet("Ac Qd"); let (me, villain) = (range(n, 41, &["Ac Qd"]), range(n, 42, &[])); @@ -245,6 +253,7 @@ fn solve_time_at_table_size() { /// blueprint is loaded, for any hero hand (in or out of the pruned range), with a legal choice. #[test] fn stage_b_decides_every_heads_up_turn_from_the_range_model() { + let _fest = crate::engine::frist::fest_hier(); let _g = MODE_LOCK.lock().unwrap(); set_turn_ueberall(false); assert!(turn_solver_decision(&facing_bet("Qc Jc")).is_none()); @@ -263,3 +272,4 @@ fn stage_b_decides_every_heads_up_turn_from_the_range_model() { assert!(turn_solver_decision(&multi).is_none()); set_turn_ueberall(false); } +