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
use super::SlotClock;
use std::sync::RwLock;
use std::time::Duration;
use types::Slot;

/// A slot clock where the slot is manually set instead of being determined by the system time.
///
/// Useful for testing scenarios.
pub struct TestingSlotClock {
    slot: RwLock<Slot>,
}

impl TestingSlotClock {
    pub fn set_slot(&self, slot: u64) {
        *self.slot.write().expect("TestingSlotClock poisoned.") = Slot::from(slot);
    }

    pub fn advance_slot(&self) {
        self.set_slot(self.now().unwrap().as_u64() + 1)
    }
}

impl SlotClock for TestingSlotClock {
    fn new(genesis_slot: Slot, _genesis_duration: Duration, _slot_duration: Duration) -> Self {
        TestingSlotClock {
            slot: RwLock::new(genesis_slot),
        }
    }

    fn now(&self) -> Option<Slot> {
        let slot = *self.slot.read().expect("TestingSlotClock poisoned.");
        Some(slot)
    }

    /// Always returns a duration of 1 second.
    fn duration_to_next_slot(&self) -> Option<Duration> {
        Some(Duration::from_secs(1))
    }

    /// Always returns a slot duration of 0 seconds.
    fn slot_duration(&self) -> Duration {
        Duration::from_secs(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_slot_now() {
        let null = Duration::from_secs(0);

        let clock = TestingSlotClock::new(Slot::new(10), null, null);
        assert_eq!(clock.now(), Some(Slot::new(10)));
        clock.set_slot(123);
        assert_eq!(clock.now(), Some(Slot::new(123)));
    }
}