diff --git a/src/message.rs b/src/message.rs index c874642..0565cf9 100644 --- a/src/message.rs +++ b/src/message.rs @@ -113,6 +113,14 @@ impl Message { fn get_action(&self) -> &MsgAction { &self.action } + + fn reply(&self, resp: Response) -> Self { + Self { + msg_id: self.msg_id.clone(), + document_id: self.document_id.clone(), + action: MsgAction::Reply(resp), + } + } } #[cfg(test)] @@ -175,6 +183,23 @@ mod messages { ids.push(id); } } + + #[test] + fn Can_make_reply_message() { + let name = "testing"; + let msg = Message::new(name, MsgAction::Query(Access::new())); + let responce = Response::new(); + let reply = msg.reply(responce); + assert_eq!(reply.get_message_id(), msg.get_message_id()); + match reply.get_document_id() { + NameID::Name(data) => assert_eq!(data, name), + _ => unreachable!("should have been a name"), + } + match reply.get_action() { + MsgAction::Reply(_) => {} + _ => unreachable!("should have been a reply"), + } + } } #[derive(Clone, Debug)] @@ -538,7 +563,7 @@ mod queuedatas { match rx.recv_timeout(TIMEOUT) { Ok(_) => unreachable!("should have timed out"), Err(err) => match err { - RecvTimeoutError::Timeout => {}, + RecvTimeoutError::Timeout => {} _ => unreachable!("should have timed out"), }, } @@ -621,9 +646,7 @@ mod queuedatas { ), ] .to_vec(); - queuedata - .register(tx, name.to_string(), routes) - .unwrap(); + queuedata.register(tx, name.to_string(), routes).unwrap(); for senders in queuedata.routes.values() { assert_eq!(senders.len(), 1, "should be no double entries"); } @@ -648,7 +671,9 @@ mod queuedatas { let mut queuedata = QueueData::new(); let name = "something"; let (tx, _) = channel(); - queuedata.register(tx, name.to_string(), Vec::new()).unwrap(); + queuedata + .register(tx, name.to_string(), Vec::new()) + .unwrap(); let msg = Message::new("something", MsgAction::Create(DocDef::new())); match queuedata.send(msg) { Ok(_) => {} @@ -664,11 +689,18 @@ mod queuedatas { let action = MsgAction::Query(Access::new()); let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); - let routes = [ - RouteRequest::new(Include::All, Include::Some(name1.to_string()), Include::All) - ].to_vec(); - queuedata.register(tx1, name1.to_string(), routes.clone()).unwrap(); - queuedata.register(tx2, name2.to_string(), routes.clone()).unwrap(); + let routes = [RouteRequest::new( + Include::All, + Include::Some(name1.to_string()), + Include::All, + )] + .to_vec(); + queuedata + .register(tx1, name1.to_string(), routes.clone()) + .unwrap(); + queuedata + .register(tx2, name2.to_string(), routes.clone()) + .unwrap(); let msg = Message::new(name1, action.clone()); queuedata.send(msg.clone()).unwrap(); let result1 = rx1.recv_timeout(TIMEOUT).unwrap(); @@ -731,7 +763,12 @@ impl CreateDoc { fn start(mut queue: Queue) { let (tx, rx) = channel(); - let routes = [RouteRequest::new(Include::All, Include::All, Include::Some(Action::Create))].to_vec(); + let routes = [RouteRequest::new( + Include::All, + Include::All, + Include::Some(Action::Create), + )] + .to_vec(); let id = queue.register(tx, "document".to_string(), routes).unwrap(); let doc = CreateDoc::new(queue, rx); spawn(move || { @@ -746,6 +783,7 @@ impl CreateDoc { NameID::Name(name) => Document::start(self.queue.clone(), name.clone()), NameID::ID(_) => unreachable!("should be a name"), } + self.queue.send(msg.reply(Response::new())).unwrap(); } } } @@ -811,15 +849,22 @@ mod createdocs { use super::support_test::TIMEOUT; use super::*; - //#[test] + #[test] fn create_document_creation() { - let queue = Queue::new(); - //let (tx, rx) = channel(); - //queue.register2 + let mut queue = Queue::new(); + let (tx, rx) = channel(); + let routes = [RouteRequest::new( + Include::All, + Include::All, + Include::Some(Action::Reply), + )] + .to_vec(); + queue.register(tx, "testing".to_string(), routes).unwrap(); CreateDoc::start(queue.clone()); let name = "project"; let msg = Message::new(name, MsgAction::Create(DocDef::new())); queue.send(msg).unwrap(); + rx.recv_timeout(TIMEOUT).unwrap(); let msg2 = Message::new(name, MsgAction::Query(Access::new())); queue.send(msg2).unwrap(); }