diff --git a/crates/jstzd/src/lib.rs b/crates/jstzd/src/lib.rs index aeadefc08..0a2c4507d 100644 --- a/crates/jstzd/src/lib.rs +++ b/crates/jstzd/src/lib.rs @@ -8,6 +8,7 @@ pub mod jstz_rollup_path { include!(concat!(env!("OUT_DIR"), "/jstz_rollup_path.rs")); } use std::process::exit; +use tokio::signal::unix::{signal, SignalKind}; include!("../build_config.rs"); pub const JSTZ_ROLLUP_ADDRESS: &str = "sr1PuFMgaRUN12rKQ3J2ae5psNtwCxPNmGNK"; @@ -35,8 +36,14 @@ async fn run(port: u16, config: JstzdConfig) { exit(1); } - server.wait().await; + let mut sigterm = signal(SignalKind::terminate()).unwrap(); + let mut sigint = signal(SignalKind::interrupt()).unwrap(); + tokio::select! { + _ = server.wait() => (), + _ = sigterm.recv() => (), + _ = sigint.recv() => (), + }; println!("Shutting down"); server.stop().await.unwrap(); } diff --git a/crates/jstzd/tests/main_test.rs b/crates/jstzd/tests/main_test.rs index 6dc83b590..f629c5b1e 100644 --- a/crates/jstzd/tests/main_test.rs +++ b/crates/jstzd/tests/main_test.rs @@ -16,6 +16,26 @@ fn unknown_command() { .stderr(predicate::str::contains("unrecognized subcommand \'test\'")); } +#[cfg_attr(feature = "skip-rollup-tests", ignore)] +#[test] +fn default_config() { + // Since the server's port number is unknown when jstzd runs on default config, + // here it's assumed that if the child process is still alive after 10 seconds, + // it means that jstzd successfully launched + let mut child = Command::cargo_bin("jstzd") + .unwrap() + .arg("run") + .spawn() + .unwrap(); + thread::sleep(Duration::from_secs(10)); + assert!(child.try_wait().unwrap().is_none()); + Command::new("kill") + .args(["-s", "TERM", &child.id().to_string()]) + .spawn() + .unwrap(); + assert!(child.wait().is_ok()); +} + #[cfg_attr(feature = "skip-rollup-tests", ignore)] #[test] fn valid_config_file() { @@ -75,3 +95,22 @@ fn bad_config_file() { "should have at least one bootstrap account with at least 6000 tez", )); } + +#[test] +fn terminate_with_sigint() { + // Since the server's port number is unknown when jstzd runs on default config, + // here it's assumed that if the child process is still alive after 10 seconds, + // it means that jstzd successfully launched + let mut child = Command::cargo_bin("jstzd") + .unwrap() + .arg("run") + .spawn() + .unwrap(); + thread::sleep(Duration::from_secs(10)); + assert!(child.try_wait().unwrap().is_none()); + Command::new("kill") + .args(["-s", "INT", &child.id().to_string()]) + .spawn() + .unwrap(); + assert!(child.wait().is_ok()); +}