Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/crates/hive-feed/tests/publish_stall.rs # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / crates / hive-feed / tests / publish_stall.rs` 179 lines ยท 5876 bytes Raw Blame History 1 //! The publisher must not treat a local write as delivery, and a receiver 2 //! that never reads must not freeze the process. 3 use std::fs; 4 use std::io::Read; 5 use std::process::{Command, Stdio}; 6 use std::thread; 7 use std::time::{Duration, Instant}; 8 9 fn bin() -> &'static str { 10 env!("CARGO_BIN_EXE_hive-publish") 11 } 12 13 fn write_ssh(dir: &std::path::Path) { 14 let path = dir.join("ssh"); 15 fs::write( 16 &path, 17 "#!/bin/sh\n\ 18 echo start >> \"$HIVE_FAKE_LOG\"\n\ 19 if [ \"$HIVE_FAKE_MODE\" = hang ]; then\n\ 20 exec sleep 120\n\ 21 fi\n\ 22 if [ \"$HIVE_FAKE_MODE\" = refuse ]; then\n\ 23 echo 'Hive receiver rejected snapshot' >&2\n\ 24 exit 1\n\ 25 fi\n\ 26 if [ \"$HIVE_FAKE_MODE\" = ok ]; then\n\ 27 exec cat >/dev/null\n\ 28 fi\n\ 29 exit 1\n", 30 ) 31 .unwrap(); 32 let mut perm = fs::metadata(&path).unwrap().permissions(); 33 #[cfg(unix)] 34 { 35 use std::os::unix::fs::PermissionsExt; 36 perm.set_mode(0o755); 37 } 38 fs::set_permissions(&path, perm).unwrap(); 39 } 40 41 fn snapshot(path: &std::path::Path, sequence: &str, pad: usize) { 42 let body = "x".repeat(pad); 43 let text = format!("{{\"sequence\":{sequence:?},\"body\":{body:?}}}"); 44 fs::write(path, text).unwrap(); 45 } 46 47 struct Publisher { 48 child: std::process::Child, 49 dir: std::path::PathBuf, 50 } 51 52 impl Publisher { 53 fn start(mode: &str, sequence: &str, pad: usize) -> Self { 54 let dir = std::env::temp_dir().join(format!( 55 "aienosqueue-hive-{}-{}", 56 mode, 57 std::process::id() 58 )); 59 let _ = fs::remove_dir_all(&dir); 60 fs::create_dir_all(&dir).unwrap(); 61 write_ssh(&dir); 62 let snap = dir.join("live.json"); 63 snapshot(&snap, sequence, pad); 64 fs::write(dir.join("key"), b"not-a-key").unwrap(); 65 fs::write(dir.join("known"), b"").unwrap(); 66 let log = dir.join("ssh.log"); 67 fs::write(&log, b"").unwrap(); 68 let mut prefixed = dir.as_os_str().to_owned(); 69 prefixed.push(":"); 70 if let Some(path) = std::env::var_os("PATH") { 71 prefixed.push(path); 72 } 73 let child = Command::new(bin()) 74 .args([ 75 snap.to_str().unwrap(), 76 "edge.example", 77 "hive", 78 dir.join("key").to_str().unwrap(), 79 dir.join("known").to_str().unwrap(), 80 ]) 81 .env("PATH", prefixed) 82 .env("HIVE_FAKE_MODE", mode) 83 .env("HIVE_FAKE_LOG", &log) 84 .stdin(Stdio::null()) 85 .stdout(Stdio::null()) 86 .stderr(Stdio::piped()) 87 .spawn() 88 .unwrap(); 89 Publisher { child, dir } 90 } 91 92 fn stops(&mut self) -> String { 93 let _ = self.child.kill(); 94 let _ = self.child.wait(); 95 let mut buf = String::new(); 96 if let Some(mut err) = self.child.stderr.take() { 97 let _ = err.read_to_string(&mut buf); 98 } 99 buf 100 } 101 } 102 103 impl Drop for Publisher { 104 fn drop(&mut self) { 105 let _ = self.child.kill(); 106 let _ = self.child.wait(); 107 let _ = fs::remove_dir_all(&self.dir); 108 } 109 } 110 111 fn wait_until(mut pred: impl FnMut() -> bool, limit: Duration) -> bool { 112 let start = Instant::now(); 113 while start.elapsed() < limit { 114 if pred() { 115 return true; 116 } 117 thread::sleep(Duration::from_millis(100)); 118 } 119 pred() 120 } 121 122 #[test] 123 fn usage_exits_2() { 124 let status = Command::new(bin()).status().unwrap(); 125 assert_eq!(status.code(), Some(2)); 126 } 127 128 #[test] 129 fn hung_receiver_is_killed_and_retried() { 130 let mut pub_ = Publisher::start("hang", "seq-hang-1", 80_000); 131 let log = pub_.dir.join("ssh.log"); 132 let retried = wait_until( 133 || fs::read_to_string(&log).unwrap_or_default().matches("start").count() >= 2, 134 Duration::from_secs(12), 135 ); 136 let starts = fs::read_to_string(&log).unwrap_or_default().matches("start").count(); 137 let err = pub_.stops(); 138 assert!(retried, "publisher stayed on one hung ssh; log starts={starts}, stderr={err}"); 139 assert!(err.contains("hive-publish: write stalled; reconnecting"), "{err}"); 140 assert!(!err.contains("sent sequence"), "a stalled write must not be recorded as sent: {err}"); 141 assert!(!err.contains("xxxxxxxx"), "snapshot body must not be logged: {err}"); 142 } 143 144 #[test] 145 fn refusing_receiver_is_not_recorded_as_sent() { 146 let mut pub_ = Publisher::start("refuse", "seq-refuse-1", 0); 147 let log = pub_.dir.join("ssh.log"); 148 let retried = wait_until( 149 || fs::read_to_string(&log).unwrap_or_default().matches("start").count() >= 2, 150 Duration::from_secs(8), 151 ); 152 let err = pub_.stops(); 153 assert!(retried, "publisher did not reconnect after refusal; stderr={err}"); 154 assert!( 155 err.contains("receiver refused snapshot") || err.contains("Hive transport interrupted"), 156 "{err}" 157 ); 158 assert!(!err.contains("sent sequence"), "a refusal must not be recorded as sent: {err}"); 159 } 160 161 #[test] 162 fn live_receiver_records_one_send_and_stays_up() { 163 let mut pub_ = Publisher::start("ok", "seq-ok-1", 0); 164 let log = pub_.dir.join("ssh.log"); 165 let up = wait_until( 166 || { 167 fs::read_to_string(&log).unwrap_or_default().matches("start").count() == 1 168 && pub_.child.try_wait().ok().flatten().is_none() 169 }, 170 Duration::from_secs(3), 171 ); 172 assert!(up, "healthy ssh did not stay up"); 173 thread::sleep(Duration::from_secs(2)); 174 let starts = fs::read_to_string(&log).unwrap_or_default().matches("start").count(); 175 let err = pub_.stops(); 176 assert_eq!(starts, 1, "a live receiver must not be torn down; stderr={err}"); 177 assert!(err.contains("hive-publish: sent sequence seq-ok-1"), "{err}"); 178 assert!(!err.contains("write stalled"), "{err}"); 179 }