//! The log's cap, and the file it actually makes. use super::*; use std::io::Write as _; #[test] fn a_small_log_is_kept_and_a_full_one_starts_again() { assert_eq!(log_start(0), LogStart::Append); assert_eq!(log_start(1), LogStart::Append); assert_eq!(log_start(LOG_CAP_BYTES - 1), LogStart::Append); // ON the cap, not only past it: a log that may sit exactly at the cap for // ever is a cap that never fires. assert_eq!(log_start(LOG_CAP_BYTES), LogStart::Fresh); assert_eq!(log_start(LOG_CAP_BYTES * 4), LogStart::Fresh); } #[test] fn a_run_appends_to_what_the_last_run_wrote() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("session-host.log"); let mut first = open(&path).expect("the log opens when it does not exist yet"); writeln!(first, "first run").expect("a line is written"); drop(first); let mut second = open(&path).expect("the log opens again"); writeln!(second, "second run").expect("a line is written"); drop(second); let said = std::fs::read_to_string(&path).expect("the log reads back"); assert_eq!(said, "first run\nsecond run\n"); } #[test] fn a_log_past_the_cap_starts_again_rather_than_growing_for_ever() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("session-host.log"); std::fs::write(&path, vec![b'x'; LOG_CAP_BYTES as usize + 1]).expect("a big log"); let mut fresh = open(&path).expect("the log opens"); writeln!(fresh, "after the cap").expect("a line is written"); drop(fresh); let said = std::fs::read_to_string(&path).expect("the log reads back"); assert_eq!(said, "after the cap\n"); } #[test] fn the_log_sits_beside_the_socket_in_the_data_directory() { // The whole reason the plist carries no `StandardErrorPath`: this path is // the process's own, so the bundle stays the same bytes on every Mac. let layout = crate::data_dir::Layout::new("/Users/x/CottonDB"); assert_eq!( layout.log(), std::path::PathBuf::from("/Users/x/CottonDB/session-host.log") ); assert_eq!(crate::data_dir::LOG_NAME, "session-host.log"); }