Make TestingSlotClock thread-safe
This commit is contained in:
parent
271a4cf8a7
commit
5f953f76d8
@ -1,11 +1,12 @@
|
|||||||
use super::SlotClock;
|
use super::SlotClock;
|
||||||
|
use std::sync::RwLock;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Error {}
|
pub enum Error {}
|
||||||
|
|
||||||
/// Determines the present slot based upon the present system time.
|
/// Determines the present slot based upon the present system time.
|
||||||
pub struct TestingSlotClock {
|
pub struct TestingSlotClock {
|
||||||
slot: u64,
|
slot: RwLock<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestingSlotClock {
|
impl TestingSlotClock {
|
||||||
@ -13,11 +14,13 @@ impl TestingSlotClock {
|
|||||||
///
|
///
|
||||||
/// Returns an Error if `slot_duration_seconds == 0`.
|
/// Returns an Error if `slot_duration_seconds == 0`.
|
||||||
pub fn new(slot: u64) -> TestingSlotClock {
|
pub fn new(slot: u64) -> TestingSlotClock {
|
||||||
TestingSlotClock { slot }
|
TestingSlotClock {
|
||||||
|
slot: RwLock::new(slot),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_slot(&mut self, slot: u64) {
|
pub fn set_slot(&self, slot: u64) {
|
||||||
self.slot = slot;
|
*self.slot.write().expect("TestingSlotClock poisoned.") = slot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,7 +28,8 @@ impl SlotClock for TestingSlotClock {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn present_slot(&self) -> Result<Option<u64>, Error> {
|
fn present_slot(&self) -> Result<Option<u64>, Error> {
|
||||||
Ok(Some(self.slot))
|
let slot = *self.slot.read().expect("TestingSlotClock poisoned.");
|
||||||
|
Ok(Some(slot))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +39,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_slot_now() {
|
fn test_slot_now() {
|
||||||
let mut clock = TestingSlotClock::new(10);
|
let clock = TestingSlotClock::new(10);
|
||||||
assert_eq!(clock.present_slot(), Ok(Some(10)));
|
assert_eq!(clock.present_slot(), Ok(Some(10)));
|
||||||
clock.set_slot(123);
|
clock.set_slot(123);
|
||||||
assert_eq!(clock.present_slot(), Ok(Some(123)));
|
assert_eq!(clock.present_slot(), Ok(Some(123)));
|
||||||
|
Loading…
Reference in New Issue
Block a user