Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the stored subsystem name via the subsystem handle #98

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/subsystem/subsystem_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ impl<ErrType: ErrTypeTraits> SubsystemHandle<ErrType> {
pub fn create_cancellation_token(&self) -> CancellationToken {
self.inner.cancellation_token.child_token()
}

/// Get the name associated with this subsystem.
///
/// Note that the names of nested subsystems are built unix-path alike,
/// starting and delimited by slashes (e.g. `/a/b/c`).
///
/// See [`SubsystemBuilder::new()`] how to set this name.
pub fn name(&self) -> &str {
&self.inner.name
}
}

impl<ErrType: ErrTypeTraits> Drop for SubsystemHandle<ErrType> {
Expand Down
22 changes: 22 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,25 @@ async fn shutdown_through_signal() {
},
);
}

#[tokio::test]
#[traced_test]
async fn access_name_from_within_subsystem() {
let subsys_nested = move |subsys: SubsystemHandle| async move {
assert_eq!("/subsys_top/subsys_nested", subsys.name());
BoxedResult::Ok(())
};

let subsys_top = move |subsys: SubsystemHandle| async move {
assert_eq!("/subsys_top", subsys.name());
subsys.start(SubsystemBuilder::new("subsys_nested", subsys_nested));
BoxedResult::Ok(())
};

Toplevel::new(move |s| async move {
s.start(SubsystemBuilder::new("subsys_top", subsys_top));
})
.handle_shutdown_requests(Duration::from_millis(100))
.await
.unwrap();
}
Loading