lib.rs (1522B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::sync::{ 18 Mutex, 19 atomic::{AtomicBool, Ordering}, 20 }; 21 22 static ENABLED: AtomicBool = AtomicBool::new(false); 23 static SCENARIO: Mutex<Vec<&'static str>> = Mutex::new(Vec::new()); 24 25 #[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] 26 #[error("injected failure at {0}")] 27 pub struct InjectedErr(pub &'static str); 28 29 pub fn set_failure_scenario(failures: &[&'static str]) { 30 ENABLED.store(true, Ordering::SeqCst); 31 let mut scenario = SCENARIO.lock().unwrap(); 32 scenario.clear(); 33 scenario.extend_from_slice(failures); 34 scenario.reverse(); 35 } 36 37 pub fn fail_point(step: &'static str) -> Result<(), InjectedErr> { 38 if ENABLED.load(Ordering::SeqCst) && SCENARIO.lock().unwrap().pop_if(|it| *it == step).is_some() 39 { 40 Err(InjectedErr(step)) 41 } else { 42 Ok(()) 43 } 44 }