Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiEres committed Jun 3, 2024
1 parent ca3da68 commit 722b8d2
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions orchestra/tests/subsystems_priority_channels_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
use futures::executor::ThreadPool;
use orchestra::*;

enum SendingMethod {
Send,
TrySend,
}

struct SubA {
regular: Vec<u8>,
priority: Vec<u8>,
sending_method: SendingMethod,
}

pub struct SubB {
Expand All @@ -27,8 +33,8 @@ pub struct SubB {
}

impl SubA {
fn new(regular: Vec<u8>, priority: Vec<u8>) -> Self {
Self { regular, priority }
fn new(regular: Vec<u8>, priority: Vec<u8>, sending_method: SendingMethod) -> Self {
Self { regular, priority, sending_method }
}
}

Expand All @@ -45,10 +51,17 @@ impl crate::Subsystem<OrchestraSubsystemContext<MsgA>, OrchestraError> for SubA
name: "sub A",
future: Box::pin(async move {
for i in self.regular {
sender.send_message(MsgB(i)).await;
match self.sending_method {
SendingMethod::Send => sender.send_message(MsgB(i)).await,
SendingMethod::TrySend => sender.try_send_message(MsgB(i)).unwrap(),
}
}
for i in self.priority {
sender.priority_send_message(MsgB(i)).await;
match self.sending_method {
SendingMethod::Send => sender.priority_send_message(MsgB(i)).await,
SendingMethod::TrySend =>
sender.try_priority_send_message(MsgB(i)).unwrap(),
}
}

Ok(())
Expand Down Expand Up @@ -132,7 +145,34 @@ fn test_priority_send_message() {
let (tx, mut rx) = oneshot::channel::<Vec<u8>>();
let regular = vec![1, 2, 3];
let priority = vec![42];
let sub_a = SubA::new(regular.clone(), priority.clone());
let sub_a = SubA::new(regular.clone(), priority.clone(), SendingMethod::Send);
let sub_b = SubB::new(regular.len() + priority.len(), tx);
let pool = ThreadPool::new().unwrap();
let (orchestra, _handle) = Orchestra::builder()
.sub_a(sub_a)
.sub_b(sub_b)
.spawner(DummySpawner(pool))
.build()
.unwrap();

futures::executor::block_on(async move {
for run_subsystem in orchestra.running_subsystems {
run_subsystem.await.unwrap();
}
});

assert_eq!(
priority.into_iter().chain(regular.into_iter()).collect::<Vec<u8>>(),
rx.try_recv().unwrap().unwrap()
);
}

#[test]
fn test_try_priority_send_message() {
let (tx, mut rx) = oneshot::channel::<Vec<u8>>();
let regular = vec![1, 2, 3];
let priority = vec![42];
let sub_a = SubA::new(regular.clone(), priority.clone(), SendingMethod::TrySend);
let sub_b = SubB::new(regular.len() + priority.len(), tx);
let pool = ThreadPool::new().unwrap();
let (orchestra, _handle) = Orchestra::builder()
Expand Down

0 comments on commit 722b8d2

Please sign in to comment.