Skip to main content

torrust_server_lib/
signals.rs

1//! This module contains functions to handle signals.
2use tokio_util::sync::CancellationToken;
3use torrust_net_primitives::service_binding::ServiceBinding;
4use tracing::instrument;
5
6/// This is the message that the "launcher" spawned task sends to the main
7/// application process to notify the service was successfully started.
8///
9#[derive(Debug)]
10pub struct Started {
11    pub service_binding: ServiceBinding,
12    pub address: std::net::SocketAddr,
13}
14
15/// Resolves when the injected cancellation token is cancelled.
16///
17/// This function never subscribes to an operating-system signal. The caller
18/// owns the cancellation lifecycle.
19#[instrument(skip(cancellation_token))]
20pub async fn cancellation_signal(cancellation_token: CancellationToken) {
21    cancellation_token.cancelled().await;
22}
23
24#[cfg(test)]
25mod tests {
26    use tokio_util::sync::CancellationToken;
27
28    use super::cancellation_signal;
29
30    #[tokio::test]
31    async fn cancellation_signal_should_resolve_when_the_token_is_cancelled() {
32        let cancellation_token = CancellationToken::new();
33        let wait_task = tokio::spawn(cancellation_signal(cancellation_token.clone()));
34
35        tokio::task::yield_now().await;
36
37        assert!(!wait_task.is_finished());
38
39        cancellation_token.cancel();
40
41        wait_task.await.expect("cancellation wait task should complete");
42    }
43}