Skip to content

Commit

Permalink
Expose the stored subsystem name via the subsystem handle (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
girstenbrei authored Jan 10, 2025
1 parent 11c85eb commit d3d8888
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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();
}

0 comments on commit d3d8888

Please sign in to comment.