Skip to content

Commit

Permalink
Add request_local_shutdown test, make wait_for_children non-mut
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Oct 22, 2023
1 parent dc2bc9e commit 2ff97e2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/subsystem/subsystem_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<ErrType: ErrTypeTraits> SubsystemHandle<ErrType> {
}

/// Waits until all the children of this subsystem are finished.
pub async fn wait_for_children(&mut self) {
pub async fn wait_for_children(&self) {
self.inner.joiner_token.join_children().await
}

Expand Down
4 changes: 1 addition & 3 deletions src/utils/joiner_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ impl<ErrType: ErrTypeTraits> JoinerToken<ErrType> {
(Self { inner }, weak_ref)
}

// Requires `mut` access to prevent children from being spawned
// while waiting
pub(crate) async fn join_children(&mut self) {
pub(crate) async fn join_children(&self) {
let mut subscriber = self.inner.counter.subscribe();

// Ignore errors; if the channel got closed, that definitely means
Expand Down
63 changes: 62 additions & 1 deletion tests/integration_test_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn wait_for_children() {
BoxedResult::Ok(())
};

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

sleep(Duration::from_millis(100)).await;
Expand All @@ -97,3 +97,64 @@ async fn wait_for_children() {
.await
.unwrap();
}

#[tokio::test]
#[traced_test]
async fn request_local_shutdown() {
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 (global_finished, set_global_finished) = Event::create();

let nested_subsys2 = move |subsys: SubsystemHandle| async move {
set_nested2_started();
subsys.on_shutdown_requested().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;
set_nested1_finished();
BoxedResult::Ok(())
};

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

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

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

subsys.request_local_shutdown();
sleep(Duration::from_millis(200)).await;

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

subsys.request_shutdown();
sleep(Duration::from_millis(50)).await;

assert!(global_finished.get());

BoxedResult::Ok(())
};

Toplevel::new(move |s| async move {
s.start(SubsystemBuilder::new("subsys", subsys1));

s.on_shutdown_requested().await;
set_global_finished();
})
.handle_shutdown_requests(Duration::from_millis(100))
.await
.unwrap();
}

0 comments on commit 2ff97e2

Please sign in to comment.