2026-01-08 15:02:03 -05:00
|
|
|
use crate::{
|
2026-02-13 12:00:39 -05:00
|
|
|
action::{Action, MsgAction, Records},
|
2026-03-26 13:12:17 -04:00
|
|
|
message::Message,
|
2026-04-23 11:31:52 -04:00
|
|
|
name::{Name, Names},
|
2026-01-30 13:58:55 -05:00
|
|
|
queue::{
|
2026-02-04 08:49:03 -05:00
|
|
|
data_director::{Include, Path, RegMsg, Register},
|
2026-01-30 13:58:55 -05:00
|
|
|
router::Queue,
|
|
|
|
|
},
|
2026-01-08 15:02:03 -05:00
|
|
|
};
|
|
|
|
|
use std::{
|
|
|
|
|
sync::mpsc::channel,
|
|
|
|
|
thread::{sleep, spawn},
|
|
|
|
|
time::Duration,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub struct Clock {
|
|
|
|
|
queue: Queue,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clock {
|
|
|
|
|
fn new(queue: Queue) -> Self {
|
|
|
|
|
Self { queue: queue }
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 15:04:16 -05:00
|
|
|
pub fn doc_names() -> Vec<Name> {
|
|
|
|
|
vec![Name::english("clock")]
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 10:39:03 -05:00
|
|
|
pub fn gen_message() -> Message {
|
2026-02-18 10:09:55 -05:00
|
|
|
Message::new(MsgAction::OnUpdate(Records::new(
|
|
|
|
|
Clock::doc_names(),
|
|
|
|
|
Names::new(),
|
|
|
|
|
)))
|
2026-02-04 08:49:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_path() -> Path {
|
|
|
|
|
Path::new(
|
|
|
|
|
Include::All,
|
2026-02-13 15:04:16 -05:00
|
|
|
Include::Just(Clock::doc_names()[0].clone().into()),
|
2026-02-04 08:49:03 -05:00
|
|
|
Include::Just(Action::OnUpdate),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:02:03 -05:00
|
|
|
pub fn start(mut queue: Queue) {
|
|
|
|
|
let clock = Clock::new(queue.clone());
|
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let id = queue.add_sender(tx);
|
2026-02-13 15:04:16 -05:00
|
|
|
let reg_msg = Register::new(id, RegMsg::AddDocName(Clock::doc_names()));
|
2026-02-18 10:09:55 -05:00
|
|
|
let msg = Message::new(reg_msg.clone());
|
2026-01-30 14:55:42 -05:00
|
|
|
queue.send(msg);
|
2026-01-08 15:02:03 -05:00
|
|
|
rx.recv().unwrap();
|
|
|
|
|
spawn(move || {
|
|
|
|
|
clock.listen();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn listen(&self) {
|
|
|
|
|
loop {
|
2026-02-04 08:49:03 -05:00
|
|
|
self.queue.send(Clock::gen_message());
|
2026-01-08 15:02:03 -05:00
|
|
|
sleep(Duration::from_secs(1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod clocks {
|
|
|
|
|
use super::*;
|
2026-05-13 11:35:30 -04:00
|
|
|
use crate::queue::{
|
|
|
|
|
data_director::{Include, Path},
|
|
|
|
|
router::TestClock,
|
|
|
|
|
};
|
2026-01-08 15:02:03 -05:00
|
|
|
use chrono::{TimeDelta, Utc};
|
|
|
|
|
|
|
|
|
|
static TIMEOUT: Duration = Duration::from_millis(1500);
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn does_clock_send_reply_every_second() {
|
2026-05-13 11:35:30 -04:00
|
|
|
let clock = TestClock::new();
|
|
|
|
|
let mut queue = Queue::with_clock(clock);
|
2026-01-08 15:02:03 -05:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
let id = queue.add_sender(tx);
|
|
|
|
|
let request = Register::new(
|
|
|
|
|
id.clone(),
|
|
|
|
|
RegMsg::AddRoute(Path::new(Include::All, Include::All, Include::All)),
|
|
|
|
|
);
|
2026-02-18 10:09:55 -05:00
|
|
|
queue.send(Message::new(request));
|
2026-01-08 15:02:03 -05:00
|
|
|
rx.recv_timeout(TIMEOUT).unwrap();
|
|
|
|
|
let mut holder: Vec<Message> = Vec::new();
|
|
|
|
|
let start = Utc::now();
|
|
|
|
|
Clock::start(queue.clone());
|
|
|
|
|
while holder.len() < 2 {
|
|
|
|
|
holder.push(rx.recv_timeout(TIMEOUT).unwrap());
|
|
|
|
|
}
|
|
|
|
|
let end = Utc::now();
|
|
|
|
|
assert!((end - start) > TimeDelta::seconds(1));
|
|
|
|
|
assert!((end - start) < TimeDelta::seconds(2));
|
2026-02-13 15:04:16 -05:00
|
|
|
let reg_request = Register::new(id, RegMsg::GetNameID(Clock::doc_names()[0].clone()));
|
2026-02-18 10:09:55 -05:00
|
|
|
queue.send(Message::new(reg_request));
|
2026-01-08 15:02:03 -05:00
|
|
|
rx.recv_timeout(TIMEOUT).unwrap();
|
|
|
|
|
for msg in holder.iter() {
|
|
|
|
|
let action = msg.get_action();
|
|
|
|
|
match action {
|
|
|
|
|
MsgAction::OnUpdate(result) => assert_eq!(result.len(), 0),
|
|
|
|
|
_ => unreachable!("got {:?}, should have been empty record", action),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|