Skip to content

Commit

Permalink
Add test for tokio file and mpsc
Browse files Browse the repository at this point in the history
  • Loading branch information
tiif committed Aug 28, 2024
1 parent 297482d commit 1df24ef
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
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"] }

[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-x86_64-unknown-linux: support for tokio only on linux and x86

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.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@only-target-x86_64-unknown-linux: support for tokio only on linux and x86
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 first handle").await.unwrap();
});

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

while let Some(message) = rx.recv().await {
println!("GOT = {}", message);
}
}
2 changes: 2 additions & 0 deletions tests/pass-dep/tokio/mpsc.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GOT = sending from first handle
GOT = sending from second handle

0 comments on commit 1df24ef

Please sign in to comment.