Skip to content

Commit 88f1812

Browse files
committed
[add] tests.
1 parent 00d1126 commit 88f1812

File tree

1 file changed

+45
-0
lines changed
  • crates/lambda-rs-logging/src

1 file changed

+45
-0
lines changed

crates/lambda-rs-logging/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,51 @@ mod tests {
502502
assert!(!content.is_empty());
503503
}
504504

505+
#[test]
506+
#[cfg(unix)]
507+
fn file_handler_does_not_reopen_between_flushes() {
508+
use std::{
509+
fs,
510+
time::UNIX_EPOCH,
511+
};
512+
513+
let tmp = std::env::temp_dir();
514+
let base = format!(
515+
"lambda_logging_persist_{}_{}",
516+
std::process::id(),
517+
SystemTime::now()
518+
.duration_since(UNIX_EPOCH)
519+
.unwrap()
520+
.as_nanos()
521+
);
522+
let original = tmp.join(format!("{}.log", base));
523+
let moved = tmp.join(format!("{}_moved.log", base));
524+
525+
let logger = Logger::new(LogLevel::TRACE, "file_reopen");
526+
logger.add_handler(Box::new(crate::handler::FileHandler::new(
527+
original.to_string_lossy().to_string(),
528+
)));
529+
530+
for i in 0..10 {
531+
logger.info(format!("batch1_line{}", i));
532+
}
533+
534+
fs::rename(&original, &moved).expect("rename log file while handler lives");
535+
536+
for i in 0..10 {
537+
logger.info(format!("batch2_line{}", i));
538+
}
539+
540+
assert!(
541+
!original.exists(),
542+
"handler should not reopen and recreate the original path"
543+
);
544+
545+
let content = fs::read_to_string(&moved).expect("moved file must exist");
546+
assert!(content.contains("batch1_line0"));
547+
assert!(content.contains("batch2_line0"));
548+
}
549+
505550
#[test]
506551
fn macro_early_guard_avoids_formatting() {
507552
// Ensure TRACE is disabled by setting level to INFO.

0 commit comments

Comments
 (0)