Skip to content

Commit

Permalink
Add a test for filestore loading from disk.
Browse files Browse the repository at this point in the history
I had a concern that the loading of filetimes from the disk may have been
broken while looking into the ordering of entries in the evicting map.  It
turns out that this was unfounded as writing a test proves that the function
works as expected.

I then broke the function on purpose by changing the sort order and found this
new test was the only one that failed.  Therefore adding it to the test suite
seems reasonable since it is now written.
  • Loading branch information
chrisstaite-menlo committed Jul 10, 2023
1 parent 7bad256 commit 5f3e9f5
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions cas/store/tests/filesystem_store_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ use futures::task::Poll;
use futures::{poll, Future};
use lazy_static::lazy_static;
use rand::{thread_rng, Rng};
use tokio::io::AsyncReadExt;
use tokio::io::Take;
use tokio::io::{AsyncReadExt, AsyncWriteExt, Take};
use tokio::sync::Barrier;
use tokio_stream::{wrappers::ReadDirStream, StreamExt};
use traits::UploadSizeInfo;

use buf_channel::{make_buf_channel_pair, DropCloserReadHalf};
use common::{fs, DigestInfo};
use config;
use error::{Error, ResultExt};
use error::{Code, Error, ResultExt};
use filesystem_store::{digest_from_filename, EncodedFilePath, FileEntry, FileEntryImpl, FilesystemStore};
use traits::StoreTrait;

Expand Down Expand Up @@ -168,6 +167,13 @@ async fn read_file_contents(file_name: &str) -> Result<Vec<u8>, Error> {
Ok(data)
}

async fn write_file(file_name: &str, data: &[u8]) -> Result<(), Error> {
let mut file = fs::create_file(&file_name)
.await
.err_tip(|| format!("Failed to create file: {}", file_name))?;
Ok(file.write_all(&data).await?)
}

#[cfg(test)]
mod filesystem_store_tests {
use super::*;
Expand Down Expand Up @@ -547,6 +553,49 @@ mod filesystem_store_tests {
Ok(())
}

#[tokio::test]
async fn oldest_entry_evicted_with_access_times_loaded_from_disk() -> Result<(), Error> {
// Note these are swapped to ensure they aren't in numerical order.
let digest1 = DigestInfo::try_new(&HASH2, VALUE2.len())?;
let digest2 = DigestInfo::try_new(&HASH1, VALUE1.len())?;

let content_path = make_temp_path("content_path");
fs::create_dir_all(&content_path).await?;

// Make the two files on disk before loading the store.
let file1 = format!("{}/{}-{}", content_path, digest1.str(), digest1.size_bytes);
let file2 = format!("{}/{}-{}", content_path, digest2.str(), digest2.size_bytes);
write_file(&file1, VALUE1.as_bytes()).await?;
write_file(&file2, VALUE2.as_bytes()).await?;
set_file_atime(&file1, FileTime::from_unix_time(0, 0))?;
set_file_atime(&file2, FileTime::from_unix_time(1, 0))?;

// Load the existing store from disk.
let store = Box::pin(
FilesystemStore::<FileEntryImpl>::new(&config::stores::FilesystemStore {
content_path,
temp_path: make_temp_path("temp_path"),
eviction_policy: Some(config::stores::EvictionPolicy {
max_bytes: 0,
max_seconds: 0,
max_count: 1,
}),
..Default::default()
})
.await?,
);

// This should exist and not have been evicted.
store.get_file_entry_for_digest(&digest2).await?;
// This should have been evicted.
match store.get_file_entry_for_digest(&digest1).await {
Ok(_) => panic!("Oldest file should have been evicted."),
Err(error) => assert_eq!(error.code, Code::NotFound),
}

Ok(())
}

#[tokio::test]
async fn eviction_drops_file_test() -> Result<(), Error> {
let digest1 = DigestInfo::try_new(&HASH1, VALUE1.len())?;
Expand Down

0 comments on commit 5f3e9f5

Please sign in to comment.