lighthouse/eth2/utils/slot_clock/src/testing_slot_clock.rs
Paul Hauner 35c914baa6
Revert "Merge pull request #200 from sigp/new-structure"
This reverts commit d7a3545be1, reversing
changes made to 1da06c156c.
2019-02-14 12:09:18 +11:00

49 lines
1.1 KiB
Rust

use super::SlotClock;
use std::sync::RwLock;
use types::Slot;
#[derive(Debug, PartialEq)]
pub enum Error {}
/// Determines the present slot based upon the present system time.
pub struct TestingSlotClock {
slot: RwLock<u64>,
}
impl TestingSlotClock {
/// Create a new `TestingSlotClock`.
///
/// Returns an Error if `slot_duration_seconds == 0`.
pub fn new(slot: u64) -> TestingSlotClock {
TestingSlotClock {
slot: RwLock::new(slot),
}
}
pub fn set_slot(&self, slot: u64) {
*self.slot.write().expect("TestingSlotClock poisoned.") = slot;
}
}
impl SlotClock for TestingSlotClock {
type Error = Error;
fn present_slot(&self) -> Result<Option<Slot>, Error> {
let slot = *self.slot.read().expect("TestingSlotClock poisoned.");
Ok(Some(Slot::new(slot)))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_slot_now() {
let clock = TestingSlotClock::new(10);
assert_eq!(clock.present_slot(), Ok(Some(Slot::new(10))));
clock.set_slot(123);
assert_eq!(clock.present_slot(), Ok(Some(Slot::new(123))));
}
}