Abandoned path. Senders can only be moved oncde.
This commit is contained in:
135
src/docbuilder.rs
Normal file
135
src/docbuilder.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
use crate::{
|
||||
field::Field,
|
||||
queue::{Message, QueueClient},
|
||||
};
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Document {
|
||||
session: Field,
|
||||
content: Field,
|
||||
}
|
||||
|
||||
impl Document {
|
||||
pub fn new(session: Field, content: Field) -> Self {
|
||||
Self {
|
||||
session: session,
|
||||
content: content,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the session id information.
|
||||
pub fn get_session(&self) -> String {
|
||||
self.session.to_string()
|
||||
}
|
||||
|
||||
/// Get the requested content.
|
||||
pub fn get_content(&self) -> String {
|
||||
self.content.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod documeents {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn create_response() {
|
||||
let id = "something";
|
||||
let txt = "stuff";
|
||||
let res = Document::new(id.into(), txt.into());
|
||||
match res.session {
|
||||
Field::Static(data) => assert_eq!(data, id),
|
||||
_ => unreachable!("wrong field type"),
|
||||
}
|
||||
match res.content {
|
||||
Field::Static(data) => assert_eq!(data, txt),
|
||||
_ => unreachable!("wrong field type"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_id_string() {
|
||||
let id = "id";
|
||||
let doc = "content";
|
||||
let reply = Document::new(id.into(), doc.into());
|
||||
assert_eq!(reply.get_session(), id);
|
||||
assert_eq!(reply.get_content(), doc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_id_content() {
|
||||
let id = "session";
|
||||
let doc = "stuff";
|
||||
let reply = Document::new(id.into(), doc.into());
|
||||
assert_eq!(reply.get_session(), id);
|
||||
assert_eq!(reply.get_content(), doc);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DocumentBuilder {
|
||||
_tx: Sender<Message>,
|
||||
rx: Receiver<Message>,
|
||||
}
|
||||
|
||||
impl QueueClient for DocumentBuilder {
|
||||
fn new(tx: Sender<Message>, rx: Receiver<Message>) -> Self {
|
||||
Self {
|
||||
_tx: tx,
|
||||
rx: rx,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_receiver(&self) -> &Receiver<Message> {
|
||||
&self.rx
|
||||
}
|
||||
|
||||
fn listen(&self) {
|
||||
let rx = self.get_receiver();
|
||||
loop {
|
||||
match rx.recv().unwrap() {
|
||||
Message::Req(data) => {
|
||||
//data.get_sender().send(Document::new("id".into(), "stuff".into())).unwrap();
|
||||
let doc = Document::new("id".into(), "stuff".into());
|
||||
data.send(doc);
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod builders {
|
||||
use crate::request_test_data::request_root_document;
|
||||
use std::sync::mpsc::{channel, TryRecvError};
|
||||
use super::*;
|
||||
|
||||
fn run_service() -> (Sender<Message>, Receiver<Message>) {
|
||||
let (tx, rx) = channel();
|
||||
let service_tx: Sender<Message>;
|
||||
DocumentBuilder::start(tx);
|
||||
match rx.recv().unwrap() {
|
||||
Message::Register(result) => service_tx = result,
|
||||
_ => unreachable!("should register the service"),
|
||||
}
|
||||
(service_tx, rx)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_document() {
|
||||
let (tx, rx) = run_service();
|
||||
let (msg, msg_rx) = request_root_document();
|
||||
tx.send(msg.into()).unwrap();
|
||||
match rx.try_recv() {
|
||||
Ok(_) => unreachable!("nothing should go back to the queue"),
|
||||
Err(err) => {
|
||||
match err {
|
||||
TryRecvError::Empty => {},
|
||||
_ => unreachable!("channel should still be open"),
|
||||
}
|
||||
},
|
||||
}
|
||||
msg_rx.recv().unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user