Skip to content

Commit

Permalink
feat(jstzd): listen to os signals
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 11, 2024
1 parent c1cfe35 commit a44e3cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/jstzd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
}
39 changes: 39 additions & 0 deletions crates/jstzd/tests/main_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
}

0 comments on commit a44e3cd

Please sign in to comment.