dactor_test_harness/
fault.rs1use dashmap::DashMap;
2use std::sync::atomic::AtomicU32;
3
4pub struct FaultEntry {
5 pub fault_type: String,
6 pub target: String,
7 pub duration_ms: u64,
8 pub count: AtomicU32,
9}
10
11pub struct FaultInjector {
12 faults: DashMap<String, FaultEntry>,
13}
14
15impl FaultInjector {
16 pub fn new() -> Self {
17 Self {
18 faults: DashMap::new(),
19 }
20 }
21
22 pub fn add_fault(&self, fault_type: &str, target: &str, duration_ms: u64, count: u32) {
23 let key = format!("{}:{}", fault_type, target);
24 self.faults.insert(
25 key,
26 FaultEntry {
27 fault_type: fault_type.to_string(),
28 target: target.to_string(),
29 duration_ms,
30 count: AtomicU32::new(count),
31 },
32 );
33 }
34
35 pub fn clear_all(&self) {
36 self.faults.clear();
37 }
38
39 pub fn has_fault(&self, fault_type: &str, target: &str) -> bool {
40 let key = format!("{}:{}", fault_type, target);
41 self.faults.contains_key(&key)
42 }
43
44 pub fn active_faults(&self) -> Vec<String> {
45 self.faults.iter().map(|e| e.key().clone()).collect()
46 }
47}
48
49impl Default for FaultInjector {
50 fn default() -> Self {
51 Self::new()
52 }
53}