Laying the ground work to add pages.

This commit is contained in:
2025-04-24 12:00:17 -04:00
parent cb69c4d55a
commit cb9bac9d8a
5 changed files with 103 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use crate::queue::{Message, MsgType, Queue};
use crate::{ErrorType, queue::{Message, MsgType, Queue}};
use std::{
sync::mpsc::{channel, Receiver},
thread::spawn,
@ -32,10 +32,10 @@ impl Document {
loop {
let msg = self.rx.recv().unwrap();
let mut reply = msg.reply(MsgType::Document);
reply.add_data(
"sess_id",
msg.get_data("sess_id").unwrap().to_uuid().unwrap(),
);
if msg.get_data("name").is_some() {
reply = msg.reply(MsgType::Error);
reply.add_data("error_type", ErrorType::DocumentNotFound);
}
reply.add_data("doc", "Something goes hwew");
self.queue.send(reply).unwrap();
}
@ -50,17 +50,17 @@ pub mod documents {
const TIMEOUT: Duration = Duration::from_millis(500);
fn setup_document(listen_for: Vec<MsgType>) -> (Queue, Receiver<Message>) {
fn setup_document() -> (Queue, Receiver<Message>) {
let queue = Queue::new();
let (tx, rx) = channel();
queue.add(tx, listen_for);
queue.add(tx, [MsgType::Document, MsgType::Error].to_vec());
Document::start(queue.clone());
(queue, rx)
}
#[test]
fn start_service() {
let (queue, rx) = setup_document([MsgType::Document].to_vec());
let (queue, rx) = setup_document();
let id = Uuid::new_v4();
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("sess_id", id.clone());
@ -71,7 +71,28 @@ pub mod documents {
MsgType::Document => {}
_ => unreachable!("got {:?} should have gotten document", msg.get_msg_type()),
}
assert_eq!(reply.get_data("sess_id").unwrap().to_uuid().unwrap(), id);
assert!(reply.get_data("doc").is_some());
}
#[test]
fn no_existing_document() {
let (queue, rx) = setup_document();
let name = format!("name-{}", Uuid::new_v4());
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("name", name.clone());
queue.send(msg.clone()).unwrap();
let reply = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply.get_id(), msg.get_id());
match reply.get_msg_type() {
MsgType::Error => {},
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
}
match reply.get_data("error_type") {
Some(err) => match err.to_error_type().unwrap() {
ErrorType::DocumentNotFound => {},
_ => unreachable!("got {:?}: should have been document not found'", err),
},
None => unreachable!("should contain error type"),
}
}
}