325 lines
12 KiB
Rust
325 lines
12 KiB
Rust
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use rcom::domain::{HostAlias, Location, SourceId};
|
|
use rcom::fs::{BackendHandle, FileOperations, FsError, PartFile, ReadFileSystem};
|
|
use rcom::test_support::{MemoryFs, MemoryTransferFs, TransferOperation};
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
async fn closed_part(fs: &MemoryTransferFs, destination: &Location, data: &[u8]) -> PartFile {
|
|
let mut part = fs.open_part(destination).await.unwrap();
|
|
fs.write_part(&mut part, data).await.unwrap();
|
|
fs.flush_and_close(part).await.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn streaming_copy_handles_short_io_journals_calls_and_preserves_read_only_memory_fs() {
|
|
let source_location = Location::local("/source/data.bin");
|
|
let destination_location = Location::local("/destination/data.bin");
|
|
let read_only = Arc::new(MemoryFs::new().with_file(source_location.clone(), b"abcdefgh"));
|
|
let before = read_only.read_prefix(&source_location, 64).await.unwrap();
|
|
let source = MemoryTransferFs::local().with_file(source_location.clone(), b"abcdefgh");
|
|
source.set_read_chunk_size(2);
|
|
let destination = MemoryTransferFs::local().with_dir(Location::local("/destination"));
|
|
destination.set_write_chunk_size(1);
|
|
let handle =
|
|
BackendHandle::new(SourceId::Local, read_only.clone(), Arc::new(source.clone())).unwrap();
|
|
|
|
let mut reader = handle.transfer.open_read(&source_location).await.unwrap();
|
|
let mut writer = destination.open_part(&destination_location).await.unwrap();
|
|
let part_location = writer.part().location().clone();
|
|
let mut buffer = [0; 64];
|
|
let mut read_sizes = Vec::new();
|
|
loop {
|
|
let count = reader.read(&mut buffer).await.unwrap();
|
|
if count == 0 {
|
|
break;
|
|
}
|
|
read_sizes.push(count);
|
|
destination
|
|
.write_part(&mut writer, &buffer[..count])
|
|
.await
|
|
.unwrap();
|
|
}
|
|
assert_eq!(read_sizes, [2, 2, 2, 2]);
|
|
assert_eq!(destination.part_owner(&part_location), Some(0));
|
|
let part = destination.flush_and_close(writer).await.unwrap();
|
|
destination.commit_part(part).await.unwrap();
|
|
|
|
assert_eq!(
|
|
destination.file_contents(&destination_location).unwrap(),
|
|
b"abcdefgh"
|
|
);
|
|
assert!(destination.part_locations().is_empty());
|
|
assert_eq!(
|
|
read_only.read_prefix(&source_location, 64).await.unwrap(),
|
|
before
|
|
);
|
|
assert_eq!(
|
|
read_only
|
|
.list(&Location::local("/source"))
|
|
.await
|
|
.unwrap()
|
|
.len(),
|
|
1
|
|
);
|
|
|
|
let source_journal = source.operations();
|
|
assert_eq!(source_journal.len(), 1);
|
|
assert_eq!(source_journal[0].operation, TransferOperation::Read);
|
|
assert_eq!(source_journal[0].location, source_location);
|
|
assert_eq!(source_journal[0].epoch, 0);
|
|
|
|
let destination_journal = destination.operations();
|
|
let kinds: Vec<_> = destination_journal
|
|
.iter()
|
|
.map(|entry| entry.operation)
|
|
.collect();
|
|
assert_eq!(
|
|
kinds,
|
|
[
|
|
TransferOperation::OpenPart,
|
|
TransferOperation::Write,
|
|
TransferOperation::Write,
|
|
TransferOperation::Write,
|
|
TransferOperation::Write,
|
|
TransferOperation::FlushClose,
|
|
TransferOperation::Commit,
|
|
]
|
|
);
|
|
assert!(destination_journal.iter().all(|entry| entry.epoch == 0));
|
|
assert_eq!(destination_journal[1].bytes, Some(2));
|
|
assert_eq!(
|
|
destination_journal.last().unwrap().destination,
|
|
Some(destination_location)
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn part_writer_can_short_write_while_write_part_still_completes_its_chunk() {
|
|
let fs = MemoryTransferFs::local().with_dir(Location::local("/out"));
|
|
let destination = Location::local("/out/file");
|
|
fs.set_write_chunk_size(1);
|
|
let mut part = fs.open_part(&destination).await.unwrap();
|
|
let part_location = part.part().location().clone();
|
|
|
|
let written = part.write(b"abc").await.unwrap();
|
|
assert_eq!(written, 1);
|
|
fs.write_part(&mut part, &b"abc"[written..]).await.unwrap();
|
|
let part = fs.flush_and_close(part).await.unwrap();
|
|
fs.commit_part(part).await.unwrap();
|
|
|
|
assert_eq!(fs.file_contents(&destination).unwrap(), b"abc");
|
|
assert_eq!(fs.part_contents(&part_location), Err(FsError::NotFound));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn failpoints_stop_before_apply_and_leave_owned_parts_for_diagnostics() {
|
|
let source = Location::local("/source");
|
|
let destination = Location::local("/out/file");
|
|
let fs = MemoryTransferFs::local()
|
|
.with_file(source.clone(), b"source")
|
|
.with_file(Location::local("/rename-from"), b"rename")
|
|
.with_file(Location::local("/delete-me"), b"delete")
|
|
.with_dir(Location::local("/out"))
|
|
.with_dir(Location::local("/empty"));
|
|
|
|
fs.fail_next(TransferOperation::Read, FsError::Timeout);
|
|
assert_eq!(fs.open_read(&source).await.err().unwrap(), FsError::Timeout);
|
|
|
|
fs.fail_next(TransferOperation::OpenPart, FsError::PermissionDenied);
|
|
assert_eq!(
|
|
fs.open_part(&destination).await.err().unwrap(),
|
|
FsError::PermissionDenied
|
|
);
|
|
assert!(fs.part_locations().is_empty());
|
|
|
|
let mut writer = fs.open_part(&destination).await.unwrap();
|
|
let failed_write_part = writer.part().clone();
|
|
fs.fail_next(TransferOperation::Write, FsError::Disconnected);
|
|
assert_eq!(
|
|
fs.write_part(&mut writer, b"not written")
|
|
.await
|
|
.unwrap_err(),
|
|
FsError::Disconnected
|
|
);
|
|
assert_eq!(fs.part_contents(failed_write_part.location()).unwrap(), b"");
|
|
assert_eq!(fs.part_owner(failed_write_part.location()), Some(0));
|
|
|
|
fs.fail_next(TransferOperation::FlushClose, FsError::Timeout);
|
|
assert_eq!(
|
|
fs.flush_and_close(writer).await.unwrap_err(),
|
|
FsError::Timeout
|
|
);
|
|
assert_eq!(fs.part_is_closed(failed_write_part.location()), Some(false));
|
|
|
|
let part = closed_part(&fs, &destination, b"complete").await;
|
|
let part_location = part.location().clone();
|
|
fs.fail_next(TransferOperation::Commit, FsError::PermissionDenied);
|
|
assert_eq!(
|
|
fs.commit_part(part).await.unwrap_err(),
|
|
FsError::PermissionDenied
|
|
);
|
|
assert_eq!(fs.part_is_closed(&part_location), Some(true));
|
|
assert_eq!(fs.file_contents(&destination), Err(FsError::NotFound));
|
|
|
|
let rename_from = Location::local("/rename-from");
|
|
let rename_to = Location::local("/rename-to");
|
|
fs.fail_next(TransferOperation::Rename, FsError::Disconnected);
|
|
assert_eq!(
|
|
fs.rename(&rename_from, &rename_to).await.unwrap_err(),
|
|
FsError::Disconnected
|
|
);
|
|
assert_eq!(fs.file_contents(&rename_from).unwrap(), b"rename");
|
|
assert_eq!(fs.file_contents(&rename_to), Err(FsError::NotFound));
|
|
|
|
let delete_me = Location::local("/delete-me");
|
|
fs.fail_next(TransferOperation::RemoveFile, FsError::PermissionDenied);
|
|
assert_eq!(
|
|
fs.remove_file(&delete_me).await.unwrap_err(),
|
|
FsError::PermissionDenied
|
|
);
|
|
assert_eq!(fs.file_contents(&delete_me).unwrap(), b"delete");
|
|
|
|
let empty = Location::local("/empty");
|
|
fs.fail_next(TransferOperation::RemoveDir, FsError::Timeout);
|
|
assert_eq!(fs.remove_dir(&empty).await.unwrap_err(), FsError::Timeout);
|
|
assert!(FileOperations::canonicalize(&fs, &empty).await.is_ok());
|
|
|
|
let kinds: Vec<_> = fs
|
|
.operations()
|
|
.into_iter()
|
|
.map(|entry| entry.operation)
|
|
.collect();
|
|
for expected in [
|
|
TransferOperation::Read,
|
|
TransferOperation::OpenPart,
|
|
TransferOperation::Write,
|
|
TransferOperation::FlushClose,
|
|
TransferOperation::Commit,
|
|
TransferOperation::Rename,
|
|
TransferOperation::RemoveFile,
|
|
TransferOperation::RemoveDir,
|
|
] {
|
|
assert!(
|
|
kinds.contains(&expected),
|
|
"missing journal entry: {expected:?}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn deterministic_gate_pauses_write_until_released_without_sleeping() {
|
|
let destination = Location::local("/out/file");
|
|
let fs = MemoryTransferFs::local().with_dir(Location::local("/out"));
|
|
let mut part = fs.open_part(&destination).await.unwrap();
|
|
let part_location = part.part().location().clone();
|
|
let gate = fs.gate_next(TransferOperation::Write);
|
|
let blocked_fs = fs.clone();
|
|
|
|
let task = tokio::spawn(async move { blocked_fs.write_part(&mut part, b"blocked").await });
|
|
tokio::time::timeout(Duration::from_secs(1), gate.wait_entered())
|
|
.await
|
|
.expect("write did not reach its gate");
|
|
assert!(!task.is_finished());
|
|
assert_eq!(fs.part_contents(&part_location).unwrap(), b"");
|
|
|
|
gate.release();
|
|
tokio::time::timeout(Duration::from_secs(1), task)
|
|
.await
|
|
.expect("released write did not finish")
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(fs.part_contents(&part_location).unwrap(), b"blocked");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn after_apply_disconnect_exposes_applied_commit_and_rename_without_retry() {
|
|
let destination = Location::local("/out/file");
|
|
let source = Location::local("/source");
|
|
let fs = MemoryTransferFs::local()
|
|
.with_file(source.clone(), b"source")
|
|
.with_file(Location::local("/from"), b"rename")
|
|
.with_dir(Location::local("/out"));
|
|
let part = closed_part(&fs, &destination, b"committed").await;
|
|
fs.disconnect_after_apply(TransferOperation::Commit);
|
|
|
|
assert_eq!(
|
|
fs.commit_part(part).await.unwrap_err(),
|
|
FsError::Disconnected
|
|
);
|
|
assert_eq!(fs.file_contents(&destination).unwrap(), b"committed");
|
|
assert_eq!(fs.file_contents(&source).unwrap(), b"source");
|
|
assert!(fs.part_locations().is_empty());
|
|
assert_eq!(
|
|
fs.operations()
|
|
.iter()
|
|
.filter(|entry| entry.operation == TransferOperation::Commit)
|
|
.count(),
|
|
1
|
|
);
|
|
|
|
fs.disconnect_after_apply(TransferOperation::Rename);
|
|
assert_eq!(
|
|
fs.rename(&Location::local("/from"), &Location::local("/to"))
|
|
.await
|
|
.unwrap_err(),
|
|
FsError::Disconnected
|
|
);
|
|
assert_eq!(
|
|
fs.file_contents(&Location::local("/from")),
|
|
Err(FsError::NotFound)
|
|
);
|
|
assert_eq!(
|
|
fs.file_contents(&Location::local("/to")).unwrap(),
|
|
b"rename"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn stale_epoch_and_new_session_cannot_commit_an_owned_partial_file() {
|
|
let host = HostAlias::new("edge");
|
|
let endpoint = SourceId::Ssh(host.clone());
|
|
let source = Location::ssh(host.clone(), "/source");
|
|
let destination = Location::ssh(host, "/out/file");
|
|
let stale = MemoryTransferFs::new(endpoint, 7)
|
|
.with_file(source.clone(), b"source")
|
|
.with_dir(Location::ssh(HostAlias::new("edge"), "/out"));
|
|
let part = closed_part(&stale, &destination, b"partial").await;
|
|
let part_location = part.location().clone();
|
|
let current = stale.reconnect(8);
|
|
|
|
let stale_error = stale.commit_part(part.clone()).await.unwrap_err();
|
|
assert!(
|
|
matches!(&stale_error, FsError::Other(message) if message.contains("stale session epoch"))
|
|
);
|
|
let owner_error = current.commit_part(part).await.unwrap_err();
|
|
assert!(matches!(&owner_error, FsError::Other(message) if message.contains("not owned")));
|
|
assert!(
|
|
stale
|
|
.open_read(&source)
|
|
.await
|
|
.err()
|
|
.unwrap()
|
|
.to_string()
|
|
.contains("stale session epoch")
|
|
);
|
|
|
|
assert_eq!(current.current_epoch(), 8);
|
|
assert_eq!(current.epoch(), 8);
|
|
assert_eq!(current.part_owner(&part_location), Some(7));
|
|
assert_eq!(current.part_is_closed(&part_location), Some(true));
|
|
assert_eq!(current.part_contents(&part_location).unwrap(), b"partial");
|
|
assert_eq!(current.file_contents(&source).unwrap(), b"source");
|
|
assert_eq!(current.file_contents(&destination), Err(FsError::NotFound));
|
|
|
|
let journal = current.operations();
|
|
let commit_epochs: Vec<_> = journal
|
|
.iter()
|
|
.filter(|entry| entry.operation == TransferOperation::Commit)
|
|
.map(|entry| entry.epoch)
|
|
.collect();
|
|
assert_eq!(commit_epochs, [7, 8]);
|
|
assert_eq!(journal.last().unwrap().operation, TransferOperation::Read);
|
|
assert_eq!(journal.last().unwrap().epoch, 7);
|
|
}
|