Skip to main content

mcp_utils/testing/
gate.rs

1use std::sync::Arc;
2use tokio::sync::watch;
3
4#[derive(Clone)]
5pub struct Gate {
6    state: Arc<watch::Sender<GateState>>,
7}
8
9#[derive(Clone, Copy, Default)]
10struct GateState {
11    reached: bool,
12    open: bool,
13}
14
15impl Gate {
16    pub fn new() -> Self {
17        Self { state: Arc::new(watch::Sender::new(GateState::default())) }
18    }
19
20    pub fn open(&self) {
21        self.state.send_modify(|state| state.open = true);
22    }
23
24    pub async fn reached(&self) {
25        let mut state = self.state.subscribe();
26        let _ = state.wait_for(|state| state.reached).await;
27    }
28
29    pub(crate) async fn pass(&self) {
30        self.state.send_modify(|state| state.reached = true);
31        let mut state = self.state.subscribe();
32        let _ = state.wait_for(|state| state.open).await;
33    }
34}
35
36impl Default for Gate {
37    fn default() -> Self {
38        Self::new()
39    }
40}