use safer_ring::{OwnedBuffer, Ring};
#[tokio::test]
async fn test_basic_ring_operations() {
#[cfg(not(target_os = "linux"))]
{
println!("Skipping io_uring-specific test on macOS/Windows - no io_uring kernel support");
return;
}
#[cfg(target_os = "linux")]
{
let ring = match Ring::new(32) {
Ok(r) => r,
Err(e) => {
println!("Could not create ring (io_uring may not be available): {e}");
return;
}
};
let buffer = OwnedBuffer::new(512);
let operation_future = ring.read_owned(0, buffer);
match tokio::time::timeout(std::time::Duration::from_millis(100), operation_future).await {
Ok(_) => println!("✅ Operation completed successfully"),
Err(_) => println!("✅ Operation timed out as expected (no data on stdin)"),
}
let buffer2 = OwnedBuffer::new(256);
let operation_future2 = ring.read_owned(0, buffer2);
match tokio::time::timeout(std::time::Duration::from_millis(50), operation_future2).await {
Ok(_) => println!("✅ Second operation completed successfully"),
Err(_) => println!("✅ Second operation timed out as expected"),
}
println!("✅ Basic ring operations test completed successfully");
}
}
#[tokio::test]
async fn test_ring_buffer_management() {
#[cfg(not(target_os = "linux"))]
{
println!("Skipping io_uring-specific test on macOS/Windows - no io_uring kernel support");
return;
}
#[cfg(target_os = "linux")]
{
let ring = match Ring::new(32) {
Ok(r) => r,
Err(e) => {
println!("Could not create ring (io_uring may not be available): {e}");
return;
}
};
let buffer = OwnedBuffer::from_slice(b"test data");
assert_eq!(buffer.size(), 9); assert!(buffer.is_user_owned());
if let Some(guard) = buffer.try_access() {
assert_eq!(guard.len(), 9);
println!("✅ Buffer access working correctly");
}
let operation_future = ring.read_owned(0, buffer);
match tokio::time::timeout(std::time::Duration::from_millis(10), operation_future).await {
Ok(_) => println!("✅ Buffer operation completed"),
Err(_) => println!("✅ Buffer operation timed out as expected"),
}
println!("✅ Ring buffer management test completed successfully");
}
}
#[tokio::test]
async fn test_ring_capacity() {
#[cfg(not(target_os = "linux"))]
{
println!("Skipping io_uring-specific test on macOS/Windows - no io_uring kernel support");
return;
}
#[cfg(target_os = "linux")]
{
for capacity in [8, 16, 32, 64] {
match Ring::new(capacity) {
Ok(mut ring) => {
println!("✅ Successfully created ring with capacity {capacity}");
assert!(ring.capacity() > 0);
let buffer = OwnedBuffer::new(128);
let operation = ring.read_owned(0, buffer);
drop(operation);
drop(ring);
}
Err(e) => {
println!("Could not create ring with capacity {capacity}: {e}");
}
}
}
println!("✅ Ring capacity test completed successfully");
}
}