- Added `RpcError` enum for comprehensive error handling in RPC operations. - Introduced `HealthReporter` and `HealthService` for managing service health statuses. - Implemented health check and watch functionality in the health service. - Created integration tests for health check service, covering various scenarios. - Added unit tests for client options validation and stream functionality. - Established logging infrastructure for terminal and registry components. - Implemented logging lifecycle management and conflict resolution in tests.
161 lines
5.5 KiB
Rust
161 lines
5.5 KiB
Rust
use crate::{LoggingConfig, build_with_writer, parse_level};
|
|
use std::{io, path::Path};
|
|
|
|
fn config(directory: &Path, level: &str, max_files: usize) -> LoggingConfig {
|
|
LoggingConfig {
|
|
level: parse_level(level).unwrap(),
|
|
directory: directory.into(),
|
|
file_prefix: "registry".into(),
|
|
max_files,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn validates_configuration_and_flushes_without_installing_globals() {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
let mut config = LoggingConfig {
|
|
level: parse_level("INFO").unwrap(),
|
|
directory: directory.path().into(),
|
|
file_prefix: "test".into(),
|
|
max_files: 10,
|
|
};
|
|
let (subscriber, guard) = build_with_writer(&config, std::io::sink, false).unwrap();
|
|
tracing::dispatcher::with_default(&subscriber, || {
|
|
tracing::info!("visible-record");
|
|
tracing::debug!("hidden-record");
|
|
});
|
|
drop(guard);
|
|
let path = std::fs::read_dir(directory.path())
|
|
.unwrap()
|
|
.next()
|
|
.unwrap()
|
|
.unwrap()
|
|
.path();
|
|
let text = std::fs::read_to_string(path).unwrap();
|
|
assert_eq!(text.matches("visible-record").count(), 1);
|
|
assert!(!text.contains("hidden-record"));
|
|
for prefix in ["", " ", "..", "../escape", "a\\b", "a:b"] {
|
|
config.file_prefix = prefix.into();
|
|
assert!(config.validate().is_err());
|
|
}
|
|
assert!(parse_level("").is_err());
|
|
assert!(parse_level("verbose").is_err());
|
|
}
|
|
#[test]
|
|
fn rotation_retention_off_and_reopen() {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
for day in 1..=12 {
|
|
std::fs::write(
|
|
directory
|
|
.path()
|
|
.join(format!("registry.2000-01-{day:02}.log")),
|
|
b"old",
|
|
)
|
|
.unwrap();
|
|
}
|
|
let unrelated = directory.path().join("terminal.keep.log");
|
|
std::fs::write(&unrelated, b"keep").unwrap();
|
|
let (subscriber, guard) =
|
|
crate::build_with_writer(&config(directory.path(), "info", 3), io::sink, false).unwrap();
|
|
tracing::dispatcher::with_default(&subscriber, || tracing::info!("first-session"));
|
|
drop(guard);
|
|
let (subscriber, guard) =
|
|
crate::build_with_writer(&config(directory.path(), "info", 3), io::sink, false).unwrap();
|
|
tracing::dispatcher::with_default(&subscriber, || tracing::info!("second-session"));
|
|
drop(guard);
|
|
let (subscriber, guard) =
|
|
crate::build_with_writer(&config(directory.path(), "off", 3), io::sink, false).unwrap();
|
|
tracing::dispatcher::with_default(&subscriber, || tracing::error!("off-hidden"));
|
|
drop(guard);
|
|
let files = std::fs::read_dir(directory.path())
|
|
.unwrap()
|
|
.map(|entry| entry.unwrap().path())
|
|
.filter(|path| {
|
|
path.file_name()
|
|
.unwrap()
|
|
.to_string_lossy()
|
|
.starts_with("registry.")
|
|
})
|
|
.collect::<Vec<_>>();
|
|
assert!(files.len() <= 3);
|
|
assert_eq!(std::fs::read(unrelated).unwrap(), b"keep");
|
|
let text = files
|
|
.iter()
|
|
.map(|path| std::fs::read_to_string(path).unwrap())
|
|
.collect::<String>();
|
|
assert_eq!(text.matches("first-session").count(), 1);
|
|
assert_eq!(text.matches("second-session").count(), 1);
|
|
assert!(!text.contains("off-hidden"));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_logging_configuration_is_rejected() {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
assert!(crate::parse_level("invalid").is_err());
|
|
assert!(
|
|
crate::build_with_writer(&config(directory.path(), "info", 0), io::sink, false).is_err()
|
|
);
|
|
let file = directory.path().join("not-a-directory");
|
|
std::fs::write(&file, b"file").unwrap();
|
|
assert!(crate::build_with_writer(&config(&file, "info", 10), io::sink, false).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn bounded_file_queue_applies_backpressure_without_dropping() {
|
|
use std::io::Write;
|
|
use std::sync::{Arc, Mutex, mpsc};
|
|
use std::time::Duration;
|
|
|
|
struct PausedWriter {
|
|
bytes: Arc<Mutex<Vec<u8>>>,
|
|
entered: mpsc::Sender<()>,
|
|
release: Option<mpsc::Receiver<()>>,
|
|
}
|
|
|
|
impl Write for PausedWriter {
|
|
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
|
if let Some(release) = self.release.take() {
|
|
self.entered.send(()).unwrap();
|
|
release.recv_timeout(Duration::from_secs(3)).unwrap();
|
|
}
|
|
self.bytes.lock().unwrap().extend_from_slice(bytes);
|
|
Ok(bytes.len())
|
|
}
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
let bytes = Arc::new(Mutex::new(Vec::new()));
|
|
let (entered, wait_entered) = mpsc::channel();
|
|
let (release, wait_release) = mpsc::channel();
|
|
let (mut writer, guard) = crate::file_writer(
|
|
PausedWriter {
|
|
bytes: bytes.clone(),
|
|
entered,
|
|
release: Some(wait_release),
|
|
},
|
|
1,
|
|
"test-log-writer",
|
|
);
|
|
writer.write_all(b"first\n").unwrap();
|
|
wait_entered.recv_timeout(Duration::from_secs(1)).unwrap();
|
|
writer.write_all(b"second\n").unwrap();
|
|
let (started, wait_started) = mpsc::channel();
|
|
let (completed, wait_completed) = mpsc::channel();
|
|
let producer = std::thread::spawn(move || {
|
|
started.send(()).unwrap();
|
|
writer.write_all(b"third\n").unwrap();
|
|
completed.send(()).unwrap();
|
|
});
|
|
wait_started.recv_timeout(Duration::from_secs(1)).unwrap();
|
|
let blocked = wait_completed
|
|
.recv_timeout(Duration::from_millis(30))
|
|
.is_err();
|
|
release.send(()).unwrap();
|
|
producer.join().unwrap();
|
|
drop(guard);
|
|
assert!(blocked);
|
|
assert_eq!(&*bytes.lock().unwrap(), b"first\nsecond\nthird\n");
|
|
}
|