mirror of
https://github.com/PR0M3TH3AN/Marlin.git
synced 2025-09-08 07:08:44 +00:00
Add DB polling helper and update watcher rename tests
This commit is contained in:
@@ -13,9 +13,40 @@ mod tests {
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
// No longer need: use std::path::PathBuf;
|
// No longer need: use std::path::PathBuf;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
|
|
||||||
|
/// Polls the DB until `query` returns `expected` or the timeout elapses.
|
||||||
|
fn wait_for_row_count(
|
||||||
|
marlin: &Marlin,
|
||||||
|
path: &std::path::Path,
|
||||||
|
expected: i64,
|
||||||
|
timeout: Duration,
|
||||||
|
) {
|
||||||
|
let start = Instant::now();
|
||||||
|
loop {
|
||||||
|
let count: i64 = marlin
|
||||||
|
.conn()
|
||||||
|
.query_row(
|
||||||
|
"SELECT COUNT(*) FROM files WHERE path = ?1",
|
||||||
|
[path.to_string_lossy()],
|
||||||
|
|r| r.get(0),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
if count == expected {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if start.elapsed() > timeout {
|
||||||
|
panic!(
|
||||||
|
"Timed out waiting for {} rows for {}",
|
||||||
|
expected,
|
||||||
|
path.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
thread::sleep(Duration::from_millis(50));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_watcher_lifecycle() {
|
fn test_watcher_lifecycle() {
|
||||||
// Create a temp directory for testing
|
// Create a temp directory for testing
|
||||||
@@ -169,13 +200,12 @@ mod tests {
|
|||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(100));
|
||||||
let new_file = dir.join("b.txt");
|
let new_file = dir.join("b.txt");
|
||||||
fs::rename(&file, &new_file).unwrap();
|
fs::rename(&file, &new_file).unwrap();
|
||||||
thread::sleep(Duration::from_millis(1500));
|
wait_for_row_count(&marlin, &new_file, 1, Duration::from_secs(10));
|
||||||
|
watcher.stop().unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
watcher.status().unwrap().events_processed > 0,
|
watcher.status().unwrap().events_processed > 0,
|
||||||
"rename event should be processed"
|
"rename event should be processed"
|
||||||
);
|
);
|
||||||
watcher.stop().unwrap();
|
|
||||||
thread::sleep(Duration::from_millis(100));
|
|
||||||
|
|
||||||
let count: i64 = marlin
|
let count: i64 = marlin
|
||||||
.conn()
|
.conn()
|
||||||
@@ -216,13 +246,15 @@ mod tests {
|
|||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(100));
|
||||||
let new = dir.join("newdir");
|
let new = dir.join("newdir");
|
||||||
fs::rename(&sub, &new).unwrap();
|
fs::rename(&sub, &new).unwrap();
|
||||||
thread::sleep(Duration::from_millis(1500));
|
for fname in ["one.txt", "two.txt"] {
|
||||||
|
let p = new.join(fname);
|
||||||
|
wait_for_row_count(&marlin, &p, 1, Duration::from_secs(10));
|
||||||
|
}
|
||||||
|
watcher.stop().unwrap();
|
||||||
assert!(
|
assert!(
|
||||||
watcher.status().unwrap().events_processed > 0,
|
watcher.status().unwrap().events_processed > 0,
|
||||||
"rename event should be processed"
|
"rename event should be processed"
|
||||||
);
|
);
|
||||||
watcher.stop().unwrap();
|
|
||||||
thread::sleep(Duration::from_millis(100));
|
|
||||||
|
|
||||||
for fname in ["one.txt", "two.txt"] {
|
for fname in ["one.txt", "two.txt"] {
|
||||||
let p = new.join(fname);
|
let p = new.join(fname);
|
||||||
|
Reference in New Issue
Block a user