Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
260 lines
8.3 KiB
Rust
260 lines
8.3 KiB
Rust
pub mod action;
|
|
mod document;
|
|
mod message;
|
|
mod mtterror;
|
|
pub mod name;
|
|
mod queue;
|
|
|
|
use document::{Clock, CreateDoc, Session};
|
|
use message::{wrapper::Message, MessageAction};
|
|
use queue::{
|
|
data_director::{RegMsg, Register},
|
|
router::Queue,
|
|
};
|
|
use std::{
|
|
sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender},
|
|
time::Duration,
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
pub use action::*;
|
|
pub use document::MissingTranslation;
|
|
pub use mtterror::{ErrorID, MTTError};
|
|
pub use name::{Name, NameType};
|
|
pub use queue::data_director::{Include, Path};
|
|
|
|
#[cfg(test)]
|
|
mod support_tests {
|
|
use super::*;
|
|
use std::time::Duration;
|
|
|
|
pub static TIMEOUT: Duration = Duration::from_millis(500);
|
|
|
|
pub fn random_name() -> Name {
|
|
Name::english(Uuid::new_v4().to_string().as_str())
|
|
}
|
|
}
|
|
|
|
static TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
#[derive(Clone)]
|
|
pub struct MoreThanText {
|
|
queue: Queue,
|
|
}
|
|
|
|
impl MoreThanText {
|
|
pub fn new() -> Self {
|
|
let queue = Queue::new();
|
|
let mut output = Self {
|
|
queue: queue.clone(),
|
|
};
|
|
Clock::start(queue.clone());
|
|
CreateDoc::start(queue.clone());
|
|
output
|
|
.create_document(Session::document_definition())
|
|
.unwrap();
|
|
output
|
|
}
|
|
|
|
fn new_session() -> ClientAction {
|
|
Addition::new(Session::doc_names()[0].clone()).into()
|
|
}
|
|
|
|
fn recursive_message_request<UA>(&mut self, action: UA) -> Uuid
|
|
where
|
|
UA: Into<ClientAction>,
|
|
{
|
|
match self.records(action) {
|
|
Ok(data) => {
|
|
if data.len() == 0 {
|
|
self.recursive_message_request(MoreThanText::new_session())
|
|
} else {
|
|
let rec = data.iter().last().unwrap();
|
|
match rec.get(Name::english("id")).unwrap() {
|
|
Field::Uuid(id) => id,
|
|
_ => unreachable!("should always return uuid"),
|
|
}
|
|
}
|
|
}
|
|
Err(_) => self.recursive_message_request(MoreThanText::new_session()),
|
|
}
|
|
}
|
|
|
|
pub fn validate_session(&mut self, session: Option<String>) -> Uuid {
|
|
let action = match session {
|
|
Some(data) => match Uuid::try_from(data.as_str()) {
|
|
Ok(id) => {
|
|
let mut query = Query::new(Session::doc_names()[0].clone());
|
|
let mut calc = Calculation::new(Operand::Equal);
|
|
calc.add_value(CalcValue::Existing(FieldType::Uuid))
|
|
.unwrap();
|
|
calc.add_value(id).unwrap();
|
|
query.add(Name::english("id"), calc);
|
|
query.into()
|
|
}
|
|
Err(_) => MoreThanText::new_session(),
|
|
},
|
|
None => MoreThanText::new_session(),
|
|
};
|
|
self.recursive_message_request(action)
|
|
}
|
|
|
|
pub fn records<UA>(&mut self, request: UA) -> Result<Records, MTTError>
|
|
where
|
|
UA: Into<ClientAction>,
|
|
{
|
|
let req = request.into();
|
|
let (tx, rx) = channel();
|
|
let sender_id = self.queue.add_sender(tx);
|
|
let doc_id = req.doc_name().clone();
|
|
let msg = Message::new(req);
|
|
let msg_id = msg.get_message_id();
|
|
let paths = [
|
|
Path::new(
|
|
Include::Just(msg_id.clone()),
|
|
Include::Just(doc_id.clone()),
|
|
Include::Just(Action::Records),
|
|
),
|
|
Path::new(
|
|
Include::Just(msg_id.clone()),
|
|
Include::All,
|
|
Include::Just(Action::Error),
|
|
),
|
|
];
|
|
for path in paths.iter() {
|
|
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
|
self.queue.send(Message::new(reg_msg));
|
|
let result = rx.recv().unwrap();
|
|
let action = result.get_action();
|
|
match action {
|
|
MsgAction::Register(status) => match status.get_msg() {
|
|
RegMsg::Error(err) => {
|
|
let mut error = err.clone();
|
|
error.add_parent(ErrorID::Document(msg.doc_name().clone()));
|
|
self.queue.remove_sender(&sender_id);
|
|
return Err(error);
|
|
}
|
|
_ => {}
|
|
},
|
|
_ => unreachable!("got {:?} should have been a registry message", action),
|
|
}
|
|
}
|
|
self.queue.send(msg);
|
|
let output = match rx.recv_timeout(TIMEOUT) {
|
|
Ok(data) => match data.get_action() {
|
|
MsgAction::Records(data) => Ok(data.clone()),
|
|
MsgAction::Error(err) => Err(err.clone()),
|
|
_ => unreachable!("should only receive records or errors"),
|
|
},
|
|
Err(_) => Err(MTTError::new(ErrorID::TimeOut)),
|
|
};
|
|
self.queue.remove_sender(&sender_id);
|
|
output
|
|
}
|
|
|
|
pub fn create_document(&mut self, docdef: DocDef) -> Result<(), MTTError> {
|
|
let (tx, rx) = channel();
|
|
let sender_id = self.queue.add_sender(tx);
|
|
let msg = Message::new(docdef);
|
|
let msg_id = msg.get_message_id();
|
|
let paths = [
|
|
Path::new(
|
|
Include::Just(msg_id.clone()),
|
|
Include::All,
|
|
Include::Just(Action::DocumentCreated),
|
|
),
|
|
Path::new(
|
|
Include::Just(msg_id.clone()),
|
|
Include::All,
|
|
Include::Just(Action::Error),
|
|
),
|
|
];
|
|
for path in paths.iter() {
|
|
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
|
self.queue.send(Message::new(reg_msg));
|
|
rx.recv().unwrap(); // Wait for completion.
|
|
}
|
|
self.queue.send(msg);
|
|
let output = match rx.recv_timeout(TIMEOUT) {
|
|
Ok(data) => match data.get_action() {
|
|
MsgAction::DocumentCreated => Ok(()),
|
|
MsgAction::Error(err) => Err(err.clone()),
|
|
_ => unreachable!("should only receive records or errors"),
|
|
},
|
|
Err(_) => Err(MTTError::new(ErrorID::TimeOut)),
|
|
};
|
|
self.queue.remove_sender(&sender_id);
|
|
output
|
|
}
|
|
|
|
pub fn get_document(&self, name: &str, id: &str) -> Result<String, MTTError> {
|
|
if name == "page" {
|
|
Ok("something".to_string())
|
|
} else {
|
|
Err(MTTError::new(ErrorID::DocumentNotFound))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct TestMoreThanText {
|
|
mtt: MoreThanText,
|
|
queue: Queue,
|
|
channel: Option<Receiver<Message>>,
|
|
}
|
|
|
|
impl TestMoreThanText {
|
|
pub fn new() -> Self {
|
|
let mut mtt = MoreThanText::new();
|
|
let queue = mtt.queue.clone();
|
|
Self {
|
|
mtt: mtt,
|
|
queue: queue,
|
|
channel: None,
|
|
}
|
|
}
|
|
|
|
pub fn get_morethantext(&self) -> MoreThanText {
|
|
self.mtt.clone()
|
|
}
|
|
|
|
pub fn send_time_pulse(&self) {
|
|
let msg = Clock::gen_message();
|
|
self.queue.send(msg);
|
|
}
|
|
|
|
pub fn register_channel(&mut self, paths: Vec<Path>) {
|
|
let mut queue = self.mtt.queue.clone();
|
|
let (tx, rx) = channel();
|
|
let sender_id = queue.add_sender(tx);
|
|
for path in paths.iter() {
|
|
let reg_msg = Register::new(sender_id.clone(), RegMsg::AddRoute(path.clone()));
|
|
queue.send(Message::new(reg_msg));
|
|
rx.recv().unwrap(); // Wait for completion.
|
|
}
|
|
self.channel = Some(rx);
|
|
}
|
|
|
|
pub fn recv(&self) -> Result<Message, RecvTimeoutError> {
|
|
match &self.channel {
|
|
Some(rx) => rx.recv_timeout(Duration::from_millis(500)),
|
|
None => panic!("test environment does not have a channel setup"),
|
|
}
|
|
}
|
|
|
|
pub fn get_trigger_records(&self, action: Action) -> Records {
|
|
let msg = self.recv().unwrap();
|
|
let msg_action = msg.get_action();
|
|
if action == msg_action.clone().into() {
|
|
match msg_action {
|
|
MsgAction::OnAddition(data) => data.clone(),
|
|
MsgAction::OnDelete(data) => data.clone(),
|
|
MsgAction::OnQuery(data) => data.clone(),
|
|
MsgAction::OnUpdate(data) => data.clone(),
|
|
_ => panic!("{:?} is not a trigger", action),
|
|
}
|
|
} else {
|
|
panic!("received {:?} instead of {:?} trigger", msg, action);
|
|
}
|
|
}
|
|
}
|