Skip to content

Commit

Permalink
test(signal): last line of coverage but really really now I swear
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkloock committed Jan 7, 2025
1 parent 13a373f commit c44f941
Showing 1 changed file with 31 additions and 44 deletions.
75 changes: 31 additions & 44 deletions crates/signal/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,53 @@ mod tests {
use crate::tests::raise_signal;
use crate::SignalHandler;

struct TestGlobal;

impl GlobalWithoutConfig for TestGlobal {
fn init() -> impl std::future::Future<Output = anyhow::Result<Arc<Self>>> + Send {
std::future::ready(Ok(Arc::new(Self)))
}
}
async fn force_shutdown_two_signals<Global: GlobalWithoutConfig + SignalConfig>() {
let (ctx, handler) = scuffle_context::Context::new();

impl SignalConfig for TestGlobal {}
// Block the global context
let _global_ctx = scuffle_context::Context::global();

#[tokio::test]
async fn default_bootstrap_service() {
let (ctx, handler) = scuffle_context::Context::new();
let svc = SignalSvc;
let global = NoTimeoutTestGlobal::init().await.unwrap();
let global = <Global as GlobalWithoutConfig>::init().await.unwrap();

assert!(svc.enabled(&global).await.unwrap());
let result = tokio::spawn(svc.run(global, ctx));

// Wait for the service to start
tokio::time::sleep(tokio::time::Duration::from_millis(5)).await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

raise_signal(tokio::signal::unix::SignalKind::interrupt());
assert!(result.await.is_ok());
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
raise_signal(tokio::signal::unix::SignalKind::interrupt());

match result.with_timeout(tokio::time::Duration::from_millis(100)).await {
Ok(Ok(Err(e))) => {
assert_eq!(e.to_string(), "received signal, shutting down immediately: SignalKind(2)");
}
_ => panic!("unexpected result"),
}

assert!(handler
.shutdown()
.with_timeout(tokio::time::Duration::from_millis(100))
.await
.is_ok());
}

struct TestGlobal;

impl GlobalWithoutConfig for TestGlobal {
fn init() -> impl std::future::Future<Output = anyhow::Result<Arc<Self>>> + Send {
std::future::ready(Ok(Arc::new(Self)))
}
}

impl SignalConfig for TestGlobal {}

#[tokio::test]
async fn default_bootstrap_service() {
force_shutdown_two_signals::<TestGlobal>().await;
}
struct NoTimeoutTestGlobal;

impl GlobalWithoutConfig for NoTimeoutTestGlobal {
Expand Down Expand Up @@ -157,36 +173,7 @@ mod tests {

#[tokio::test]
async fn bootstrap_service_force_shutdown() {
let (ctx, handler) = scuffle_context::Context::new();

// Block the global context
let _global_ctx = scuffle_context::Context::global();

let svc = SignalSvc;
let global = NoTimeoutTestGlobal::init().await.unwrap();

assert!(svc.enabled(&global).await.unwrap());
let result = tokio::spawn(svc.run(global, ctx));

// Wait for the service to start
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

raise_signal(tokio::signal::unix::SignalKind::interrupt());
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
raise_signal(tokio::signal::unix::SignalKind::interrupt());

match result.with_timeout(tokio::time::Duration::from_millis(100)).await {
Ok(Ok(Err(e))) => {
assert_eq!(e.to_string(), "received signal, shutting down immediately: SignalKind(2)");
}
_ => panic!("unexpected result"),
}

assert!(handler
.shutdown()
.with_timeout(tokio::time::Duration::from_millis(100))
.await
.is_ok());
force_shutdown_two_signals::<NoTimeoutTestGlobal>().await;
}

struct NoSignalsTestGlobal;
Expand Down

0 comments on commit c44f941

Please sign in to comment.