Skip to content

Commit

Permalink
Add CTRL-C test (#3413)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Mar 29, 2024
1 parent 23fdd59 commit 3b240e2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
75 changes: 75 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CTRL-C> 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));
}
}

0 comments on commit 3b240e2

Please sign in to comment.