forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#3848 - tiif:tokiotest, r=RalfJung
Add tokio io test After rust-lang#3804 landed, these tests passed.
- Loading branch information
Showing
7 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
GOT = sending from handle | ||
GOT = sending from handle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.