oxedyne/fe2o3/fe2o3_core/tests/stop_signal.rs
5.4 KiB, 1 run
created by r1870400018:65718, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A real signal, sent to a real process, and what `stop::on_stop_request` |
| 2 | //! makes of it. |
| 3 | //! |
| 4 | //! Everything else that touches `stop.rs` asks the code a question and reads |
| 5 | //! the code's own answer; `test_a_listener_installs_once_00` in the module |
| 6 | //! itself is exactly that. This file asks the operating system instead, because |
| 7 | //! the claim under test -- that `SIGINT`, `SIGTERM` and `SIGHUP` each reach the |
| 8 | //! closure as the right [`Stop`](oxedyne_fe2o3_core::stop::Stop) variant -- can |
| 9 | //! only be shown by a real signal crossing a real process boundary. A test |
| 10 | //! cannot send itself a signal and go on being that test: the closure would run |
| 11 | //! in whichever test binary happened to share the process. So the process under |
| 12 | //! signal is `stop_signal_child`, a binary of a few lines (`src/bin/`), and the |
| 13 | //! signal is sent by `kill`, the operating system's own tool, exactly as |
| 14 | //! `fe2o3_steel/tests/stopping_signal.rs` does for the same reason. |
| 15 | //! |
| 16 | //! Unix only: Windows has no `kill` and no signals, and the console events the |
| 17 | //! same listener answers there cannot be sent from a test. |
| 18 | |
| 19 | #![cfg(unix)] |
| 20 | |
| 21 | use oxedyne_fe2o3_core::prelude::*; |
| 22 | |
| 23 | use std::{ |
| 24 | io::Read, |
| 25 | process::{ |
| 26 | Child, |
| 27 | Command, |
| 28 | Stdio, |
| 29 | }, |
| 30 | time::{ |
| 31 | Duration, |
| 32 | Instant, |
| 33 | }, |
| 34 | }; |
| 35 | |
| 36 | const WAIT_SECS: u64 = 10; |
| 37 | |
| 38 | /// Starts the child and waits until it has had a moment to install its |
| 39 | /// listener. |
| 40 | /// |
| 41 | /// The child prints nothing until it hears a signal, so there is no readiness |
| 42 | /// line to wait on. A fixed pause is used instead: the listener installs |
| 43 | /// before `main` does anything else observable, and giving it two hundred |
| 44 | /// milliseconds costs far less than a pipe protocol just to carry one "ready". |
| 45 | fn spawn_child() -> Outcome<Child> { |
| 46 | let exe = env!("CARGO_BIN_EXE_stop_signal_child"); |
| 47 | let child = res!(Command::new(exe) |
| 48 | .stdout(Stdio::piped()) |
| 49 | .stderr(Stdio::piped()) |
| 50 | .spawn(), IO, File); |
| 51 | std::thread::sleep(Duration::from_millis(200)); |
| 52 | Ok(child) |
| 53 | } |
| 54 | |
| 55 | /// Sends one signal to one process, by pid, using the operating system's own |
| 56 | /// tool. |
| 57 | /// |
| 58 | /// `kill` rather than anything in this program: sending a signal from Rust |
| 59 | /// means `libc::kill`, an `extern "C"` call that would need an `unsafe` block |
| 60 | /// in a crate that forbids them -- and the external tool is also the better |
| 61 | /// oracle, since it is what a person at a terminal or a service manager would |
| 62 | /// use, rather than this crate agreeing with itself. |
| 63 | fn send(signal: &str, child: &Child) -> Outcome<()> { |
| 64 | let out = res!(Command::new("kill") |
| 65 | .arg(fmt!("-{}", signal)) |
| 66 | .arg(fmt!("{}", child.id())) |
| 67 | .output(), IO, File); |
| 68 | if !out.status.success() { |
| 69 | return Err(err!( |
| 70 | "kill -{} {} answered {:?}: {}", |
| 71 | signal, child.id(), out.status, |
| 72 | String::from_utf8_lossy(&out.stderr).trim(); |
| 73 | Test, System)); |
| 74 | } |
| 75 | Ok(()) |
| 76 | } |
| 77 | |
| 78 | /// Waits for the child to exit, then reads back what it printed and how. |
| 79 | fn wait_and_read(mut child: Child) -> Outcome<(Option<i32>, String)> { |
| 80 | let began = Instant::now(); |
| 81 | let status = loop { |
| 82 | match child.try_wait() { |
| 83 | Ok(Some(status)) => break status, |
| 84 | Ok(None) => { |
| 85 | if began.elapsed() >= Duration::from_secs(WAIT_SECS) { |
| 86 | return Err(err!( |
| 87 | "stop_signal_child was still running {} seconds \ |
| 88 | after being signalled.", WAIT_SECS; Test, Timeout)); |
| 89 | } |
| 90 | std::thread::sleep(Duration::from_millis(50)); |
| 91 | }, |
| 92 | Err(e) => return Err(err!(e, |
| 93 | "Could not poll the child."; Test, IO)), |
| 94 | } |
| 95 | }; |
| 96 | let mut out = String::new(); |
| 97 | if let Some(mut stdout) = child.stdout.take() { |
| 98 | let _ = stdout.read_to_string(&mut out); |
| 99 | } |
| 100 | Ok((status.code(), out)) |
| 101 | } |
| 102 | |
| 103 | /// One signal, and the exact word the listener told the child about it. |
| 104 | /// |
| 105 | /// This is the whole claim in one function, so `SIGINT`, `SIGTERM` and |
| 106 | /// `SIGHUP` are held to exactly the same standard rather than three slightly |
| 107 | /// different ones: the process exits by its own choice (a code, not a |
| 108 | /// felling), and it names the ask it heard, correctly. |
| 109 | fn a_signal_is_heard_and_named(signal: &str, expect: &str) -> Outcome<()> { |
| 110 | let child = res!(spawn_child()); |
| 111 | res!(send(signal, &child)); |
| 112 | let (code, out) = res!(wait_and_read(child)); |
| 113 | req!(code, Some(0), |
| 114 | "stop_signal_child did not exit cleanly after {}: it said {:?}. An \ |
| 115 | exit with no code at all means the signal felled it rather than being \ |
| 116 | caught, which is the fault this listener exists to fix.", |
| 117 | signal, out); |
| 118 | req!(out.trim(), expect, |
| 119 | "stop_signal_child was sent {} but named a different ask: {:?}", |
| 120 | signal, out); |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | /// Ctrl-C, told apart from the other two. |
| 125 | #[test] |
| 126 | fn test_an_interrupt_is_heard_as_interrupt_00() -> Outcome<()> { |
| 127 | a_signal_is_heard_and_named("INT", "Interrupt") |
| 128 | } |
| 129 | |
| 130 | /// What a service manager and every reboot send. |
| 131 | #[test] |
| 132 | fn test_a_terminate_is_heard_as_terminate_01() -> Outcome<()> { |
| 133 | a_signal_is_heard_and_named("TERM", "Terminate") |
| 134 | } |
| 135 | |
| 136 | /// The controlling terminal going away, or `kill -HUP` -- the ask this unit |
| 137 | /// added. Before it, `on_stop_request` could not hear this signal at all, and |
| 138 | /// a program relying on it alone (as `ore edit`'s final mark does) would be |
| 139 | /// killed outright rather than asked. |
| 140 | #[test] |
| 141 | fn test_a_hangup_is_heard_as_hangup_02() -> Outcome<()> { |
| 142 | a_signal_is_heard_and_named("HUP", "Hangup") |
| 143 | } |