Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jstzd): listen to os signals #686

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
Loading