harmont_cli/orchestrator/
cancel.rs1use std::sync::Arc;
8use std::sync::atomic::{AtomicBool, Ordering};
9
10#[derive(Debug, Clone, Default)]
11pub struct CancellationToken {
12 inner: Arc<AtomicBool>,
13}
14
15impl CancellationToken {
16 #[must_use]
17 pub fn new() -> Self {
18 Self::default()
19 }
20
21 pub fn cancel(&self) {
22 self.inner.store(true, Ordering::SeqCst);
23 }
24
25 #[must_use]
26 pub fn is_cancelled(&self) -> bool {
27 self.inner.load(Ordering::SeqCst)
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 #[test]
36 fn default_is_not_cancelled() {
37 assert!(!CancellationToken::new().is_cancelled());
38 }
39
40 #[test]
41 fn cancel_persists() {
42 let t = CancellationToken::new();
43 t.cancel();
44 assert!(t.is_cancelled());
45 }
46
47 #[test]
48 fn cancel_is_clone_shared() {
49 let t = CancellationToken::new();
50 let u = t.clone();
51 t.cancel();
52 assert!(u.is_cancelled());
53 }
54}