Skip to content

Commit 08919cf

Browse files
committed
fix(pty_proxy): improve write retry test reliability with deadline-based polling
Signed-off-by: Luke Hinds <lukehinds@gmail.com>
1 parent fffc881 commit 08919cf

1 file changed

Lines changed: 40 additions & 10 deletions

File tree

crates/nono-cli/src/pty_proxy.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,8 +2185,7 @@ mod tests {
21852185
fn write_all_fd_retries_after_would_block() {
21862186
let (reader, mut writer) = UnixStream::pair().expect("socket pair");
21872187
assert!(super::set_nonblocking(writer.as_raw_fd()));
2188-
let (drained_tx, drained_rx) = std::sync::mpsc::channel();
2189-
let (release_tx, release_rx) = std::sync::mpsc::channel();
2188+
let (result_tx, result_rx) = std::sync::mpsc::channel();
21902189

21912190
let fill_buf = vec![b'x'; 8192];
21922191
loop {
@@ -2201,17 +2200,48 @@ mod tests {
22012200
let reader_thread = thread::spawn(move || {
22022201
thread::sleep(Duration::from_millis(50));
22032202
let mut reader = reader;
2204-
let mut drained = vec![0u8; 16 * 1024];
2205-
let _ = reader.read(&mut drained);
2206-
let _ = drained_tx.send(());
2207-
let _ = release_rx.recv();
2203+
reader
2204+
.set_read_timeout(Some(Duration::from_millis(100)))
2205+
.expect("set read timeout");
2206+
let deadline = std::time::Instant::now() + Duration::from_secs(5);
2207+
let mut buf = [0u8; 16 * 1024];
2208+
let mut saw_ok = false;
2209+
2210+
while std::time::Instant::now() < deadline {
2211+
match reader.read(&mut buf) {
2212+
Ok(0) => break,
2213+
Ok(n) => {
2214+
if buf[..n].windows(2).any(|window| window == b"ok") {
2215+
saw_ok = true;
2216+
break;
2217+
}
2218+
}
2219+
Err(err)
2220+
if err.kind() == std::io::ErrorKind::WouldBlock
2221+
|| err.kind() == std::io::ErrorKind::TimedOut =>
2222+
{
2223+
continue;
2224+
}
2225+
Err(err) => {
2226+
let _ = result_tx.send(Err(format!("reader failed: {err}")));
2227+
return;
2228+
}
2229+
}
2230+
}
2231+
2232+
let _ = result_tx.send(if saw_ok {
2233+
Ok(())
2234+
} else {
2235+
Err("reader never observed retried write".to_string())
2236+
});
22082237
});
22092238

22102239
write_all_fd(writer.as_raw_fd(), b"ok").expect("write_all_fd should retry");
2211-
drained_rx
2212-
.recv_timeout(Duration::from_secs(1))
2213-
.expect("reader should drain buffer before closing");
2214-
let _ = release_tx.send(());
2240+
match result_rx.recv_timeout(Duration::from_secs(5)) {
2241+
Ok(Ok(())) => {}
2242+
Ok(Err(err)) => panic!("{err}"),
2243+
Err(err) => panic!("reader thread timed out: {err}"),
2244+
}
22152245
reader_thread.join().expect("reader thread");
22162246
}
22172247

0 commit comments

Comments
 (0)