Skip to content

Commit d1550bd

Browse files
committed
Add test for tokio file io and mpsc
1 parent 4d87818 commit d1550bd

File tree

7 files changed

+72
-9
lines changed

7 files changed

+72
-9
lines changed

test_dependencies/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test_dependencies/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tempfile = "3"
2020
page_size = "0.6"
2121
# Avoid pulling in all of tokio's dependencies.
2222
# However, without `net` and `signal`, tokio uses fewer relevant system APIs.
23-
tokio = { version = "1.24", features = ["macros", "rt-multi-thread", "time", "net", "fs", "sync", "signal"] }
23+
tokio = { version = "1.24", features = ["macros", "rt-multi-thread", "time", "net", "fs", "sync", "signal", "io-util"] }
2424

2525
[target.'cfg(windows)'.dependencies]
2626
windows-sys = { version = "0.52", features = [ "Win32_Foundation", "Win32_System_Threading" ] }

tests/pass-dep/tokio/file-io.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@compile-flags: -Zmiri-disable-isolation
2+
//@only-target-linux: We only support tokio on Linux
3+
4+
use std::fs::remove_file;
5+
use tokio::fs::{File, OpenOptions};
6+
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
7+
8+
#[path = "../../utils/mod.rs"]
9+
mod utils;
10+
11+
#[tokio::main]
12+
async fn main() {
13+
test_create_and_write().await.unwrap();
14+
test_create_and_read().await.unwrap();
15+
}
16+
17+
async fn test_create_and_write() -> io::Result<()> {
18+
let path = utils::prepare("foo.txt");
19+
let mut file = File::create(&path).await?;
20+
21+
// Write 10 bytes to the file.
22+
file.write(b"some bytes").await?;
23+
assert_eq!(file.metadata().await.unwrap().len(), 10);
24+
25+
remove_file(&path).unwrap();
26+
Ok(())
27+
}
28+
29+
async fn test_create_and_read() -> io::Result<()> {
30+
let bytes = b"more bytes";
31+
let path = utils::prepare_with_content("foo.txt", bytes);
32+
let mut file = OpenOptions::new().read(true).open(&path).await.unwrap();
33+
let mut buffer = [0u8; 10];
34+
35+
// Read the whole file.
36+
file.read(&mut buffer[..]).await?;
37+
assert_eq!(&buffer, b"more bytes");
38+
39+
remove_file(&path).unwrap();
40+
Ok(())
41+
}

tests/pass-dep/tokio/mpsc-await.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@only-target-linux: We only support tokio on Linux
2+
use tokio::sync::mpsc;
3+
4+
#[tokio::main]
5+
async fn main() {
6+
let (tx, mut rx) = mpsc::channel(32);
7+
let tx2 = tx.clone();
8+
9+
tokio::spawn(async move {
10+
tx.send("sending from handle").await.unwrap();
11+
});
12+
13+
tokio::spawn(async move {
14+
tx2.send("sending from handle").await.unwrap();
15+
});
16+
17+
while let Some(message) = rx.recv().await {
18+
println!("GOT = {}", message);
19+
}
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
GOT = sending from handle
2+
GOT = sending from handle

tests/pass-dep/tokio/sleep.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//@compile-flags: -Zmiri-permissive-provenance -Zmiri-backtrace=full
2-
//@only-target-x86_64-unknown-linux: support for tokio only on linux and x86
1+
//@only-target-linux: We only support tokio on Linux
32

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

tests/pass-dep/tokio/tokio_mvp.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)