From 3b240e2f4c31b77a825cb27f2a8e6af437c65761 Mon Sep 17 00:00:00 2001 From: raph Date: Fri, 29 Mar 2024 23:56:52 +0100 Subject: [PATCH] Add CTRL-C test (#3413) --- Cargo.lock | 1 + Cargo.toml | 1 + tests/server.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 81414c6963..e24df91011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2267,6 +2267,7 @@ dependencies = [ "mime_guess", "miniscript", "mp4", + "nix", "ord-bitcoincore-rpc", "ordinals", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 926638fcd2..0abc2ff0f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,7 @@ urlencoding = "2.1.3" [dev-dependencies] criterion = "0.5.1" executable-path = "1.0.0" +nix = "0.28.0" pretty_assertions = "1.2.1" reqwest = { version = "0.11.10", features = ["blocking", "brotli", "json"] } test-bitcoincore-rpc = { path = "crates/test-bitcoincore-rpc" } diff --git a/tests/server.rs b/tests/server.rs index ffd26eab4a..85938dabe9 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -619,3 +619,78 @@ fn authentication() { child.kill().unwrap(); } + +#[cfg(unix)] +#[test] +fn ctrl_c() { + use nix::{ + sys::signal::{self, Signal}, + unistd::Pid, + }; + + let rpc_server = test_bitcoincore_rpc::spawn(); + + let port = TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + + let tempdir = Arc::new(TempDir::new().unwrap()); + + rpc_server.mine_blocks(3); + + let mut spawn = CommandBuilder::new(format!("server --address 127.0.0.1 --http-port {port}")) + .temp_dir(tempdir.clone()) + .bitcoin_rpc_server(&rpc_server) + .spawn(); + + for attempt in 0.. { + if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockcount")) { + if response.status() == 200 || response.text().unwrap() == *"3" { + break; + } + } + + if attempt == 100 { + panic!("Server did not respond to status check",); + } + + thread::sleep(Duration::from_millis(50)); + } + + signal::kill(Pid::from_raw(spawn.child.id() as i32), Signal::SIGINT).unwrap(); + + let mut buffer = String::new(); + BufReader::new(spawn.child.stdout.as_mut().unwrap()) + .read_line(&mut buffer) + .unwrap(); + + assert_eq!( + buffer, + "Shutting down gracefully. Press again to shutdown immediately.\n" + ); + + spawn.child.wait().unwrap(); + + CommandBuilder::new(format!( + "server --no-sync --address 127.0.0.1 --http-port {port}" + )) + .temp_dir(tempdir) + .bitcoin_rpc_server(&rpc_server) + .spawn(); + + for attempt in 0.. { + if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockcount")) { + if response.status() == 200 || response.text().unwrap() == *"3" { + break; + } + } + + if attempt == 100 { + panic!("Server did not respond to status check",); + } + + thread::sleep(Duration::from_millis(50)); + } +}