qualia_core_db/net/qdnf/harness/
partition.rs1use crate::net::qdnf::errors::QdnfError;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7pub struct PartitionGate {
8 partitioned: bool,
9}
10
11impl PartitionGate {
12 pub const fn open() -> Self {
14 Self { partitioned: false }
15 }
16
17 pub const fn is_partitioned(self) -> bool {
19 self.partitioned
20 }
21
22 pub fn partition(&mut self) {
24 self.partitioned = true;
25 }
26
27 pub fn heal(&mut self) {
29 self.partitioned = false;
30 }
31
32 pub fn admit(self) -> Result<(), QdnfError> {
34 if self.partitioned {
35 Err(QdnfError::WouldBlock)
36 } else {
37 Ok(())
38 }
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn partition_heal() {
48 let mut gate = PartitionGate::open();
49 assert_eq!(gate.admit(), Ok(()));
50 gate.partition();
51 assert_eq!(gate.admit(), Err(QdnfError::WouldBlock));
52 gate.heal();
53 assert_eq!(gate.admit(), Ok(()));
54 assert!(!gate.is_partitioned());
55 }
56}