|
| 1 | +use std::{ |
| 2 | + fs, |
| 3 | + io::{Seek, Write}, |
| 4 | + path::Path, |
| 5 | + time::{Duration, SystemTime}, |
| 6 | +}; |
| 7 | + |
| 8 | +use bytes::Bytes; |
| 9 | +use file_source_common::ReadFrom; |
| 10 | +use tokio::time::sleep; |
| 11 | + |
| 12 | +use crate::file_watcher::{FileWatcher, RawLineResult}; |
| 13 | + |
| 14 | +async fn wait_for_mtime_change(path: &Path, previous: SystemTime) { |
| 15 | + for _ in 0..50 { |
| 16 | + if let Ok(modified) = fs::metadata(path).and_then(|meta| meta.modified()) { |
| 17 | + if modified > previous { |
| 18 | + return; |
| 19 | + } |
| 20 | + } |
| 21 | + sleep(Duration::from_millis(20)).await; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Write content at the given byte offset and NUL-pad the rest of the file |
| 26 | +/// up to `size` bytes, simulating how MT4 overwrites its fixed-size log file. |
| 27 | +fn write_at(file: &mut fs::File, size: usize, offset: u64, content: &str) { |
| 28 | + file.set_len(size as u64).expect("set_len failed"); |
| 29 | + file.seek(std::io::SeekFrom::Start(offset)) |
| 30 | + .expect("seek failed"); |
| 31 | + file.write_all(content.as_bytes()) |
| 32 | + .expect("write content failed"); |
| 33 | + file.flush().expect("flush failed"); |
| 34 | +} |
| 35 | + |
| 36 | +async fn drain_nul_padding(watcher: &mut FileWatcher) { |
| 37 | + for _ in 0..5 { |
| 38 | + if let Ok(RawLineResult { raw_line: None, .. }) = watcher.read_line().await { |
| 39 | + if watcher.nul_padding_eof() { |
| 40 | + return; |
| 41 | + } |
| 42 | + } |
| 43 | + sleep(Duration::from_millis(10)).await; |
| 44 | + } |
| 45 | + panic!("did not reach nul_padding_eof within retry limit"); |
| 46 | +} |
| 47 | + |
| 48 | +async fn read_expected_line(watcher: &mut FileWatcher, expected: &'static str, label: &str) { |
| 49 | + match watcher.read_line().await { |
| 50 | + Ok(RawLineResult { |
| 51 | + raw_line: Some(line), |
| 52 | + .. |
| 53 | + }) => assert_eq!(line.bytes, Bytes::from(expected), "{label}"), |
| 54 | + other => panic!("unexpected result for {label}: {other:?}"), |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Simulates the MT4 append-in-place pattern: new lines are written |
| 59 | +/// after existing content, replacing NUL padding, without changing |
| 60 | +/// the file size. The watcher should only read the *new* lines, |
| 61 | +/// not re-read the entire file. |
| 62 | +#[tokio::test] |
| 63 | +async fn reads_only_new_content_on_in_place_append() { |
| 64 | + let dir = tempfile::TempDir::new().expect("could not create tempdir"); |
| 65 | + let path = dir.path().join("nul.log"); |
| 66 | + let mut file = fs::File::create(&path).expect("could not create file"); |
| 67 | + |
| 68 | + // Initial state: "line1\n" followed by NUL padding to 4096 bytes. |
| 69 | + write_at(&mut file, 4096, 0, "line1\n"); |
| 70 | + |
| 71 | + let mut watcher = FileWatcher::new( |
| 72 | + path.clone(), |
| 73 | + ReadFrom::Beginning, |
| 74 | + None, |
| 75 | + 100_000, |
| 76 | + Bytes::from("\n"), |
| 77 | + ) |
| 78 | + .await |
| 79 | + .expect("must create watcher"); |
| 80 | + |
| 81 | + read_expected_line(&mut watcher, "line1", "initial read").await; |
| 82 | + drain_nul_padding(&mut watcher).await; |
| 83 | + |
| 84 | + // MT4 appends "line2\n" right after "line1\n" (at byte offset 6), |
| 85 | + // file size stays 4096. |
| 86 | + let previous_mtime = fs::metadata(&path) |
| 87 | + .and_then(|meta| meta.modified()) |
| 88 | + .expect("mtime missing"); |
| 89 | + |
| 90 | + write_at(&mut file, 4096, 6, "line2\n"); |
| 91 | + wait_for_mtime_change(&path, previous_mtime).await; |
| 92 | + |
| 93 | + // The watcher should pick up "line2" without re-emitting "line1". |
| 94 | + read_expected_line(&mut watcher, "line2", "appended line").await; |
| 95 | +} |
| 96 | + |
| 97 | +/// Simulates a full rewrite of the file (e.g. MT4 rotating/replacing |
| 98 | +/// the entire content). The watcher should re-read from the start of |
| 99 | +/// the new content. |
| 100 | +#[tokio::test] |
| 101 | +async fn rereads_on_full_rewrite() { |
| 102 | + let dir = tempfile::TempDir::new().expect("could not create tempdir"); |
| 103 | + let path = dir.path().join("nul.log"); |
| 104 | + let mut file = fs::File::create(&path).expect("could not create file"); |
| 105 | + |
| 106 | + write_at(&mut file, 4096, 0, "old1\n"); |
| 107 | + |
| 108 | + let mut watcher = FileWatcher::new( |
| 109 | + path.clone(), |
| 110 | + ReadFrom::Beginning, |
| 111 | + None, |
| 112 | + 100_000, |
| 113 | + Bytes::from("\n"), |
| 114 | + ) |
| 115 | + .await |
| 116 | + .expect("must create watcher"); |
| 117 | + |
| 118 | + read_expected_line(&mut watcher, "old1", "initial read").await; |
| 119 | + drain_nul_padding(&mut watcher).await; |
| 120 | + |
| 121 | + // Full rewrite from offset 0 (file truncated and re-padded). |
| 122 | + let previous_mtime = fs::metadata(&path) |
| 123 | + .and_then(|meta| meta.modified()) |
| 124 | + .expect("mtime missing"); |
| 125 | + |
| 126 | + file.set_len(0).expect("truncate failed"); |
| 127 | + write_at(&mut file, 4096, 0, "new1\n"); |
| 128 | + wait_for_mtime_change(&path, previous_mtime).await; |
| 129 | + |
| 130 | + // Because the file size changed (truncated then re-created), |
| 131 | + // should_rewind is false and the watcher stays at its old |
| 132 | + // position — it will see NULs or whatever is at that offset |
| 133 | + // in the rewritten file. For a true rotation the file_server |
| 134 | + // layer would detect the inode change. Here we just verify |
| 135 | + // no panic / hang occurs and the watcher eventually reaches |
| 136 | + // NUL-padding EOF again. |
| 137 | + drain_nul_padding(&mut watcher).await; |
| 138 | +} |
| 139 | + |
| 140 | +/// Multiple consecutive appends should each yield only the newly |
| 141 | +/// added lines. |
| 142 | +#[tokio::test] |
| 143 | +async fn consecutive_appends_read_incrementally() { |
| 144 | + let dir = tempfile::TempDir::new().expect("could not create tempdir"); |
| 145 | + let path = dir.path().join("nul.log"); |
| 146 | + let mut file = fs::File::create(&path).expect("could not create file"); |
| 147 | + |
| 148 | + write_at(&mut file, 4096, 0, "A\n"); |
| 149 | + |
| 150 | + let mut watcher = FileWatcher::new( |
| 151 | + path.clone(), |
| 152 | + ReadFrom::Beginning, |
| 153 | + None, |
| 154 | + 100_000, |
| 155 | + Bytes::from("\n"), |
| 156 | + ) |
| 157 | + .await |
| 158 | + .expect("must create watcher"); |
| 159 | + |
| 160 | + read_expected_line(&mut watcher, "A", "line A").await; |
| 161 | + drain_nul_padding(&mut watcher).await; |
| 162 | + |
| 163 | + // Append B at offset 2 ("A\n" = 2 bytes). |
| 164 | + let mtime1 = fs::metadata(&path) |
| 165 | + .and_then(|meta| meta.modified()) |
| 166 | + .expect("mtime missing"); |
| 167 | + write_at(&mut file, 4096, 2, "B\n"); |
| 168 | + wait_for_mtime_change(&path, mtime1).await; |
| 169 | + |
| 170 | + read_expected_line(&mut watcher, "B", "line B").await; |
| 171 | + drain_nul_padding(&mut watcher).await; |
| 172 | + |
| 173 | + // Append C at offset 4 ("A\nB\n" = 4 bytes). |
| 174 | + let mtime2 = fs::metadata(&path) |
| 175 | + .and_then(|meta| meta.modified()) |
| 176 | + .expect("mtime missing"); |
| 177 | + write_at(&mut file, 4096, 4, "C\n"); |
| 178 | + wait_for_mtime_change(&path, mtime2).await; |
| 179 | + |
| 180 | + read_expected_line(&mut watcher, "C", "line C").await; |
| 181 | +} |
0 commit comments