Got frontend to make a validate request.
All checks were successful
MoreThanText/morethantext/pipeline/head This commit looks good
All checks were successful
MoreThanText/morethantext/pipeline/head This commit looks good
This commit is contained in:
parent
9d30dab87d
commit
b761791044
87
src/frontend.rs
Normal file
87
src/frontend.rs
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
use crate::{
|
||||||
|
Field,
|
||||||
|
Request,
|
||||||
|
queue::Message,
|
||||||
|
session2::SessionMessage,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
sync::mpsc::{channel, Receiver, Sender},
|
||||||
|
thread::spawn,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FrontEnd {
|
||||||
|
tx: Sender<Message>,
|
||||||
|
rx: Receiver<Message>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FrontEnd {
|
||||||
|
fn new(tx: Sender<Message>, rx: Receiver<Message>) -> Self {
|
||||||
|
Self {
|
||||||
|
tx: tx,
|
||||||
|
rx: rx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start(queue_tx: Sender<Message>) {
|
||||||
|
spawn(move || {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let frontend = FrontEnd::new(queue_tx.clone(), rx);
|
||||||
|
queue_tx.send(tx.into()).unwrap();
|
||||||
|
frontend.listen();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listen(&self) {
|
||||||
|
loop {
|
||||||
|
self.rx.recv().unwrap();
|
||||||
|
let id: Option<Field> = None;
|
||||||
|
let msg: SessionMessage = id.into();
|
||||||
|
self.tx.send(msg.into()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod frontends {
|
||||||
|
use crate::create_request::empty_request;
|
||||||
|
use std::time::Duration;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn run_service() -> (Sender<Message>, Receiver<Message>) {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let service_tx: Sender<Message>;
|
||||||
|
FrontEnd::start(tx);
|
||||||
|
match rx.recv().unwrap() {
|
||||||
|
Message::Register(result) => service_tx = result,
|
||||||
|
_ => unreachable!("should register the service"),
|
||||||
|
}
|
||||||
|
(service_tx, rx)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gets_registered() {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
FrontEnd::start(tx);
|
||||||
|
match rx.recv().unwrap() {
|
||||||
|
Message::Register(_) => {
|
||||||
|
},
|
||||||
|
_ => unreachable!("should register the service"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handle_request() {
|
||||||
|
let (tx, rx) = run_service();
|
||||||
|
let (req, _) = empty_request();
|
||||||
|
tx.send(req.into()).unwrap();
|
||||||
|
match rx.recv_timeout(Duration::from_millis(500)).unwrap() {
|
||||||
|
Message::SessMsg(output) => {
|
||||||
|
match output {
|
||||||
|
SessionMessage::Validate(result) => assert!(result.is_none()),
|
||||||
|
_ => unreachable!("Should have sent a val9idate"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => unreachable!("Should have generated a session message"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
mod client;
|
mod client;
|
||||||
mod data;
|
mod data;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod frontend;
|
||||||
mod message;
|
mod message;
|
||||||
mod queue;
|
mod queue;
|
||||||
mod router;
|
mod router;
|
||||||
mod session;
|
mod session;
|
||||||
|
mod session2;
|
||||||
|
|
||||||
use client::{Client, ClientMsg};
|
use client::{Client, ClientMsg};
|
||||||
use message::{Message, MsgData};
|
use message::{Message, MsgData};
|
||||||
@ -446,6 +448,7 @@ mod test_message {
|
|||||||
|
|
||||||
/// Application client to MoreThanText
|
/// Application client to MoreThanText
|
||||||
pub struct MoreThanText {
|
pub struct MoreThanText {
|
||||||
|
// tx: Sender<Message>,
|
||||||
session: Option<SessionData>,
|
session: Option<SessionData>,
|
||||||
tx: Sender<Message>,
|
tx: Sender<Message>,
|
||||||
}
|
}
|
||||||
@ -461,6 +464,11 @@ impl MoreThanText {
|
|||||||
/// MoreThanText::new();
|
/// MoreThanText::new();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
/* Future code.
|
||||||
|
* tx = Queue::start();
|
||||||
|
* tx.send(Session::start().into()).unwrap();
|
||||||
|
* Self { tx: tx }
|
||||||
|
*/
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut senders = Vec::new();
|
let mut senders = Vec::new();
|
||||||
senders.push(Client::start(tx.clone()));
|
senders.push(Client::start(tx.clone()));
|
||||||
|
34
src/queue.rs
34
src/queue.rs
@ -1,13 +1,17 @@
|
|||||||
use crate::Request;
|
use crate::{
|
||||||
|
Request,
|
||||||
|
session2::SessionMessage,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
sync::mpsc::{channel, Receiver, Sender},
|
sync::mpsc::{channel, Receiver, Sender},
|
||||||
thread::spawn,
|
thread::spawn,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
enum Message {
|
pub enum Message {
|
||||||
Register(Sender<Message>),
|
Register(Sender<Message>),
|
||||||
Req(Request),
|
Req(Request),
|
||||||
|
SessMsg(SessionMessage),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Request> for Message {
|
impl From<Request> for Message {
|
||||||
@ -22,10 +26,19 @@ impl From<Sender<Message>> for Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<SessionMessage> for Message {
|
||||||
|
fn from(value: SessionMessage) -> Self {
|
||||||
|
Message::SessMsg(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod messages {
|
mod messages {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::create_request::empty_request;
|
use crate::{
|
||||||
|
create_request::empty_request,
|
||||||
|
Field,
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_request() {
|
fn from_request() {
|
||||||
@ -51,6 +64,21 @@ mod messages {
|
|||||||
_ => unreachable!("should have been a register"),
|
_ => unreachable!("should have been a register"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_sessionmessage() {
|
||||||
|
let id: Option<Field> = None;
|
||||||
|
let sess_msg: SessionMessage = id.into();
|
||||||
|
match sess_msg.into() {
|
||||||
|
Message::SessMsg(result) => {
|
||||||
|
match result {
|
||||||
|
SessionMessage::Validate(data) => assert!(data.is_none()),
|
||||||
|
_ => unreachable!("should have been a validate"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => unreachable!("should have been a session message"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Queue {
|
struct Queue {
|
||||||
|
125
src/session2.rs
Normal file
125
src/session2.rs
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
use crate::{
|
||||||
|
Field,
|
||||||
|
queue::Message,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
sync::mpsc::{channel, Receiver, Sender},
|
||||||
|
thread::spawn,
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum SessionMessage {
|
||||||
|
New(Field),
|
||||||
|
Validate(Option<Field>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Option<Field>> for SessionMessage {
|
||||||
|
fn from(value: Option<Field>) -> Self {
|
||||||
|
SessionMessage::Validate(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod crete_session_message {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn create_session_new() -> SessionMessage {
|
||||||
|
SessionMessage::New(Uuid::new_v4().into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod sessionmessages {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_optional_field() {
|
||||||
|
let text = "afield";
|
||||||
|
let id = Some(text.into());
|
||||||
|
match id.into() {
|
||||||
|
SessionMessage::Validate(result) => {
|
||||||
|
match result {
|
||||||
|
Some(data) => {
|
||||||
|
match data {
|
||||||
|
Field::Static(output) => assert_eq!(output, text),
|
||||||
|
_ => unreachable!("should have returned static text"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => unreachable!("shoulf have returned data"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => unreachable!("should have been a vaqlidate"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Session {
|
||||||
|
tx: Sender<Message>,
|
||||||
|
rx: Receiver<Message>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Session {
|
||||||
|
fn new(tx: Sender<Message>, rx: Receiver<Message>) -> Self {
|
||||||
|
Self {
|
||||||
|
tx: tx,
|
||||||
|
rx: rx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start(queue_tx: Sender<Message>) {
|
||||||
|
spawn(move || {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let session = Session::new(queue_tx.clone(), rx);
|
||||||
|
queue_tx.send(tx.clone().into()).unwrap();
|
||||||
|
session.listen();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listen(&self) {
|
||||||
|
loop {
|
||||||
|
self.rx.recv().unwrap();
|
||||||
|
let res = Uuid::nil();
|
||||||
|
self.tx.send(SessionMessage::New(res.into()).into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod sessions {
|
||||||
|
use std::time::Duration;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gets_registered() {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
Session::start(tx);
|
||||||
|
match rx.recv().unwrap() {
|
||||||
|
Message::Register(_) => {},
|
||||||
|
_ => unreachable!("should register the service"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_none_returns_new_id() {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let sender: Sender<Message>;
|
||||||
|
Session::start(tx);
|
||||||
|
match rx.recv().unwrap() {
|
||||||
|
Message::Register(result) => sender = result,
|
||||||
|
_ => unreachable!("should register the service"),
|
||||||
|
}
|
||||||
|
let data: Option<Field> = None;
|
||||||
|
let req: SessionMessage = data.into();
|
||||||
|
sender.send(req.into()).unwrap();
|
||||||
|
match rx.recv_timeout(Duration::from_millis(500)).unwrap() {
|
||||||
|
Message::SessMsg(data) => {
|
||||||
|
match data {
|
||||||
|
SessionMessage::New(_) => {},
|
||||||
|
_ => unreachable!("should have been a new session"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => unreachable!("should have been a session message response"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user