Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 108 additions & 20 deletions hide-platform/tests/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,37 +451,53 @@ fn one_connect_is_one_accepted_connection() {
/// What the listener's end writes just before it is dropped still arrives
/// whole, however slowly the client reads: the pane bootstrap answers that
/// way, and a Windows pipe closed with unread bytes throws them away.
///
/// The client leaves the last bytes unread until the listener's end is gone.
/// They are fewer than either system buffers (a Windows pipe here is asked
/// for 512), so the write can finish without them being read.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
fn an_answer_written_just_before_the_end_is_dropped_arrives_whole() {
const ANSWER: usize = 256 * 1024;
const UNREAD: usize = 256;
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let (dropped, gone) = mpsc::channel();
let answering = thread::spawn(move || {
let mut stream = listener.accept().unwrap();
stream.write_all(&vec![7_u8; ANSWER]).unwrap();
drop(stream);
dropped.send(()).unwrap();
});
let mut client = LocalStream::connect(&path).unwrap();
thread::sleep(Duration::from_millis(300));
let mut answer = Vec::new();
let mut answer = vec![0_u8; ANSWER - UNREAD];
client.read_exact(&mut answer).unwrap();
gone.recv_timeout(Duration::from_secs(10))
.expect("the listener's end never finished writing");
client.read_to_end(&mut answer).unwrap();
assert_eq!(answer.len(), ANSWER);
answering.join().unwrap();
}

/// The answer is the same when the close lands before the accept blocks, so
/// a slow thread can make this pass without blocking but never fail.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
#[allow(clippy::disallowed_methods)] // time for the other thread to block in the call it frees: no portable state says a thread is inside a system call
fn close_from_another_thread_frees_a_blocked_accept() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let closer = listener.closer();
let (done, finished) = mpsc::channel();
let (accepting, about_to_accept) = mpsc::channel();
thread::spawn(move || {
accepting.send(()).unwrap();
let first = listener.accept().map(drop).map_err(|error| error.kind());
let later = listener.accept().map(drop).map_err(|error| error.kind());
let _ = done.send((first, later));
});
// The accept is waiting well before this wakes it.
about_to_accept
.recv_timeout(Duration::from_secs(10))
.expect("the accepting thread never started");
// Only the last step, from the signal into the accept, is left to time.
thread::sleep(Duration::from_millis(300));
let started = Instant::now();
closer.close();
Expand All @@ -494,14 +510,17 @@ fn close_from_another_thread_frees_a_blocked_accept() {
assert_eq!(later, Err(std::io::ErrorKind::ConnectionAborted));
}

/// The peer stays open and silent until the read has answered, so only the
/// timeout can end the read.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
fn a_read_timeout_fires_after_the_time_and_not_much_later() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let (release, released) = mpsc::channel::<()>();
let silent = thread::spawn(move || {
let stream = listener.accept().unwrap();
thread::sleep(Duration::from_millis(1500));
// Bounded only so a read that never times out ends in a failure.
let _ = released.recv_timeout(Duration::from_secs(10));
drop(stream);
});
let mut client = LocalStream::connect(&path).unwrap();
Expand All @@ -517,18 +536,22 @@ fn a_read_timeout_fires_after_the_time_and_not_much_later() {
"returned early: {waited:?}"
);
assert!(waited < Duration::from_secs(2), "returned late: {waited:?}");
drop(release);
drop(client);
silent.join().unwrap();
}

/// The peer writes only once the client's read has timed out.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
fn a_timeout_does_not_lose_bytes_that_arrive_later() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let (timed_out, heard) = mpsc::channel();
let server = thread::spawn(move || {
let mut stream = listener.accept().unwrap();
thread::sleep(Duration::from_millis(500));
heard
.recv_timeout(Duration::from_secs(10))
.expect("the client's read never timed out");
stream.write_all(b"late\n").unwrap();
});
let mut client = LocalStream::connect(&path).unwrap();
Expand All @@ -539,13 +562,15 @@ fn a_timeout_does_not_lose_bytes_that_arrive_later() {
client.read(&mut [0_u8; 1]).unwrap_err().kind(),
std::io::ErrorKind::TimedOut
);
timed_out.send(()).unwrap();
client.set_read_timeout(None).unwrap();
assert_eq!(read_line(&mut client), "late\n");
server.join().unwrap();
}

/// The peer's end is closed once its thread has joined: a Unix stream closes
/// its descriptor on drop.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
fn a_timeout_can_be_set_after_the_peer_has_closed_and_its_bytes_still_read() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
Expand All @@ -555,7 +580,6 @@ fn a_timeout_can_be_set_after_the_peer_has_closed_and_its_bytes_still_read() {
});
let mut client = LocalStream::connect(&path).unwrap();
server.join().unwrap();
thread::sleep(Duration::from_millis(200));
// macOS refuses `SO_RCVTIMEO` on a socket whose peer has gone.
client
.set_read_timeout(Some(Duration::from_secs(1)))
Expand All @@ -579,26 +603,35 @@ fn a_zero_timeout_is_refused() {
);
}

/// The peer stays open and silent, so only the shutdown can end the read. The
/// answer is the same when the shutdown lands before the read blocks, so a
/// slow thread can make this pass without blocking but never fail.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
#[allow(clippy::disallowed_methods)] // time for the other thread to block in the call it frees: no portable state says a thread is inside a system call
fn shutdown_from_another_thread_frees_a_blocked_read() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let (release, released) = mpsc::channel::<()>();
let held = thread::spawn(move || {
let stream = listener.accept().unwrap();
thread::sleep(Duration::from_secs(10));
let _ = released.recv_timeout(Duration::from_secs(10));
drop(stream);
});
let mut client = LocalStream::connect(&path).unwrap();
let handle = client.shutdown_handle();
let (done, finished) = mpsc::channel();
let (reading, about_to_read) = mpsc::channel();
let reader = thread::spawn(move || {
let mut byte = [0_u8; 1];
reading.send(()).unwrap();
let result = client.read(&mut byte);
let after = client.write(b"x");
let _ = done.send((result.map_err(|error| error.kind()), after.is_err()));
});
// The reader is blocked well before this wakes it.
about_to_read
.recv_timeout(Duration::from_secs(10))
.expect("the reading thread never started");
// Only the last step, from the signal into the read, is left to time.
thread::sleep(Duration::from_millis(300));
let started = Instant::now();
handle.shutdown();
Expand All @@ -609,17 +642,20 @@ fn shutdown_from_another_thread_frees_a_blocked_read() {
assert_eq!(read, Ok(0));
assert!(write_failed, "a write after shutdown must fail");
reader.join().unwrap();
drop(held);
drop(release);
held.join().unwrap();
}

/// As above, with a read timeout far longer than the test.
#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
#[allow(clippy::disallowed_methods)] // time for the other thread to block in the call it frees: no portable state says a thread is inside a system call
fn shutdown_frees_a_read_that_has_a_timeout_too() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let _held = thread::spawn(move || {
let (release, released) = mpsc::channel::<()>();
let held = thread::spawn(move || {
let stream = listener.accept().unwrap();
thread::sleep(Duration::from_secs(10));
let _ = released.recv_timeout(Duration::from_secs(10));
drop(stream);
});
let mut client = LocalStream::connect(&path).unwrap();
Expand All @@ -628,35 +664,87 @@ fn shutdown_frees_a_read_that_has_a_timeout_too() {
.unwrap();
let handle = client.shutdown_handle();
let (done, finished) = mpsc::channel();
let (reading, about_to_read) = mpsc::channel();
thread::spawn(move || {
reading.send(()).unwrap();
let _ = done.send(client.read(&mut [0_u8; 1]).map_err(|error| error.kind()));
});
about_to_read
.recv_timeout(Duration::from_secs(10))
.expect("the reading thread never started");
// Only the last step, from the signal into the read, is left to time.
thread::sleep(Duration::from_millis(300));
handle.shutdown();
assert_eq!(
finished.recv_timeout(Duration::from_secs(5)).unwrap(),
Ok(0)
);
drop(release);
held.join().unwrap();
}

/// The three tests above leave the last step into the blocking call to the
/// scheduler; a close or shutdown that lands before the call must give the
/// same answer, or a slow runner would fail them.
#[test]
fn a_close_before_the_accept_gives_the_answer_a_blocked_accept_gets() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let closer = listener.closer();
closer.close();
closer.close();
let first = listener.accept().map(drop).map_err(|error| error.kind());
let later = listener.accept().map(drop).map_err(|error| error.kind());
assert_eq!(first, Err(std::io::ErrorKind::ConnectionAborted));
assert_eq!(later, Err(std::io::ErrorKind::ConnectionAborted));
}

#[test]
fn a_shutdown_before_the_read_gives_the_answer_a_blocked_read_gets() {
for timeout in [None, Some(Duration::from_secs(30))] {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
let (release, released) = mpsc::channel::<()>();
let held = thread::spawn(move || {
let stream = listener.accept().unwrap();
let _ = released.recv_timeout(Duration::from_secs(10));
drop(stream);
});
let mut client = LocalStream::connect(&path).unwrap();
client.set_read_timeout(timeout).unwrap();
client.shutdown_handle().shutdown();
let read = client.read(&mut [0_u8; 1]).map_err(|error| error.kind());
assert_eq!(read, Ok(0), "timeout {timeout:?}");
assert!(
client.write(b"x").is_err(),
"a write after shutdown must fail"
);
drop(release);
held.join().unwrap();
}
}

#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
fn the_accepted_stream_names_the_connecting_process() {
let (_folder, path) = endpoint();
let listener = LocalListener::bind(&path).unwrap();
// This process owns the listener; the peer is a different one.
let (served, accepted) = mpsc::channel();
let (release, released) = mpsc::channel::<()>();
let server = thread::spawn(move || {
let stream = listener.accept().unwrap();
let _ = served.send(stream.peer_pid());
thread::sleep(Duration::from_millis(500));
// The connection stays open until the test has compared the pid.
let _ = released.recv_timeout(Duration::from_secs(10));
drop(stream);
});
let peer = spawn_role("connect", &path);
let pid = accepted
.recv_timeout(Duration::from_secs(10))
.unwrap()
.expect("this system reports the peer's pid");
assert_eq!(pid, peer.child.id());
drop(release);
server.join().unwrap();
}

Expand Down
17 changes: 14 additions & 3 deletions hide-platform/tests/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ fn only_a_folder_that_exists_can_be_watched() {
}

#[test]
#[allow(clippy::disallowed_methods)] // #437 the sleep stands in for a state the test can wait for
fn a_filtered_watch_reports_only_what_it_keeps_and_a_burst_it_drops_is_not_an_overflow() {
let dir = tempfile::tempdir().unwrap();
let (mut watcher, changes) = Watcher::keeping(|relative| relative.starts_with("refs")).unwrap();
Expand All @@ -181,13 +180,25 @@ fn a_filtered_watch_reports_only_what_it_keeps_and_a_burst_it_drops_is_not_an_ov
drain(&changes);
// More paths than the queue holds, paced so the system's own queue keeps
// up: an overflow there is the system's and no filter can prevent it.
// After every hundred dropped writes a kept one is written and waited
// for, and the system reports in order, so the hundred before it have
// been read by then.
let outside =
|change: &Change| !matches!(change, Change::Path { path, .. } if path.starts_with(&refs));
let mut dropped = Vec::new();
for index in 0..CAPACITY + 500 {
fs::write(objects.join(format!("o{index}")), b"x").unwrap();
if index % 100 == 99 {
std::thread::sleep(Duration::from_millis(20));
let pace = refs.join(format!("pace{index}"));
fs::write(&pace, b"").unwrap();
dropped.extend(
expect_path(&changes, &pace, "a kept path")
.into_iter()
.filter(outside),
);
}
}
let dropped = drain(&changes);
dropped.extend(drain(&changes).into_iter().filter(outside));
assert!(
dropped.is_empty(),
"{} changes outside the filter, first {:?}",
Expand Down
Loading