use core::future::Future;
use core::time::Duration;
use std::io::{Read as _, Write as _};
use std::net::{TcpListener, TcpStream};
use embedded_io_async::{Read, Write};
use super::blocking::past_signals;
use super::BlockingTcp;
use crate::transport::{ReadCancelSafety, Splittable};
fn block_on<F: Future>(fut: F) -> F::Output {
let mut fut = core::pin::pin!(fut);
let mut cx = core::task::Context::from_waker(core::task::Waker::noop());
match fut.as_mut().poll(&mut cx) {
core::task::Poll::Ready(v) => v,
core::task::Poll::Pending => panic!("a blocking transport never parks"),
}
}
fn connected() -> (TcpStream, TcpStream) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let host = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
let (device, _) = listener.accept().unwrap();
(host, device)
}
#[test]
fn the_halves_of_a_split_share_one_connection() {
let (host, mut device) = connected();
let (mut rx, mut tx) = BlockingTcp::new(host).split().unwrap();
block_on(tx.write_all(b"ping")).unwrap();
block_on(tx.flush()).unwrap();
let mut got = [0u8; 4];
device.read_exact(&mut got).unwrap();
assert_eq!(&got, b"ping");
device.write_all(b"pong").unwrap();
let mut got = [0u8; 4];
block_on(rx.read_exact(&mut got)).unwrap();
assert_eq!(&got, b"pong");
}
#[test]
fn wrapping_a_socket_leaves_its_options_alone() {
let (host, _device) = connected();
host.set_nodelay(false).unwrap();
let tcp = BlockingTcp::new(host);
assert!(!tcp.get_ref().nodelay().unwrap());
tcp.get_ref().set_nodelay(true).unwrap();
assert!(
tcp.get_ref().nodelay().unwrap(),
"get_ref is the socket itself"
);
}
#[test]
fn a_read_is_not_raced_against_an_interrupt() {
let (host, _device) = connected();
assert!(!BlockingTcp::new(host).read_cancel_safe());
}
#[test]
fn a_call_is_made_again_for_a_signal_and_for_nothing_else() {
use std::io::{Error, ErrorKind};
let mut calls = 0;
let done = past_signals(|| {
calls += 1;
match calls {
1 | 2 => Err(Error::from(ErrorKind::Interrupted)),
_ => Ok(calls),
}
});
assert_eq!(done.unwrap(), 3);
for kind in [
ErrorKind::WouldBlock,
ErrorKind::TimedOut,
ErrorKind::ConnectionReset,
] {
let mut calls = 0;
let failed = past_signals(|| -> std::io::Result<()> {
calls += 1;
Err(Error::from(kind))
});
assert_eq!(failed.unwrap_err().kind(), kind);
assert_eq!(calls, 1, "{kind:?} is an answer, not an interruption");
}
}
#[test]
fn a_timeout_still_ends_a_read() {
let (host, _device) = connected();
host.set_read_timeout(Some(Duration::from_millis(20)))
.unwrap();
let mut tcp = BlockingTcp::new(host);
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let mut got = [0u8; 4];
let _ = tx.send(block_on(tcp.read(&mut got)).map_err(|e| e.kind()));
});
let read = rx
.recv_timeout(Duration::from_secs(10))
.expect("the timeout did not end the read");
assert!(
matches!(
read,
Err(std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut)
),
"got {read:?}"
);
}
#[cfg(unix)]
#[allow(unsafe_code)]
mod real_signals {
use alloc::vec;
use core::time::Duration;
use std::io::{Read as _, Write as _};
use std::sync::{mpsc, Once};
use std::thread;
use embedded_io_async::{Read, Write};
use super::{block_on, connected, BlockingTcp};
const SIGNAL: libc::c_int = libc::SIGUSR2;
extern "C" fn does_nothing(_signal: libc::c_int) {}
fn install_handler() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
unsafe {
let mut action: libc::sigaction = core::mem::zeroed();
action.sa_sigaction = does_nothing as extern "C" fn(libc::c_int) as usize;
assert_eq!(libc::sigemptyset(&mut action.sa_mask), 0);
assert_eq!(
libc::sigaction(SIGNAL, &action, core::ptr::null_mut()),
0,
"{}",
std::io::Error::last_os_error()
);
}
});
}
fn interrupt(thread: libc::pthread_t) {
for _ in 0..25 {
assert_eq!(unsafe { libc::pthread_kill(thread, SIGNAL) }, 0);
thread::sleep(Duration::from_millis(2));
}
}
fn report_this_thread(tx: mpsc::Sender<libc::pthread_t>) {
tx.send(unsafe { libc::pthread_self() }).unwrap();
}
#[test]
fn a_signal_does_not_end_a_blocked_read() {
install_handler();
let (host, mut device) = connected();
let mut tcp = BlockingTcp::new(host);
let (tx, rx) = mpsc::channel();
let reading = thread::spawn(move || {
report_this_thread(tx);
let mut got = [0u8; 4];
let n = block_on(tcp.read(&mut got))?;
std::io::Result::Ok(got[..n].to_vec())
});
interrupt(rx.recv().unwrap());
device.write_all(b"pong").unwrap();
assert_eq!(reading.join().unwrap().unwrap(), b"pong");
}
#[test]
fn a_signal_does_not_end_a_blocked_write() {
install_handler();
let (host, mut device) = connected();
let mut sent = 0;
host.set_nonblocking(true).unwrap();
loop {
match (&host).write(&[0xA5; 64 * 1024]) {
Ok(n) => sent += n,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
Err(e) => panic!("filling the socket: {e}"),
}
}
host.set_nonblocking(false).unwrap();
let mut tcp = BlockingTcp::new(host);
let (tx, rx) = mpsc::channel();
let writing = thread::spawn(move || {
report_this_thread(tx);
block_on(tcp.write(&[0x5A]))
});
interrupt(rx.recv().unwrap());
let mut drained = vec![0u8; sent + 1];
device.read_exact(&mut drained).unwrap();
assert_eq!(writing.join().unwrap().unwrap(), 1);
assert_eq!(drained.last(), Some(&0x5A));
}
}