-
Notifications
You must be signed in to change notification settings - Fork 35
wasi:[email protected]: Add tests for read/write/append #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "dirs": ["fs-tests.dir"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| use futures::join; | ||
| use std::process; | ||
| extern crate wit_bindgen; | ||
|
|
||
| wit_bindgen::generate!({ | ||
| inline: r" | ||
| package test:test; | ||
|
|
||
| world test { | ||
| include wasi:filesystem/[email protected]; | ||
| include wasi:cli/[email protected]; | ||
| } | ||
| ", | ||
| additional_derives: [PartialEq, Eq, Hash, Clone], | ||
| // Work around https://github.com/bytecodealliance/wasm-tools/issues/2285. | ||
| features:["clocks-timezone"], | ||
| generate_all | ||
| }); | ||
|
|
||
| use wasi::filesystem::types::Descriptor; | ||
| use wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags}; | ||
| use wit_bindgen::StreamResult; | ||
|
|
||
| async fn pread(fd: &Descriptor, size: usize, offset: u64) -> Result<Vec<u8>, ErrorCode> { | ||
| let (mut rx, future) = fd.read_via_stream(offset); | ||
| let data = Vec::<u8>::with_capacity(size); | ||
| let mut bytes_read = 0; | ||
| let (mut result, mut data) = rx.read(data).await; | ||
| loop { | ||
| match result { | ||
| StreamResult::Complete(n) => { | ||
| assert!(n <= size - bytes_read); | ||
| bytes_read += n; | ||
| assert_eq!(data.len(), bytes_read); | ||
| if bytes_read == size { | ||
| break; | ||
| } | ||
| (result, data) = rx.read(data).await; | ||
| } | ||
| StreamResult::Dropped => { | ||
| assert_eq!(data.len(), bytes_read); | ||
| break; | ||
| } | ||
| StreamResult::Cancelled => { | ||
| panic!("who cancelled the stream?"); | ||
| } | ||
| } | ||
| }; | ||
| drop(rx); | ||
| match future.await { | ||
| Ok(()) => Ok(data), | ||
| Err(err) => Err(err), | ||
| } | ||
| } | ||
|
|
||
| async fn pwrite(fd: &Descriptor, offset: u64, data: &[u8]) -> Result<usize, ErrorCode> { | ||
| let (mut tx, rx) = wit_stream::new(); | ||
| let future = fd.write_via_stream(rx, offset); | ||
| let len = data.len(); | ||
| let mut written: usize = 0; | ||
| let mut result: Result<(), ErrorCode> = Ok(()); | ||
| join!{ | ||
| async { | ||
| let (mut result, mut buf) = tx.write(data.to_vec()).await; | ||
| loop { | ||
| match result { | ||
| StreamResult::Complete(n) => { | ||
| assert!(n <= len - written); | ||
| written += n; | ||
| assert_eq!(buf.remaining(), len - written); | ||
| if buf.remaining() != 0 { | ||
| (result, buf) = tx.write_buf(buf).await; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| StreamResult::Dropped => { | ||
| panic!("receiver dropped the stream?"); | ||
| } | ||
| StreamResult::Cancelled => { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| assert_eq!(buf.remaining(), len - written); | ||
| drop(tx); | ||
|
Comment on lines
+64
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be replacable with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW... I have been avoiding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! Either way seems reasonable to me |
||
| }, | ||
| async { result = future.await; } | ||
| }; | ||
| match result { | ||
| Ok(()) => Ok(written), | ||
| Err(err) => Err(err), | ||
| } | ||
| } | ||
|
|
||
| async fn pappend(fd: &Descriptor, data: &[u8]) -> Result<usize, ErrorCode> { | ||
| let (mut tx, rx) = wit_stream::new(); | ||
| let future = fd.append_via_stream(rx); | ||
| let initial_size = fd.stat().await.unwrap().size as usize; | ||
| let len = data.len(); | ||
| let mut written: usize = 0; | ||
| let mut result: Result<(), ErrorCode> = Ok(()); | ||
| join!{ | ||
| async { | ||
| let (mut result, mut buf) = tx.write(data.to_vec()).await; | ||
| loop { | ||
| match result { | ||
| StreamResult::Complete(n) => { | ||
| assert!(n <= len - written); | ||
| written += n; | ||
| assert_eq!(buf.remaining(), len - written); | ||
| assert_eq!(fd.stat().await.unwrap().size as usize, | ||
| initial_size + written); | ||
| if buf.remaining() != 0 { | ||
| (result, buf) = tx.write_buf(buf).await; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| StreamResult::Dropped => { | ||
| panic!("receiver dropped the stream?"); | ||
| } | ||
| StreamResult::Cancelled => { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| assert_eq!(buf.remaining(), len - written); | ||
| drop(tx); | ||
| }, | ||
| async { result = future.await; } | ||
| }; | ||
| match result { | ||
| Ok(()) => Ok(written), | ||
| Err(err) => Err(err), | ||
| } | ||
| } | ||
|
|
||
| async fn read_to_eof(fd: &Descriptor, offset: u64) -> Vec<u8> { | ||
| let (stream, success) = fd.read_via_stream(offset); | ||
| let ret = stream.collect().await; | ||
| success.await.unwrap(); | ||
| ret | ||
| } | ||
|
|
||
| async fn test_io(dir: &Descriptor) { | ||
| let open = |path: &str, oflags: OpenFlags, fdflags: DescriptorFlags| -> _ { | ||
| dir.open_at(PathFlags::empty(), path.to_string(), oflags, fdflags) | ||
| }; | ||
| let open_r = |path: &str| -> _ { open(path, OpenFlags::empty(), DescriptorFlags::READ) }; | ||
| let creat = |path: &str| -> _ { | ||
| open( | ||
| path, | ||
| OpenFlags::CREATE | OpenFlags::EXCLUSIVE, | ||
| DescriptorFlags::READ | DescriptorFlags::WRITE, | ||
| ) | ||
| }; | ||
| let rm = |path: &str| dir.unlink_file_at(path.to_string()); | ||
|
|
||
| let a = open_r("a.txt").await.unwrap(); | ||
|
|
||
| pread(&a, 0, 0).await.unwrap(); | ||
| pread(&a, 0, 1).await.unwrap(); | ||
| pread(&a, 0, 6).await.unwrap(); | ||
| pread(&a, 0, 7).await.unwrap(); | ||
| pread(&a, 0, 17).await.unwrap(); | ||
|
|
||
| assert_eq!(&pread(&a, 1, 0).await.unwrap(), b"t"); | ||
| assert_eq!(&pread(&a, 1, 1).await.unwrap(), b"e"); | ||
| assert_eq!(&pread(&a, 1, 6).await.unwrap(), b"\n"); | ||
| assert_eq!(&pread(&a, 1, 7).await.unwrap(), b""); | ||
| assert_eq!(&pread(&a, 1, 17).await.unwrap(), b""); | ||
|
|
||
| assert_eq!(&read_to_eof(&a, 0).await, b"test-a\n"); | ||
| assert_eq!(&read_to_eof(&a, 1).await, b"est-a\n"); | ||
| assert_eq!(&read_to_eof(&a, 6).await, b"\n"); | ||
| assert_eq!(&read_to_eof(&a, 7).await, b""); | ||
| assert_eq!(&read_to_eof(&a, 17).await, b""); | ||
|
|
||
| // No-op on read-only fds. | ||
| a.sync_data().await.unwrap(); | ||
| a.sync().await.unwrap(); | ||
|
|
||
| assert_eq!(pread(&a, 1, u64::MAX).await, Err(ErrorCode::Invalid)); | ||
|
|
||
| let c = creat("c.cleanup").await.unwrap(); | ||
| assert_eq!(&read_to_eof(&c, 0).await, b""); | ||
| assert_eq!(pwrite(&c, 0, b"hello!").await, Ok(b"hello!".len())); | ||
| assert_eq!(&read_to_eof(&c, 0).await, b"hello!"); | ||
| assert_eq!(pwrite(&c, 0, b"byeee").await, Ok(b"byeee".len())); | ||
| assert_eq!(&read_to_eof(&c, 0).await, b"byeee!"); | ||
| assert_eq!(pappend(&c, b" laters!!").await, Ok(b" laters!!".len())); | ||
| assert_eq!(&read_to_eof(&c, 0).await, b"byeee! laters!!"); | ||
| c.sync_data().await.unwrap(); | ||
| assert_eq!(&read_to_eof(&open_r("c.cleanup").await.unwrap(), 0).await, | ||
| b"byeee! laters!!"); | ||
| c.sync().await.unwrap(); | ||
|
|
||
| rm("c.cleanup").await.unwrap(); | ||
| } | ||
|
|
||
| struct Component; | ||
| export!(Component); | ||
| impl exports::wasi::cli::run::Guest for Component { | ||
| async fn run() -> Result<(), ()> { | ||
| match &wasi::filesystem::preopens::get_directories()[..] { | ||
| [(dir, dirname)] if dirname == "fs-tests.dir" => { | ||
| test_io(dir).await; | ||
| } | ||
| [..] => { | ||
| eprintln!("usage: run with one open dir named 'fs-tests.dir'"); | ||
| process::exit(1) | ||
| } | ||
| }; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| unreachable!("main is a stub"); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assert may want to get deferred to the
Okreturn value of this function because a short-read might happen due to an error and the error may want to get returned instead of panicking hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am trying to understand the subtleties but I am a bit lost. If I understand you correctly,
Droppedcan be accompanied by data, is that right? If so we should add anntoDropped: bytecodealliance/wit-bindgen#1396. If not I think this assertion will never cause a failed pread, becausebytes_readanddata.len()are both incremented as part ofComplete.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're correct that
Droppedcan be accompanied by data, yeah. The lack ofnhere is definitely an oversight and the assumption was that you'd look at the length of the buffer before/after to figure out the size (but then that's inconsistent withnonStreamResult::Complete, sonshould be onDroppedandCancelledas well for consistency)I think I actually confused myself about this assertion and what it originally meant. I thought it was somehow taking the
sizeparameter to the function into account but it isn't. Otherwise though you're also correct that this can panic in the case thatDroppedcommunicates some data, which wasn't actually what I was thinking about but is true nonetheless!