use std::sync::Arc;
use super::*;
#[tokio::test]
async fn test_default_disk_writer_write_and_finalize() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_default.bin");
let mut writer = DefaultDiskWriter::new(&path);
writer.write(b"hello").await.unwrap();
writer.write(b" world").await.unwrap();
writer.finalize().await.unwrap();
let content = tokio::fs::read_to_string(&path).await.unwrap();
assert_eq!(content, "hello world");
}
#[tokio::test]
async fn test_default_disk_writer_resume_writes_at_offset() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("resume.bin");
tokio::fs::write(&path, b"prefix").await.unwrap();
let mut writer = DefaultDiskWriter::new_with_offset(&path, 6);
writer.write(b"suffix").await.unwrap();
writer.finalize().await.unwrap();
assert_eq!(tokio::fs::read(&path).await.unwrap(), b"prefixsuffix");
}
#[tokio::test]
async fn test_default_disk_writer_sync_data_persists_payload_and_path_entry() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("checkpoint-payload.bin");
let mut writer = DefaultDiskWriter::new(&path);
writer.write(b"durable prefix").await.unwrap();
writer.sync_data().await.unwrap();
assert_eq!(tokio::fs::read(&path).await.unwrap(), b"durable prefix");
writer.finalize().await.unwrap();
}
#[tokio::test]
async fn test_byte_array_disk_writer() {
let mut writer = ByteArrayDiskWriter::with_capacity(10);
writer.write(b"abc").await.unwrap();
writer.write(b"def").await.unwrap();
let result = writer.finalize().await.unwrap();
assert_eq!(result, b"abcdef");
assert_eq!(
writer.len(),
0,
"finalize transfers ownership of the buffer"
);
}
#[tokio::test]
async fn test_sequential_download_writer_memory_mode_never_opens_path() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("metadata.torrent");
let mut writer = new_sequential_download_writer(&path, true, 99, Some(6));
writer.write(b"abcdef").await.unwrap();
assert_eq!(writer.finalize().await.unwrap(), b"abcdef");
assert!(!path.exists());
}
#[tokio::test]
async fn test_seekable_writer_basic() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_seekable.bin");
let mut writer = CachedDiskWriter::new(&path, Some(1024), None);
writer.open().await.unwrap();
assert!(writer.is_opened());
writer.write_at(0, b"hello").await.unwrap();
writer.write_at(5, b" world").await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(&content[..11], b"hello world");
}
#[tokio::test]
async fn test_seekable_writer_random_access() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_random.bin");
let mut writer = CachedDiskWriter::new(&path, None, None);
writer.open().await.unwrap();
writer.write_at(200, b"SEG2").await.unwrap();
writer.write_at(0, b"SEG0").await.unwrap();
writer.write_at(100, b"SEG1").await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(content.len(), 204);
assert_eq!(&content[0..4], b"SEG0");
assert_eq!(&content[100..104], b"SEG1");
assert_eq!(&content[200..204], b"SEG2");
}
#[tokio::test]
async fn test_seekable_writer_read_at() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_read.bin");
let mut writer = CachedDiskWriter::new(&path, Some(100), None);
writer.open().await.unwrap();
writer.write_at(50, b"offset-50-data").await.unwrap();
writer.flush().await.unwrap();
let mut buf = [0u8; 14];
let n = writer.read_at(50, &mut buf).await.unwrap();
assert_eq!(n, 14);
assert_eq!(&buf, b"offset-50-data");
}
#[tokio::test]
async fn test_cached_writer_with_cache() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_cached.bin");
let mut writer = CachedDiskWriter::new(&path, Some(4096), Some(1));
writer.open().await.unwrap();
for i in 0..100 {
let data = vec![i as u8; 64];
writer.write_at((i * 64) as u64, &data).await.unwrap();
}
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(content.len(), 6400);
for i in 0..100 {
let start = i * 64;
assert_eq!(content[start], i as u8, "mismatch at byte {}", start);
}
}
#[tokio::test]
async fn test_cached_writer_sync_data_drains_write_back_cache() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_cached_sync.bin");
let mut writer = CachedDiskWriter::new(&path, None, Some(1));
writer.write_at(0, b"durable payload").await.unwrap();
assert!(tokio::fs::read(&path).await.unwrap().is_empty());
writer.sync_data().await.unwrap();
assert_eq!(tokio::fs::read(&path).await.unwrap(), b"durable payload");
assert!(writer.stats().cache.flush_write_count > 0);
}
#[tokio::test]
async fn test_cached_writer_large_write_bypasses_cache() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_large.bin");
let large_data = vec![0xAB; 128 * 1024];
let mut writer = CachedDiskWriter::new(&path, None, Some(1));
writer.open().await.unwrap();
writer.write_at(0, &large_data).await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(content.len(), large_data.len());
assert!(content.iter().all(|&b| b == 0xAB));
}
#[tokio::test]
async fn test_seekable_writer_truncate() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_trunc.bin");
let mut writer = CachedDiskWriter::new(&path, Some(1000), None);
writer.open().await.unwrap();
writer
.write_at(0, b"hello world - this is longer than 20 bytes of data")
.await
.unwrap();
writer.flush().await.unwrap();
writer.truncate(20).await.unwrap();
writer.flush().await.unwrap();
let len = writer.len().await.unwrap();
assert!(len <= 21);
let content = tokio::fs::read(&path).await.unwrap();
assert!(content.len() <= 21);
assert_eq!(&content[..4], b"hell");
}
#[tokio::test]
async fn test_seekable_writer_len_before_open() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_len.bin");
let writer = CachedDiskWriter::new(&path, Some(9999), None);
let len = writer.len().await.unwrap();
assert_eq!(len, 9999);
}
#[tokio::test]
async fn test_close_reopens_cleanly() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_close.bin");
let mut writer = CachedDiskWriter::new(&path, None, None);
writer.open().await.unwrap();
writer.write_at(0, b"before close").await.unwrap();
writer.close().await.unwrap();
assert!(!writer.is_opened());
writer.open().await.unwrap();
writer.write_at(12, b" after reopen").await.unwrap();
writer.close().await.unwrap();
let content = tokio::fs::read_to_string(&path).await.unwrap();
assert_eq!(content, "before close after reopen");
}
#[tokio::test]
async fn test_cached_writer_with_rate_limiter() {
use crate::rate_limiter::{RateLimiter, RateLimiterConfig};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_ratelimited.bin");
let cfg = RateLimiterConfig::new(Some(100), None).with_burst(Some(20), None);
let rl = Arc::new(RateLimiter::new(&cfg));
let mut writer = CachedDiskWriter::new(&path, Some(4096), None).with_rate_limiter(rl.clone());
writer.open().await.unwrap();
let data = vec![0x42u8; 64];
writer.write_at(0, &data).await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert!(content.len() >= data.len(), "file should contain the write");
assert_eq!(&content[..data.len()], &data);
assert!(content.iter().take(data.len()).all(|&b| b == 0x42));
}
#[tokio::test]
async fn test_cached_writer_without_rate_limiter_no_effect() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_nolimiter.bin");
let mut writer = CachedDiskWriter::new(&path, Some(1024), None);
writer.open().await.unwrap();
writer.write_at(0, b"no limiter").await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert!(
content.starts_with(b"no limiter"),
"should contain written data"
);
}
#[tokio::test]
async fn test_concurrent_writes_different_offsets() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_concurrent.bin");
let mut writer = CachedDiskWriter::new(&path, Some(16 * 1024 * 1024), None);
writer.open().await.unwrap();
let mut handles = vec![];
for i in 0..16 {
let offset = (i as u64) * 1024 * 1024;
let data = vec![i as u8; 4096];
let path_clone = path.clone();
handles.push(tokio::spawn(async move {
let mut w = CachedDiskWriter::new(&path_clone, None, None);
w.open().await.unwrap();
w.write_at(offset, &data).await.unwrap();
w.flush().await.unwrap();
w.close().await.unwrap();
}));
}
for handle in handles {
handle.await.unwrap();
}
let content = tokio::fs::read(&path).await.unwrap();
for i in 0..16 {
let offset = (i as usize) * 1024 * 1024;
let expected = vec![i as u8; 4096];
assert_eq!(
&content[offset..offset + 4096],
&expected[..],
"Data mismatch at offset {}",
i
);
}
}
#[tokio::test]
async fn test_concurrent_writes_serialized() {
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_same_offset.bin");
let mut writer = CachedDiskWriter::new(&path, Some(1024 * 1024), None);
writer.open().await.unwrap();
writer.close().await.unwrap();
let write_count = Arc::new(AtomicUsize::new(0));
let mut handles = vec![];
for i in 0..10 {
let offset = (i as u64) * 1024;
let data = vec![i as u8; 1024];
let path_clone = path.clone();
let counter = write_count.clone();
handles.push(tokio::spawn(async move {
let mut w = CachedDiskWriter::new(&path_clone, None, None);
w.open().await.unwrap();
w.write_at(offset, &data).await.unwrap();
counter.fetch_add(1, Ordering::SeqCst);
w.flush().await.unwrap();
w.close().await.unwrap();
}));
}
for handle in handles {
handle.await.unwrap();
}
assert_eq!(write_count.load(Ordering::SeqCst), 10);
let content = tokio::fs::read(&path).await.unwrap();
for i in 0..10 {
let offset = i * 1024;
let expected = vec![i as u8; 1024];
assert_eq!(
&content[offset..offset + 1024],
&expected[..],
"Data mismatch at offset {}",
offset
);
}
}
#[tokio::test]
async fn test_high_concurrency_stress() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_stress.bin");
let mut writer = CachedDiskWriter::new(&path, Some(32 * 1024 * 1024), None);
writer.open().await.unwrap();
writer.close().await.unwrap();
let num_threads = 32;
let writes_per_thread = 100;
let mut handles = vec![];
for thread_id in 0..num_threads {
let path_clone = path.clone();
handles.push(tokio::spawn(async move {
let mut w = CachedDiskWriter::new(&path_clone, None, None);
w.open().await.unwrap();
for write_id in 0..writes_per_thread {
let offset = ((thread_id * writes_per_thread + write_id) as u64) * 8192;
let data = vec![(thread_id + write_id) as u8; 8192];
w.write_at(offset, &data).await.unwrap();
}
w.flush().await.unwrap();
w.close().await.unwrap();
}));
}
for handle in handles {
handle.await.unwrap();
}
let content = tokio::fs::read(&path).await.unwrap();
for thread_id in 0..num_threads {
for write_id in 0..writes_per_thread {
let offset = ((thread_id * writes_per_thread + write_id) as usize) * 8192;
let expected = vec![(thread_id + write_id) as u8; 8192];
if offset + 8192 <= content.len() {
assert_eq!(
&content[offset..offset + 8192],
&expected[..],
"Data mismatch at thread {} write {}",
thread_id,
write_id
);
}
}
}
}
#[tokio::test]
async fn test_concurrent_writes_no_mutex_contention() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_no_contention.bin");
let chunk_size: usize = 64 * 1024;
let num_tasks: usize = 8;
let total_size = (chunk_size * num_tasks) as u64;
let mut writer = CachedDiskWriter::new(&path, Some(total_size), None);
writer.open().await.unwrap();
let writer = Arc::new(tokio::sync::Mutex::new(writer));
let mut handles = Vec::with_capacity(num_tasks);
for i in 0..num_tasks {
let offset = (i as u64) * chunk_size as u64;
let fill = (i as u8) + 1;
let data = bytes::Bytes::from(vec![fill; chunk_size]);
let w = writer.clone();
handles.push(tokio::spawn(async move {
let mut guard = w.lock().await;
guard.write_bytes_at(offset, data).await.unwrap();
}));
}
for handle in handles {
handle.await.unwrap();
}
{
let mut guard = writer.lock().await;
guard.flush().await.unwrap();
}
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(content.len(), total_size as usize);
for i in 0..num_tasks {
let start = i * chunk_size;
let expected = (i as u8) + 1;
let chunk = &content[start..start + chunk_size];
assert!(
chunk.iter().all(|&b| b == expected),
"data mismatch in task {} chunk",
i
);
}
}
#[tokio::test]
async fn test_cached_writer_out_of_order_writes_land_at_offsets() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out_of_order.bin");
let mut writer = CachedDiskWriter::new(&path, Some(8), Some(16));
writer.open().await.unwrap();
writer.write_at(4, b"bbbb").await.unwrap();
writer.write_at(0, b"aaaa").await.unwrap();
writer.flush().await.unwrap();
writer.close().await.unwrap();
let content = std::fs::read(&path).unwrap();
assert_eq!(
content,
b"aaaabbbb".to_vec(),
"pieces must land at their offsets"
);
}
#[tokio::test]
async fn test_large_write_flushes_pending_cache_before_bypass() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("large-write-order.bin");
let large_len = 1024 * 1024;
let large = vec![b'B'; large_len];
let mut writer = CachedDiskWriter::new(&path, Some(large_len as u64), Some(4));
writer.open().await.unwrap();
writer.write_at(0, b"small").await.unwrap();
writer.write_at(0, &large).await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(&content[..5], b"BBBBB");
}
#[tokio::test]
async fn test_large_bytes_write_flushes_pending_cache_before_bypass() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("large-bytes-write-order.bin");
let large_len = 1024 * 1024;
let large = bytes::Bytes::from(vec![b'C'; large_len]);
let mut writer = CachedDiskWriter::new(&path, Some(large_len as u64), Some(4));
writer.open().await.unwrap();
writer.write_at(0, b"small").await.unwrap();
writer.write_bytes_at(0, large).await.unwrap();
writer.flush().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(&content[..5], b"CCCCC");
}
#[tokio::test]
async fn test_cached_writer_stats_separate_cache_and_direct_writes() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("stats.bin");
let large_len = 1024 * 1024;
let mut writer = CachedDiskWriter::new(&path, Some(large_len as u64), Some(4));
writer.open().await.unwrap();
writer
.write_bytes_at(0, bytes::Bytes::from_static(b"small"))
.await
.unwrap();
writer
.write_bytes_at(0, bytes::Bytes::from(vec![b'X'; large_len]))
.await
.unwrap();
let stats = writer.stats();
assert_eq!(stats.cache.flush_pending_write_count, 1);
assert_eq!(stats.cache.flush_write_count, 1);
assert_eq!(stats.cache.flush_write_bytes, 5);
assert_eq!(stats.direct_write_count, 1);
assert_eq!(stats.direct_write_bytes, large_len as u64);
assert_eq!(stats.cache.flush_write_count + stats.direct_write_count, 2);
writer.close().await.unwrap();
}
#[tokio::test]
async fn test_cached_writer_keeps_write_back_cache_within_hard_limit() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("hard-limit.bin");
let mut writer = CachedDiskWriter::new_with_mmap_bytes(&path, Some(64), Some(24), false);
writer.open().await.unwrap();
let cache = writer.cache_handle().expect("cache is configured");
for index in 0..8 {
writer
.write_bytes_at(index * 4, bytes::Bytes::from_static(b"abcd"))
.await
.unwrap();
assert!(
cache.current_size_bytes() <= cache.max_size_bytes(),
"cache exceeded its hard limit after write {index}"
);
}
writer.close().await.unwrap();
let content = tokio::fs::read(&path).await.unwrap();
assert_eq!(&content[..32], b"abcdabcdabcdabcdabcdabcdabcdabcd");
}
#[tokio::test]
async fn test_sequential_writer_appends_out_of_order() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("seq_ooo.bin");
let mut writer = DefaultDiskWriter::new(&path);
writer.write(b"bbbb").await.unwrap(); writer.write(b"aaaa").await.unwrap(); writer.finalize().await.unwrap();
let content = std::fs::read(&path).unwrap();
assert_eq!(
content,
b"bbbbaaaa".to_vec(),
"sequential append ignores offsets"
);
}