1
//! A testing implementation of [`NetDirProvider`].
2

            
3
use std::sync::{Arc, Mutex};
4

            
5
use crate::{DirEvent, Error, NetDir, NetDirProvider, Result};
6

            
7
use postage::broadcast::{self, Receiver, Sender};
8
use postage::sink::Sink as _;
9

            
10
/// Helper implementation of a [`NetDirProvider`].
11
///
12
/// A [`TestNetDirProvider`] can be used to provide a netdir in a single
13
/// situation that requires a [`NetDirProvider`].
14
///
15
/// It notifies its owner of changes
16
/// by firing a [`NewConsensus`](DirEvent::NewConsensus) event
17
/// each time [`TestNetDirProvider::set_netdir_and_notify`] is called.
18
///
19
/// Calling [`TestNetDirProvider::set_netdir`] will **not** trigger a notification.
20
#[derive(Debug)]
21
pub struct TestNetDirProvider {
22
    /// The mutable inner state.
23
    inner: Mutex<Inner>,
24
}
25

            
26
/// The inner part of a TestNetDirProvider.
27
#[derive(Debug)]
28
struct Inner {
29
    /// The latest netdir that this will return.
30
    current: Option<Arc<NetDir>>,
31
    /// The event sender, which fires every time the netdir is updated.
32
    event_tx: Sender<DirEvent>,
33
    /// The event receiver.
34
    _event_rx: Receiver<DirEvent>,
35
}
36

            
37
#[allow(clippy::new_without_default)]
38
impl TestNetDirProvider {
39
    /// Create a new [`TestNetDirProvider`] with no netdir available.
40
1312
    pub fn new() -> Self {
41
1312
        let (event_tx, _event_rx) = broadcast::channel(128);
42
1312
        let inner = Inner {
43
1312
            current: None,
44
1312
            event_tx,
45
1312
            _event_rx,
46
1312
        };
47
1312

            
48
1312
        Self {
49
1312
            inner: Mutex::new(inner),
50
1312
        }
51
1312
    }
52

            
53
    /// Replace the `NetDir` in this [`TestNetDirProvider`].
54
1144
    pub fn set_netdir(&self, dir: impl Into<Arc<NetDir>>) {
55
1144
        let mut inner = self.inner.lock().expect("lock poisoned");
56
1144
        inner.current = Some(dir.into());
57
1144
    }
58

            
59
    /// Replace the `NetDir` in this [`TestNetDirProvider`],
60
    /// firing a [`NewConsensus`](DirEvent::NewConsensus) event.
61
104
    pub async fn set_netdir_and_notify(&self, dir: impl Into<Arc<NetDir>>) {
62
104
        let mut event_tx = {
63
104
            let mut inner = self.inner.lock().expect("lock poisoned");
64
104
            inner.current = Some(dir.into());
65
104
            inner.event_tx.clone()
66
104
        };
67
104
        event_tx
68
104
            .send(DirEvent::NewConsensus)
69
            .await
70
104
            .expect("receivers were dropped");
71
104
    }
72
}
73

            
74
impl From<NetDir> for TestNetDirProvider {
75
224
    fn from(nd: NetDir) -> Self {
76
224
        let rv = Self::new();
77
224
        rv.set_netdir(nd);
78
224
        rv
79
224
    }
80
}
81

            
82
impl NetDirProvider for TestNetDirProvider {
83
2412
    fn netdir(&self, _timeliness: crate::Timeliness) -> Result<Arc<NetDir>> {
84
2412
        match self.inner.lock().expect("lock poisoned").current.as_ref() {
85
1932
            Some(netdir) => Ok(Arc::clone(netdir)),
86
480
            None => Err(Error::NoInfo),
87
        }
88
2412
    }
89

            
90
560
    fn events(&self) -> futures::stream::BoxStream<'static, DirEvent> {
91
560
        let inner = self.inner.lock().expect("lock poisoned");
92
560
        let events = inner.event_tx.subscribe();
93
560
        Box::pin(events)
94
560
    }
95

            
96
56
    fn params(&self) -> Arc<dyn AsRef<crate::params::NetParameters>> {
97
56
        if let Ok(nd) = self.netdir(crate::Timeliness::Unchecked) {
98
56
            nd
99
        } else {
100
            Arc::new(crate::params::NetParameters::default())
101
        }
102
56
    }
103
}