Skip to content

Commit

Permalink
VirtualFile::atomic_overwrite: add basic unit tests (#5191)
Browse files Browse the repository at this point in the history
Should have added them in the initial PR #5186.

Would have been nice to test the failure cases as well, but, without
mocking the FS, that's too hard / platform-dependent.
  • Loading branch information
problame authored Sep 25, 2023
1 parent a0c8296 commit 1d98d3e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pageserver/src/virtual_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ mod tests {
File(File),
}

impl From<VirtualFile> for MaybeVirtualFile {
fn from(vf: VirtualFile) -> Self {
MaybeVirtualFile::VirtualFile(vf)
}
}

impl MaybeVirtualFile {
async fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error> {
match self {
Expand Down Expand Up @@ -887,4 +893,54 @@ mod tests {

Ok(())
}

#[tokio::test]
async fn test_atomic_overwrite_basic() {
let testdir = crate::config::PageServerConf::test_repo_dir("test_atomic_overwrite_basic");
std::fs::create_dir_all(&testdir).unwrap();

let path = testdir.join("myfile");
let tmp_path = testdir.join("myfile.tmp");

VirtualFile::crashsafe_overwrite(&path, &tmp_path, b"foo")
.await
.unwrap();
let mut file = MaybeVirtualFile::from(VirtualFile::open(&path).await.unwrap());
let post = file.read_string().await.unwrap();
assert_eq!(post, "foo");
assert!(!tmp_path.exists());
drop(file);

VirtualFile::crashsafe_overwrite(&path, &tmp_path, b"bar")
.await
.unwrap();
let mut file = MaybeVirtualFile::from(VirtualFile::open(&path).await.unwrap());
let post = file.read_string().await.unwrap();
assert_eq!(post, "bar");
assert!(!tmp_path.exists());
drop(file);
}

#[tokio::test]
async fn test_atomic_overwrite_preexisting_tmp() {
let testdir =
crate::config::PageServerConf::test_repo_dir("test_atomic_overwrite_preexisting_tmp");
std::fs::create_dir_all(&testdir).unwrap();

let path = testdir.join("myfile");
let tmp_path = testdir.join("myfile.tmp");

std::fs::write(&tmp_path, "some preexisting junk that should be removed").unwrap();
assert!(tmp_path.exists());

VirtualFile::crashsafe_overwrite(&path, &tmp_path, b"foo")
.await
.unwrap();

let mut file = MaybeVirtualFile::from(VirtualFile::open(&path).await.unwrap());
let post = file.read_string().await.unwrap();
assert_eq!(post, "foo");
assert!(!tmp_path.exists());
drop(file);
}
}

1 comment on commit 1d98d3e

@github-actions
Copy link

@github-actions github-actions bot commented on 1d98d3e Sep 25, 2023

Choose a reason for hiding this comment

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

2564 tests run: 2441 passed, 0 failed, 123 skipped (full report)


Flaky tests (3)

Postgres 16

Postgres 14

  • test_get_tenant_size_with_multiple_branches: debug

Code coverage (full report)

  • functions: 53.1% (7808 of 14711 functions)
  • lines: 81.2% (45676 of 56262 lines)

The comment gets automatically updated with the latest test results
1d98d3e at 2023-09-25T18:19:42.765Z :recycle:

Please sign in to comment.