Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions crates/data/src/source/shp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,12 +971,32 @@ fn a_shapefile_says_whether_it_is_as_it_was_opened() {
cache::with_index_dir(tmp.path().join("cache"), || {
let s = ShpSource::open_path(&path, "pts").unwrap();
assert!(s.same_as_opened().unwrap(), "as opened");
// The same size, its time set back: the change time still moves.
// The same size, its time set back: the change time still moves. It moves with the
// system's coarse clock, so a write in the tick the file was opened in leaves it as it
// was: written again until it has moved (a few milliseconds).
let dbf = path.with_extension("dbf");
let modified = std::fs::metadata(&dbf).unwrap().modified().unwrap();
let bytes = std::fs::read(&dbf).unwrap();
std::fs::write(&dbf, &bytes).unwrap();
std::fs::File::options().write(true).open(&dbf).unwrap().set_modified(modified).unwrap();
#[cfg(unix)]
let changed_at = |p: &std::path::Path| {
use std::os::unix::fs::MetadataExt;
let m = std::fs::metadata(p).unwrap();
(m.ctime(), m.ctime_nsec())
};
#[cfg(unix)]
let opened_at = changed_at(&dbf);
let rewrite = || {
std::fs::write(&dbf, &bytes).unwrap();
std::fs::File::options().write(true).open(&dbf).unwrap().set_modified(modified).unwrap();
};
rewrite();
#[cfg(unix)]
{
let give_up = std::time::Instant::now() + std::time::Duration::from_secs(2); // test-wait: the change time moves with the system's clock tick, which no signal announces; two seconds cover a file system that keeps whole seconds.
while changed_at(&dbf) == opened_at && std::time::Instant::now() < give_up {
rewrite();
}
}
Comment on lines +980 to +999

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On non-Unix targets the loop body always ends in break, so the for never loops more than once. That should trip clippy's never_loop lint (deny by default) on Windows and other non-Unix runs, even though Linux CI passes. Moving the write into a closure and putting the loop only under #[cfg(unix)] keeps the same behaviour and removes the dead-loop shape:

Suggested change
#[cfg(unix)]
let changed_at = |p: &std::path::Path| {
use std::os::unix::fs::MetadataExt;
let m = std::fs::metadata(p).unwrap();
(m.ctime(), m.ctime_nsec())
};
#[cfg(unix)]
let opened_at = changed_at(&dbf);
for _ in 0..1_000_000 {
std::fs::write(&dbf, &bytes).unwrap();
std::fs::File::options().write(true).open(&dbf).unwrap().set_modified(modified).unwrap();
#[cfg(unix)]
if changed_at(&dbf) != opened_at {
break;
}
#[cfg(not(unix))]
break;
}
let rewrite = || {
std::fs::write(&dbf, &bytes).unwrap();
std::fs::File::options().write(true).open(&dbf).unwrap().set_modified(modified).unwrap();
};
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
let changed_at = || {
let m = std::fs::metadata(&dbf).unwrap();
(m.ctime(), m.ctime_nsec())
};
let opened_at = changed_at();
for _ in 0..1_000_000 {
rewrite();
if changed_at() != opened_at {
break;
}
}
}
#[cfg(not(unix))]
rewrite();

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right: fixed in 041f262. The rewrite loop is now Unix-only; other systems write the file once.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the two-second deadline passes with the change time unmoved, the test falls through to assert!(!s.same_as_opened()…, "written again at the same size and time"). That message blames the source, but the file system never showed the change, so a failure here reads like a detection bug. Asserting the precondition right after the loop gives the real cause.

Suggested change
}
assert!(changed_at(&dbf) != opened_at, "the change time did not move within two seconds");
}

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A clearer message for a timed-out wait is left for the release quality pass.

#[cfg(unix)]
assert!(!s.same_as_opened().unwrap(), "written again at the same size and time");
let s = ShpSource::open_path(&path, "pts").unwrap();
Expand Down
Loading