mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-27 13:52:16 +00:00
tests: keep child-spawning tests apart from lock and FIFO tests
This commit is contained in:
1 parent
48adcf726e
commit
bf7d16fc36
6 files changed
+142
-112
No files matched your search
@@ -293,7 +293,7 @@ mod tests {
|
||||
use super::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn unique_test_dir(label: &str) -> PathBuf {
|
||||
pub(super) fn unique_test_dir(label: &str) -> PathBuf {
|
||||
static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
let id = NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
std::env::temp_dir().join(format!(
|
||||
@@ -347,36 +347,6 @@ mod tests {
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// Re-run `test` in a child process whose files are limited to `limit`
|
||||
/// bytes, with SIGXFSZ ignored so a longer write fails with EFBIG. Returns
|
||||
/// true in the child, which then runs the test body.
|
||||
#[cfg(unix)]
|
||||
pub(super) fn in_file_size_limited_child(test: &str, limit: u64) -> bool {
|
||||
const CHILD: &str = "RNS_FILE_SIZE_LIMITED_CHILD";
|
||||
if std::env::var_os(CHILD).is_some() {
|
||||
unsafe {
|
||||
libc::signal(libc::SIGXFSZ, libc::SIG_IGN);
|
||||
let mut current = std::mem::zeroed::<libc::rlimit>();
|
||||
assert_eq!(libc::getrlimit(libc::RLIMIT_FSIZE, &mut current), 0);
|
||||
current.rlim_cur = limit as libc::rlim_t;
|
||||
assert_eq!(libc::setrlimit(libc::RLIMIT_FSIZE, ¤t), 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
let output = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args([test, "--exact", "--nocapture", "--test-threads=1"])
|
||||
.env(CHILD, "1")
|
||||
.output()
|
||||
.unwrap();
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
output.status.success() && stdout.contains("1 passed"),
|
||||
"file-size-limited child failed:\n{stdout}\n{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
false
|
||||
}
|
||||
|
||||
/// Root ignores directory permissions.
|
||||
#[cfg(unix)]
|
||||
pub(super) fn is_root() -> bool {
|
||||
@@ -441,28 +411,6 @@ mod tests {
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn atomic_write_beyond_the_file_size_limit_keeps_the_previous_file() {
|
||||
if !in_file_size_limited_child(
|
||||
"persistence::tests::atomic_write_beyond_the_file_size_limit_keeps_the_previous_file",
|
||||
4096,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let dir = unique_test_dir("atomic-file-size");
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("state");
|
||||
fs::write(&path, b"previous").unwrap();
|
||||
|
||||
let err = atomic_write(&path, &[0x5a; 16384]).unwrap_err();
|
||||
|
||||
assert_eq!(err.raw_os_error(), Some(libc::EFBIG));
|
||||
assert_eq!(fs::read(&path).unwrap(), b"previous");
|
||||
assert_eq!(fs::read_dir(&dir).unwrap().count(), 1);
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atomic_write_onto_a_directory_leaves_it_and_no_temporary_file() {
|
||||
let dir = unique_test_dir("atomic-directory-target");
|
||||
@@ -614,6 +562,16 @@ mod tests {
|
||||
// A reader that gave up has closed its end.
|
||||
let _ = fifo.write_all(second);
|
||||
drop(fifo);
|
||||
// Fail instead of hanging if another descriptor still holds the writer.
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
||||
while !reader.is_finished() {
|
||||
assert!(
|
||||
std::time::Instant::now() < deadline,
|
||||
"reader of {} saw no EOF 30 s after the writer closed",
|
||||
path.display()
|
||||
);
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
let result = reader.join().unwrap();
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
result
|
||||
|
||||
@@ -35,11 +35,7 @@ pub fn try_lock_exclusive(file: &File) -> io::Result<()> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn lock_files() -> (std::path::PathBuf, File, File) {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"rns-native-lock-{}-{}",
|
||||
std::process::id(),
|
||||
super::super::TEMP_FILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
));
|
||||
let path = super::super::tests::unique_test_dir("native-lock");
|
||||
let first = File::create_new(&path).unwrap();
|
||||
let second = File::options().read(true).write(true).open(&path).unwrap();
|
||||
(path, first, second)
|
||||
|
||||
@@ -930,11 +930,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn dir() -> PathBuf {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"rns-durable-{}-{}",
|
||||
std::process::id(),
|
||||
super::super::TEMP_FILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
));
|
||||
let path = super::super::tests::unique_test_dir("durable");
|
||||
fs::create_dir(&path).unwrap();
|
||||
path
|
||||
}
|
||||
@@ -967,6 +963,7 @@ mod tests {
|
||||
fs::remove_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(local).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1044,31 +1041,6 @@ mod tests {
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn strict_write_beyond_the_file_size_limit_keeps_the_previous_file() {
|
||||
if !super::super::tests::in_file_size_limited_child(
|
||||
"persistence::strict::tests::strict_write_beyond_the_file_size_limit_keeps_the_previous_file",
|
||||
4096,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let root = dir();
|
||||
let target = root.join("state");
|
||||
durable_write(&target, b"old").unwrap();
|
||||
|
||||
let failure = durable_write(&target, &[0x5a; 16384]).unwrap_err();
|
||||
|
||||
assert_eq!(failure.phase, DurableCommitPhase::NotCommitted);
|
||||
let source = std::error::Error::source(&failure)
|
||||
.and_then(|source| source.downcast_ref::<io::Error>())
|
||||
.unwrap();
|
||||
assert_eq!(source.raw_os_error(), Some(libc::EFBIG));
|
||||
assert_eq!(fs::read(&target).unwrap(), b"old");
|
||||
assert_eq!(failure.recovery_paths.len(), 2);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn strict_write_onto_an_immutable_target_is_uncertain_and_recoverable() {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
//! Writes beyond the process file-size limit fail with EFBIG and keep the
|
||||
//! previous file. Each test re-runs itself in a child with a lowered limit.
|
||||
//! Spawning stays out of the unit-test binary: until a child execs it holds
|
||||
//! copies of all its parent's descriptors, close-on-exec ones included, so a
|
||||
//! sibling test's lock or FIFO end would outlive that test's close.
|
||||
#![cfg(unix)]
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use rns_identity::persistence::{DurableCommitPhase, atomic_write, durable_write};
|
||||
|
||||
fn unique_test_dir(label: &str) -> PathBuf {
|
||||
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"reticulum-{label}-{}-{}-{}",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_nanos(),
|
||||
NEXT_ID.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// Re-run `test` in a child process whose files are limited to `limit`
|
||||
/// bytes, with SIGXFSZ ignored so a longer write fails with EFBIG. Returns
|
||||
/// true in the child, which then runs the test body.
|
||||
fn in_file_size_limited_child(test: &str, limit: u64) -> bool {
|
||||
const CHILD: &str = "RNS_FILE_SIZE_LIMITED_CHILD";
|
||||
if std::env::var_os(CHILD).is_some() {
|
||||
unsafe {
|
||||
libc::signal(libc::SIGXFSZ, libc::SIG_IGN);
|
||||
let mut current = std::mem::zeroed::<libc::rlimit>();
|
||||
assert_eq!(libc::getrlimit(libc::RLIMIT_FSIZE, &mut current), 0);
|
||||
current.rlim_cur = limit as libc::rlim_t;
|
||||
assert_eq!(libc::setrlimit(libc::RLIMIT_FSIZE, ¤t), 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
let output = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args([test, "--exact", "--nocapture", "--test-threads=1"])
|
||||
.env(CHILD, "1")
|
||||
.output()
|
||||
.unwrap();
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
assert!(
|
||||
output.status.success() && stdout.contains("1 passed"),
|
||||
"file-size-limited child failed:\n{stdout}\n{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
false
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atomic_write_beyond_the_file_size_limit_keeps_the_previous_file() {
|
||||
if !in_file_size_limited_child(
|
||||
"atomic_write_beyond_the_file_size_limit_keeps_the_previous_file",
|
||||
4096,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let dir = unique_test_dir("atomic-file-size");
|
||||
let path = dir.join("state");
|
||||
fs::write(&path, b"previous").unwrap();
|
||||
|
||||
let err = atomic_write(&path, &[0x5a; 16384]).unwrap_err();
|
||||
|
||||
assert_eq!(err.raw_os_error(), Some(libc::EFBIG));
|
||||
assert_eq!(fs::read(&path).unwrap(), b"previous");
|
||||
assert_eq!(fs::read_dir(&dir).unwrap().count(), 1);
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_write_beyond_the_file_size_limit_keeps_the_previous_file() {
|
||||
if !in_file_size_limited_child(
|
||||
"strict_write_beyond_the_file_size_limit_keeps_the_previous_file",
|
||||
4096,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let root = unique_test_dir("durable-file-size");
|
||||
let target = root.join("state");
|
||||
durable_write(&target, b"old").unwrap();
|
||||
|
||||
let failure = durable_write(&target, &[0x5a; 16384]).unwrap_err();
|
||||
|
||||
assert_eq!(failure.phase, DurableCommitPhase::NotCommitted);
|
||||
let source = std::error::Error::source(&failure)
|
||||
.and_then(|source| source.downcast_ref::<std::io::Error>())
|
||||
.unwrap();
|
||||
assert_eq!(source.raw_os_error(), Some(libc::EFBIG));
|
||||
assert_eq!(fs::read(&target).unwrap(), b"old");
|
||||
assert_eq!(failure.recovery_paths.len(), 2);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
@@ -47,27 +47,3 @@ impl ConfigWriteLock {
|
||||
Ok(Self { _file: file })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn config_lock_excludes_concurrent_writers_and_releases_on_drop() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"rns-config-lock-{}-{:?}",
|
||||
std::process::id(),
|
||||
std::thread::current().id()
|
||||
));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let config = dir.join("config");
|
||||
let lock = ConfigWriteLock::acquire(&config).unwrap();
|
||||
assert_eq!(
|
||||
ConfigWriteLock::acquire(&config).unwrap_err().kind(),
|
||||
io::ErrorKind::WouldBlock
|
||||
);
|
||||
drop(lock);
|
||||
let next = ConfigWriteLock::acquire(&config).unwrap();
|
||||
drop(next);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//! The config write lock excludes a second writer and is released on drop.
|
||||
//! It runs outside the unit-test binary, whose tests spawn processes: until a
|
||||
//! child execs it holds copies of all its parent's descriptors, close-on-exec
|
||||
//! ones included, so a dropped lock would stay held for that window.
|
||||
|
||||
use std::io;
|
||||
|
||||
use rns_runtime::config::ifac_lock::ConfigWriteLock;
|
||||
|
||||
#[test]
|
||||
fn config_lock_excludes_concurrent_writers_and_releases_on_drop() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"rns-config-lock-{}-{:?}",
|
||||
std::process::id(),
|
||||
std::thread::current().id()
|
||||
));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let config = dir.join("config");
|
||||
let lock = ConfigWriteLock::acquire(&config).unwrap();
|
||||
assert_eq!(
|
||||
ConfigWriteLock::acquire(&config).unwrap_err().kind(),
|
||||
io::ErrorKind::WouldBlock
|
||||
);
|
||||
drop(lock);
|
||||
let next = ConfigWriteLock::acquire(&config).unwrap();
|
||||
drop(next);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user