mithril_common/era/adapters/
dummy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use async_trait::async_trait;
use std::sync::RwLock;

use crate::era::{EraMarker, EraReaderAdapter};
use crate::StdResult;

/// Dummy adapter is intended to be used in a test environment (end to end test)
/// to simulate not yet activated Eras.
#[derive(Default)]
pub struct DummyAdapter {
    markers: RwLock<Vec<EraMarker>>,
}

impl DummyAdapter {
    /// Create a new instance directly from markers
    pub fn from_markers(markers: Vec<EraMarker>) -> Self {
        let myself = Self::default();
        myself.set_markers(markers);

        myself
    }

    /// Tells what markers should be sent back by the adapter.
    pub fn set_markers(&self, markers: Vec<EraMarker>) {
        let mut my_markers = self.markers.write().unwrap();
        *my_markers = markers;
    }
}

#[async_trait]
impl EraReaderAdapter for DummyAdapter {
    async fn read(&self) -> StdResult<Vec<EraMarker>> {
        let markers = self.markers.read().unwrap();

        Ok((*markers.clone()).to_vec())
    }
}

#[cfg(test)]
mod tests {
    use crate::entities::Epoch;

    use super::super::super::SupportedEra;
    use super::*;

    #[tokio::test]
    async fn empty_dummy_adapter() {
        let adapter = DummyAdapter::default();

        assert!(adapter
            .read()
            .await
            .expect("dummy adapter shall not fail reading")
            .is_empty());
    }

    #[tokio::test]
    async fn dummy_adapter_output() {
        let markers = vec![
            EraMarker::new("one", Some(Epoch(1))),
            EraMarker::new(&SupportedEra::dummy().to_string(), None),
            EraMarker::new(&SupportedEra::dummy().to_string(), Some(Epoch(10))),
        ];

        let adapter = DummyAdapter::default();
        adapter.set_markers(markers.clone());

        assert_eq!(
            markers,
            adapter
                .read()
                .await
                .expect("dummy adapter shall not fail reading")
        );
    }
}