Skip to content

Commit

Permalink
Add test for AsyncFd::to_file_descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Apr 20, 2024
1 parent 9255dfd commit 2cfcc3e
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tests/async_fd/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use a10::fs::OpenOptions;

use crate::util::{test_queue, Waker, LOREM_IPSUM_5};
use crate::util::{require_kernel, test_queue, Waker, LOREM_IPSUM_5};

#[test]
fn to_direct_descriptor() {
Expand Down Expand Up @@ -38,3 +38,54 @@ fn to_direct_descriptor() {
"read content is different"
);
}

#[test]
fn to_file_descriptor() {
require_kernel!(6, 8);

let sq = test_queue();
let waker = Waker::new();

// TODO: use open with direct descriptor?
let open_file = OpenOptions::new().open(sq, LOREM_IPSUM_5.path.into());
let regular_fd = waker.block_on(open_file).unwrap();
let direct_fd = waker.block_on(regular_fd.to_direct_descriptor()).unwrap();
let regular_fd2 = waker.block_on(direct_fd.to_file_descriptor()).unwrap();

let mut buf = Vec::with_capacity(LOREM_IPSUM_5.content.len() + 1);

// Regular fd.
let read = regular_fd.read_at(buf, 0);
buf = waker.block_on(read).unwrap();
if buf.is_empty() {
panic!("read zero bytes");
}
assert!(
buf == &LOREM_IPSUM_5.content[..buf.len()],
"read content is different"
);

// Direct descriptor.
buf.clear();
let read = direct_fd.read_at(buf, 0);
buf = waker.block_on(read).unwrap();
if buf.is_empty() {
panic!("read zero bytes");
}
assert!(
buf == &LOREM_IPSUM_5.content[..buf.len()],
"read content is different"
);

// Regular fd (created from the direct descriptor).
buf.clear();
let read = regular_fd2.read_at(buf, 0);
buf = waker.block_on(read).unwrap();
if buf.is_empty() {
panic!("read zero bytes");
}
assert!(
buf == &LOREM_IPSUM_5.content[..buf.len()],
"read content is different"
);
}

0 comments on commit 2cfcc3e

Please sign in to comment.