mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-23 00:00:10 +02:00
committed by
Carl Lerche
parent
09f2ac85bf
commit
33a216e4c1
+5
-16
@@ -1,32 +1,21 @@
|
||||
extern crate futures;
|
||||
extern crate tempdir;
|
||||
extern crate tokio_fs;
|
||||
extern crate tokio_threadpool;
|
||||
|
||||
use futures::sync::oneshot;
|
||||
use futures::{Future, Stream};
|
||||
use std::fs;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tempdir::TempDir;
|
||||
use tokio_fs::*;
|
||||
use tokio_threadpool::Builder;
|
||||
|
||||
fn run_in_pool<F>(f: F)
|
||||
where
|
||||
F: Future<Item = (), Error = std::io::Error> + Send + 'static,
|
||||
{
|
||||
let pool = Builder::new().pool_size(1).build();
|
||||
let (tx, rx) = oneshot::channel::<()>();
|
||||
pool.spawn(f.then(|_| tx.send(())));
|
||||
rx.wait().unwrap()
|
||||
}
|
||||
mod pool;
|
||||
|
||||
#[test]
|
||||
fn create() {
|
||||
let base_dir = TempDir::new("base").unwrap();
|
||||
let new_dir = base_dir.path().join("foo");
|
||||
|
||||
run_in_pool({ create_dir(new_dir.clone()) });
|
||||
pool::run({ create_dir(new_dir.clone()) });
|
||||
|
||||
assert!(new_dir.is_dir());
|
||||
}
|
||||
@@ -36,7 +25,7 @@ fn create_all() {
|
||||
let base_dir = TempDir::new("base").unwrap();
|
||||
let new_dir = base_dir.path().join("foo").join("bar");
|
||||
|
||||
run_in_pool({ create_dir_all(new_dir.clone()) });
|
||||
pool::run({ create_dir_all(new_dir.clone()) });
|
||||
|
||||
assert!(new_dir.is_dir());
|
||||
}
|
||||
@@ -48,7 +37,7 @@ fn remove() {
|
||||
|
||||
fs::create_dir(new_dir.clone()).unwrap();
|
||||
|
||||
run_in_pool({ remove_dir(new_dir.clone()) });
|
||||
pool::run({ remove_dir(new_dir.clone()) });
|
||||
|
||||
assert!(!new_dir.exists());
|
||||
}
|
||||
@@ -66,7 +55,7 @@ fn read() {
|
||||
|
||||
let f = files.clone();
|
||||
let p = p.to_path_buf();
|
||||
run_in_pool({
|
||||
pool::run({
|
||||
read_dir(p).flatten_stream().for_each(move |e| {
|
||||
let s = e.file_name().to_str().unwrap().to_string();
|
||||
f.lock().unwrap().push(s);
|
||||
|
||||
+25
-45
@@ -3,26 +3,28 @@ extern crate rand;
|
||||
extern crate tempfile;
|
||||
extern crate tokio_fs;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_threadpool;
|
||||
|
||||
use tokio_fs::*;
|
||||
use tokio_io::io;
|
||||
use tokio_threadpool::*;
|
||||
|
||||
use futures::Future;
|
||||
use futures::future::poll_fn;
|
||||
use futures::sync::oneshot;
|
||||
use rand::{thread_rng, Rng, distributions};
|
||||
use futures::Future;
|
||||
use rand::{distributions, thread_rng, Rng};
|
||||
use tempfile::Builder as TmpBuilder;
|
||||
|
||||
use std::fs::File as StdFile;
|
||||
use std::io::{Read, SeekFrom};
|
||||
|
||||
mod pool;
|
||||
|
||||
#[test]
|
||||
fn read_write() {
|
||||
const NUM_CHARS: usize = 16 * 1_024;
|
||||
|
||||
let dir = TmpBuilder::new().prefix("tokio-fs-tests").tempdir().unwrap();
|
||||
let dir = TmpBuilder::new()
|
||||
.prefix("tokio-fs-tests")
|
||||
.tempdir()
|
||||
.unwrap();
|
||||
let file_path = dir.path().join("read_write.txt");
|
||||
|
||||
let contents: Vec<u8> = thread_rng()
|
||||
@@ -31,13 +33,7 @@ fn read_write() {
|
||||
.collect::<String>()
|
||||
.into();
|
||||
|
||||
let pool = Builder::new()
|
||||
.pool_size(1)
|
||||
.build();
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
pool.spawn({
|
||||
pool::run({
|
||||
let file_path = file_path.clone();
|
||||
let contents = contents.clone();
|
||||
|
||||
@@ -45,18 +41,13 @@ fn read_write() {
|
||||
.and_then(|file| file.metadata())
|
||||
.inspect(|&(_, ref metadata)| assert!(metadata.is_file()))
|
||||
.and_then(move |(file, _)| io::write_all(file, contents))
|
||||
.and_then(|(mut file, _)| {
|
||||
poll_fn(move || file.poll_sync_all())
|
||||
})
|
||||
.and_then(|(mut file, _)| poll_fn(move || file.poll_sync_all()))
|
||||
.then(|res| {
|
||||
let _ = res.unwrap();
|
||||
tx.send(()).unwrap();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
rx.wait().unwrap();
|
||||
|
||||
let mut file = StdFile::open(&file_path).unwrap();
|
||||
|
||||
let mut dst = vec![];
|
||||
@@ -64,32 +55,26 @@ fn read_write() {
|
||||
|
||||
assert_eq!(dst, contents);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
pool.spawn({
|
||||
pool::run({
|
||||
File::open(file_path)
|
||||
.and_then(|file| io::read_to_end(file, vec![]))
|
||||
.then(move |res| {
|
||||
let (_, buf) = res.unwrap();
|
||||
assert_eq!(buf, contents);
|
||||
tx.send(()).unwrap();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
rx.wait().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata() {
|
||||
let dir = TmpBuilder::new().prefix("tokio-fs-tests").tempdir().unwrap();
|
||||
let dir = TmpBuilder::new()
|
||||
.prefix("tokio-fs-tests")
|
||||
.tempdir()
|
||||
.unwrap();
|
||||
let file_path = dir.path().join("metadata.txt");
|
||||
|
||||
let pool = Builder::new().pool_size(1).build();
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
pool.spawn({
|
||||
pool::run({
|
||||
let file_path = file_path.clone();
|
||||
let file_path2 = file_path.clone();
|
||||
let file_path3 = file_path.clone();
|
||||
@@ -103,23 +88,20 @@ fn metadata() {
|
||||
.and_then(|_| tokio_fs::metadata(file_path3))
|
||||
.then(|r| {
|
||||
assert!(r.unwrap().is_file());
|
||||
tx.send(())
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
rx.wait().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seek() {
|
||||
let dir = TmpBuilder::new().prefix("tokio-fs-tests").tempdir().unwrap();
|
||||
let dir = TmpBuilder::new()
|
||||
.prefix("tokio-fs-tests")
|
||||
.tempdir()
|
||||
.unwrap();
|
||||
let file_path = dir.path().join("seek.txt");
|
||||
|
||||
let pool = Builder::new().pool_size(1).build();
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
pool.spawn(
|
||||
pool::run({
|
||||
OpenOptions::new()
|
||||
.create(true)
|
||||
.read(true)
|
||||
@@ -139,9 +121,7 @@ fn seek() {
|
||||
})
|
||||
.then(|r| {
|
||||
let _ = r.unwrap();
|
||||
tx.send(())
|
||||
}),
|
||||
);
|
||||
|
||||
rx.wait().unwrap();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
extern crate futures;
|
||||
extern crate tempdir;
|
||||
extern crate tokio_fs;
|
||||
|
||||
use futures::Future;
|
||||
use std::fs;
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use tempdir::TempDir;
|
||||
use tokio_fs::*;
|
||||
|
||||
mod pool;
|
||||
|
||||
#[test]
|
||||
fn test_hard_link() {
|
||||
let dir = TempDir::new("base").unwrap();
|
||||
let src = dir.path().join("src.txt");
|
||||
let dst = dir.path().join("dst.txt");
|
||||
|
||||
{
|
||||
let mut file = fs::File::create(&src).unwrap();
|
||||
file.write_all(b"hello").unwrap();
|
||||
}
|
||||
|
||||
pool::run({ hard_link(src, dst.clone()) });
|
||||
|
||||
let mut content = String::new();
|
||||
|
||||
{
|
||||
let file = fs::File::open(dst).unwrap();
|
||||
let mut reader = BufReader::new(file);
|
||||
reader.read_to_string(&mut content).unwrap();
|
||||
}
|
||||
|
||||
assert!(content == "hello");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn test_symlink() {
|
||||
let dir = TempDir::new("base").unwrap();
|
||||
let src = dir.path().join("src.txt");
|
||||
let dst = dir.path().join("dst.txt");
|
||||
|
||||
{
|
||||
let mut file = fs::File::create(&src).unwrap();
|
||||
file.write_all(b"hello").unwrap();
|
||||
}
|
||||
|
||||
pool::run({ os::unix::symlink(src.clone(), dst.clone()) });
|
||||
|
||||
let mut content = String::new();
|
||||
|
||||
{
|
||||
let file = fs::File::open(dst.clone()).unwrap();
|
||||
let mut reader = BufReader::new(file);
|
||||
reader.read_to_string(&mut content).unwrap();
|
||||
}
|
||||
|
||||
assert!(content == "hello");
|
||||
|
||||
pool::run({ read_link(dst.clone()).map(move |x| assert!(x == src)) });
|
||||
pool::run({ symlink_metadata(dst.clone()).map(move |x| assert!(x.file_type().is_symlink())) });
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
extern crate futures;
|
||||
extern crate tokio_threadpool;
|
||||
|
||||
use self::tokio_threadpool::Builder;
|
||||
use futures::sync::oneshot;
|
||||
use futures::Future;
|
||||
use std::io;
|
||||
|
||||
pub fn run<F>(f: F)
|
||||
where
|
||||
F: Future<Item = (), Error = io::Error> + Send + 'static,
|
||||
{
|
||||
let pool = Builder::new().pool_size(1).build();
|
||||
let (tx, rx) = oneshot::channel::<()>();
|
||||
pool.spawn(f.then(|_| tx.send(())));
|
||||
rx.wait().unwrap()
|
||||
}
|
||||
Reference in New Issue
Block a user