Skip to content

Commit

Permalink
Add wait_for_children test
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Oct 22, 2023
1 parent 7a1cb77 commit dc2bc9e
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/integration_test_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{
sync::{Arc, Mutex},
};

use crate::common::Event;

/// Wrapper function to simplify lambdas
type BoxedError = Box<dyn Error + Sync + Send>;
type BoxedResult = Result<(), BoxedError>;
Expand Down Expand Up @@ -42,3 +44,56 @@ async fn leak_subsystem_handle() {
"The SubsystemHandle object must not be leaked out of the subsystem!"
));
}

#[tokio::test]
#[traced_test]
async fn wait_for_children() {
let (nested1_started, set_nested1_started) = Event::create();
let (nested1_finished, set_nested1_finished) = Event::create();
let (nested2_started, set_nested2_started) = Event::create();
let (nested2_finished, set_nested2_finished) = Event::create();

let nested_subsys2 = move |subsys: SubsystemHandle| async move {
set_nested2_started();
subsys.on_shutdown_requested().await;
sleep(Duration::from_millis(100)).await;
set_nested2_finished();
BoxedResult::Ok(())
};

let nested_subsys1 = move |subsys: SubsystemHandle| async move {
subsys.start(SubsystemBuilder::new("nested2", nested_subsys2));
set_nested1_started();
subsys.on_shutdown_requested().await;
sleep(Duration::from_millis(100)).await;
set_nested1_finished();
BoxedResult::Ok(())
};

let subsys1 = move |mut subsys: SubsystemHandle| async move {
subsys.start(SubsystemBuilder::new("nested1", nested_subsys1));

sleep(Duration::from_millis(100)).await;

subsys.request_shutdown();

assert!(nested1_started.get());
assert!(!nested1_finished.get());
assert!(nested2_started.get());
assert!(!nested2_finished.get());

subsys.wait_for_children().await;

assert!(nested1_finished.get());
assert!(nested2_finished.get());

BoxedResult::Ok(())
};

Toplevel::new(|s| async move {
s.start(SubsystemBuilder::new("subsys", subsys1));
})
.handle_shutdown_requests(Duration::from_millis(500))
.await
.unwrap();
}

0 comments on commit dc2bc9e

Please sign in to comment.