Skip to content
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

Add tokio io test #3848

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test_dependencies/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test_dependencies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tempfile = "3"
page_size = "0.6"
# Avoid pulling in all of tokio's dependencies.
# However, without `net` and `signal`, tokio uses fewer relevant system APIs.
tokio = { version = "1.24", features = ["macros", "rt-multi-thread", "time", "net", "fs", "sync", "signal"] }
tokio = { version = "1.24", features = ["macros", "rt-multi-thread", "time", "net", "fs", "sync", "signal", "io-util"] }
RalfJung marked this conversation as resolved.
Show resolved Hide resolved

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52", features = [ "Win32_Foundation", "Win32_System_Threading" ] }
Expand Down
41 changes: 41 additions & 0 deletions tests/pass-dep/tokio/file-io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@compile-flags: -Zmiri-disable-isolation
//@only-target-linux: We only support tokio on Linux

use std::fs::remove_file;
use tokio::fs::{File, OpenOptions};
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};

#[path = "../../utils/mod.rs"]
mod utils;

#[tokio::main]
async fn main() {
test_create_and_write().await.unwrap();
test_create_and_read().await.unwrap();
}

async fn test_create_and_write() -> io::Result<()> {
let path = utils::prepare("foo.txt");
let mut file = File::create(&path).await?;

// Write 10 bytes to the file.
file.write(b"some bytes").await?;
assert_eq!(file.metadata().await.unwrap().len(), 10);

remove_file(&path).unwrap();
Ok(())
}

async fn test_create_and_read() -> io::Result<()> {
let bytes = b"more bytes";
let path = utils::prepare_with_content("foo.txt", bytes);
let mut file = OpenOptions::new().read(true).open(&path).await.unwrap();
let mut buffer = [0u8; 10];

// Read the whole file.
file.read(&mut buffer[..]).await?;
assert_eq!(&buffer, b"more bytes");

remove_file(&path).unwrap();
Ok(())
}
20 changes: 20 additions & 0 deletions tests/pass-dep/tokio/mpsc-await.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@only-target-linux: We only support tokio on Linux
use tokio::sync::mpsc;

#[tokio::main]
async fn main() {
let (tx, mut rx) = mpsc::channel(32);
let tx2 = tx.clone();

tokio::spawn(async move {
tx.send("sending from handle").await.unwrap();
});

tokio::spawn(async move {
tx2.send("sending from handle").await.unwrap();
});

while let Some(message) = rx.recv().await {
println!("GOT = {}", message);
}
}
2 changes: 2 additions & 0 deletions tests/pass-dep/tokio/mpsc-await.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GOT = sending from handle
GOT = sending from handle
3 changes: 1 addition & 2 deletions tests/pass-dep/tokio/sleep.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@compile-flags: -Zmiri-permissive-provenance -Zmiri-backtrace=full
//@only-target-x86_64-unknown-linux: support for tokio only on linux and x86
//@only-target-linux: We only support tokio on Linux

use tokio::time::{sleep, Duration, Instant};

Expand Down
6 changes: 0 additions & 6 deletions tests/pass-dep/tokio/tokio_mvp.rs

This file was deleted.