From 2dc919a19ddada965cb5bb6f4e0486e2502e3fc4 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 14:56:09 -0700 Subject: [PATCH 1/8] Add CTRL-C test --- Cargo.lock | 1 + Cargo.toml | 1 + tests/server.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) 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..2d4e0ca25a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ mime = "0.3.16" mime_guess = "2.0.4" miniscript = "10.0.0" mp4 = "0.14.0" +nix = "0.28.0" ord-bitcoincore-rpc = "0.17.2" ordinals = { version = "0.0.4", path = "crates/ordinals" } redb = "2.0.0" diff --git a/tests/server.rs b/tests/server.rs index ffd26eab4a..d83230f7e4 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1,4 +1,12 @@ -use {super::*, ciborium::value::Integer, ord::subcommand::wallet::send::Output}; +use { + super::*, + ciborium::value::Integer, + nix::{ + sys::signal::{self, Signal}, + unistd::Pid, + }, + ord::subcommand::wallet::send::Output, +}; #[test] fn run() { @@ -619,3 +627,47 @@ fn authentication() { child.kill().unwrap(); } + +#[test] +fn ctrl_c() { + let rpc_server = test_bitcoincore_rpc::spawn(); + + let port = TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + + let mut spawn = CommandBuilder::new(format!("server --address 127.0.0.1 --http-port {port}")) + .bitcoin_rpc_server(&rpc_server) + .spawn(); + + for attempt in 0.. { + if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/status")) { + if response.status() == 200 { + break; + } + } + + if attempt == 100 { + panic!("Server did not respond to status check",); + } + + thread::sleep(Duration::from_millis(50)); + } + + let pid = Pid::from_raw(spawn.child.id() as i32); + signal::kill(pid, 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(); +} From be9b8954f6f174b3abe2e3f2b6e7964ff6ad6d19 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:14:30 -0700 Subject: [PATCH 2/8] Amend --- Cargo.lock | 1 + Cargo.toml | 1 + tests/server.rs | 32 +++++++++++++++++++++++--------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e24df91011..9aec4e31e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2294,6 +2294,7 @@ dependencies = [ "tower-http", "unindent", "urlencoding", + "winapi", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 2d4e0ca25a..5a21944a03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ tokio-stream = "0.1.9" tokio-util = {version = "0.7.3", features = ["compat"] } tower-http = { version = "0.4.0", features = ["auth", "compression-br", "compression-gzip", "cors", "set-header"] } urlencoding = "2.1.3" +winapi = { version = "0.3.9", features = ["consoleapi"] } [dev-dependencies] criterion = "0.5.1" diff --git a/tests/server.rs b/tests/server.rs index d83230f7e4..1248a9653c 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1,13 +1,14 @@ -use { - super::*, - ciborium::value::Integer, - nix::{ - sys::signal::{self, Signal}, - unistd::Pid, - }, - ord::subcommand::wallet::send::Output, +use {super::*, ciborium::value::Integer, ord::subcommand::wallet::send::Output}; + +#[cfg(unix)] +use nix::{ + sys::signal::{self, Signal}, + unistd::Pid, }; +#[cfg(not(unix))] +use winapi::um::wincon::{GenerateConsoleCtrlEvent, CTRL_C_EVENT}; + #[test] fn run() { let rpc_server = test_bitcoincore_rpc::spawn(); @@ -657,7 +658,20 @@ fn ctrl_c() { } let pid = Pid::from_raw(spawn.child.id() as i32); - signal::kill(pid, Signal::SIGINT).unwrap(); + + #[cfg(unix)] + { + signal::kill(pid, Signal::SIGINT).unwrap(); + } + + #[cfg(windows)] + unsafe { + if GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid) == 0 { + Err(std::io::Error::last_os_error()) + } else { + Ok(()) + } + } let mut buffer = String::new(); BufReader::new(spawn.child.stdout.as_mut().unwrap()) From 9f6a8631868e8defb32e7e79e52f6091c63b80d4 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:15:49 -0700 Subject: [PATCH 3/8] Amend --- tests/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/server.rs b/tests/server.rs index 1248a9653c..ff92ed6f5e 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -6,7 +6,7 @@ use nix::{ unistd::Pid, }; -#[cfg(not(unix))] +#[cfg(windows)] use winapi::um::wincon::{GenerateConsoleCtrlEvent, CTRL_C_EVENT}; #[test] From 4f44e9108bf3d0209e76f13228f40c59e6dcf5cf Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:23:36 -0700 Subject: [PATCH 4/8] Amend --- tests/server.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/server.rs b/tests/server.rs index ff92ed6f5e..b5eb095c8b 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -657,20 +657,20 @@ fn ctrl_c() { thread::sleep(Duration::from_millis(50)); } - let pid = Pid::from_raw(spawn.child.id() as i32); - #[cfg(unix)] { - signal::kill(pid, Signal::SIGINT).unwrap(); + signal::kill(Pid::from_raw(spawn.child.id() as i32), Signal::SIGINT).unwrap(); } #[cfg(windows)] unsafe { - if GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid) == 0 { + let result = if GenerateConsoleCtrlEvent(CTRL_C_EVENT, child.id()) == 0 { Err(std::io::Error::last_os_error()) } else { Ok(()) - } + }; + + result.unwrap(); } let mut buffer = String::new(); From 05c591e95913a82d4047719f1a0f4448e0ec9dcc Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:33:15 -0700 Subject: [PATCH 5/8] Amend --- tests/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/server.rs b/tests/server.rs index b5eb095c8b..6df6c28b2a 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -664,7 +664,7 @@ fn ctrl_c() { #[cfg(windows)] unsafe { - let result = if GenerateConsoleCtrlEvent(CTRL_C_EVENT, child.id()) == 0 { + let result = if GenerateConsoleCtrlEvent(CTRL_C_EVENT, spawn.child.id()) == 0 { Err(std::io::Error::last_os_error()) } else { Ok(()) From 9584bf3a1da14ef4507c377cb06c5333203c0afb Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:42:48 -0700 Subject: [PATCH 6/8] Amend --- Cargo.toml | 1 - tests/server.rs | 20 ++------------------ 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5a21944a03..2d4e0ca25a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,7 +71,6 @@ tokio-stream = "0.1.9" tokio-util = {version = "0.7.3", features = ["compat"] } tower-http = { version = "0.4.0", features = ["auth", "compression-br", "compression-gzip", "cors", "set-header"] } urlencoding = "2.1.3" -winapi = { version = "0.3.9", features = ["consoleapi"] } [dev-dependencies] criterion = "0.5.1" diff --git a/tests/server.rs b/tests/server.rs index 6df6c28b2a..2dfbb6da7f 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -6,9 +6,6 @@ use nix::{ unistd::Pid, }; -#[cfg(windows)] -use winapi::um::wincon::{GenerateConsoleCtrlEvent, CTRL_C_EVENT}; - #[test] fn run() { let rpc_server = test_bitcoincore_rpc::spawn(); @@ -629,6 +626,7 @@ fn authentication() { child.kill().unwrap(); } +#[cfg(unix)] #[test] fn ctrl_c() { let rpc_server = test_bitcoincore_rpc::spawn(); @@ -657,21 +655,7 @@ fn ctrl_c() { thread::sleep(Duration::from_millis(50)); } - #[cfg(unix)] - { - signal::kill(Pid::from_raw(spawn.child.id() as i32), Signal::SIGINT).unwrap(); - } - - #[cfg(windows)] - unsafe { - let result = if GenerateConsoleCtrlEvent(CTRL_C_EVENT, spawn.child.id()) == 0 { - Err(std::io::Error::last_os_error()) - } else { - Ok(()) - }; - - result.unwrap(); - } + 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()) From a1ecb1f4ec236e4f4aeabfc2a122bf384b0d5284 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:51:13 -0700 Subject: [PATCH 7/8] Amend --- Cargo.lock | 1 - Cargo.toml | 2 +- tests/server.rs | 41 +++++++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9aec4e31e2..e24df91011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2294,7 +2294,6 @@ dependencies = [ "tower-http", "unindent", "urlencoding", - "winapi", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 2d4e0ca25a..0abc2ff0f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,6 @@ mime = "0.3.16" mime_guess = "2.0.4" miniscript = "10.0.0" mp4 = "0.14.0" -nix = "0.28.0" ord-bitcoincore-rpc = "0.17.2" ordinals = { version = "0.0.4", path = "crates/ordinals" } redb = "2.0.0" @@ -75,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 2dfbb6da7f..6e3a9f02aa 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1,11 +1,5 @@ use {super::*, ciborium::value::Integer, ord::subcommand::wallet::send::Output}; -#[cfg(unix)] -use nix::{ - sys::signal::{self, Signal}, - unistd::Pid, -}; - #[test] fn run() { let rpc_server = test_bitcoincore_rpc::spawn(); @@ -629,6 +623,11 @@ fn authentication() { #[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") @@ -637,13 +636,18 @@ fn ctrl_c() { .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}/status")) { - if response.status() == 200 { + if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockcount")) { + if response.status() == 200 || response.text().unwrap() == "3".to_string() { break; } } @@ -668,4 +672,25 @@ fn ctrl_c() { ); 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".to_string() { + break; + } + } + + if attempt == 100 { + panic!("Server did not respond to status check",); + } + + thread::sleep(Duration::from_millis(50)); + } } From 78fb30d4749b3dbb560905c48d444c7a1cff9276 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Fri, 29 Mar 2024 15:52:04 -0700 Subject: [PATCH 8/8] Amend --- tests/server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/server.rs b/tests/server.rs index 6e3a9f02aa..85938dabe9 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -647,7 +647,7 @@ fn ctrl_c() { for attempt in 0.. { if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockcount")) { - if response.status() == 200 || response.text().unwrap() == "3".to_string() { + if response.status() == 200 || response.text().unwrap() == *"3" { break; } } @@ -682,7 +682,7 @@ fn ctrl_c() { for attempt in 0.. { if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockcount")) { - if response.status() == 200 || response.text().unwrap() == "3".to_string() { + if response.status() == 200 || response.text().unwrap() == *"3" { break; } }