1 extern crate futures;
2 extern crate rand;
3 extern crate tempdir;
4 extern crate tokio_fs;
5 extern crate tokio_io;
6 extern crate tokio_threadpool;
7
8 use tokio_fs::*;
9 use tokio_io::io;
10 use tokio_threadpool::*;
11
12 use futures::Future;
13 use futures::future::poll_fn;
14 use futures::sync::oneshot;
15 use rand::{thread_rng, Rng};
16 use tempdir::TempDir;
17
18 use std::fs::File as StdFile;
19 use std::io::{Read, SeekFrom};
20
21 #[test]
read_write()22 fn read_write() {
23 const NUM_CHARS: usize = 16 * 1_024;
24
25 let dir = TempDir::new("tokio-fs-tests").unwrap();
26 let file_path = dir.path().join("read_write.txt");
27
28 let contents: Vec<u8> = thread_rng().gen_ascii_chars()
29 .take(NUM_CHARS)
30 .collect::<String>()
31 .into();
32
33 let pool = Builder::new()
34 .pool_size(1)
35 .build();
36
37 let (tx, rx) = oneshot::channel();
38
39 pool.spawn({
40 let file_path = file_path.clone();
41 let contents = contents.clone();
42
43 File::create(file_path)
44 .and_then(|file| file.metadata())
45 .inspect(|&(_, ref metadata)| assert!(metadata.is_file()))
46 .and_then(move |(file, _)| io::write_all(file, contents))
47 .and_then(|(mut file, _)| {
48 poll_fn(move || file.poll_sync_all())
49 })
50 .then(|res| {
51 let _ = res.unwrap();
52 tx.send(()).unwrap();
53 Ok(())
54 })
55 });
56
57 rx.wait().unwrap();
58
59 let mut file = StdFile::open(&file_path).unwrap();
60
61 let mut dst = vec![];
62 file.read_to_end(&mut dst).unwrap();
63
64 assert_eq!(dst, contents);
65
66 let (tx, rx) = oneshot::channel();
67
68 pool.spawn({
69 File::open(file_path)
70 .and_then(|file| io::read_to_end(file, vec![]))
71 .then(move |res| {
72 let (_, buf) = res.unwrap();
73 assert_eq!(buf, contents);
74 tx.send(()).unwrap();
75 Ok(())
76 })
77 });
78
79 rx.wait().unwrap();
80 }
81
82 #[test]
metadata()83 fn metadata() {
84 let dir = TempDir::new("tokio-fs-tests").unwrap();
85 let file_path = dir.path().join("metadata.txt");
86
87 let pool = Builder::new().pool_size(1).build();
88
89 let (tx, rx) = oneshot::channel();
90
91 pool.spawn({
92 let file_path = file_path.clone();
93 let file_path2 = file_path.clone();
94 let file_path3 = file_path.clone();
95
96 tokio_fs::metadata(file_path)
97 .then(|r| {
98 let _ = r.err().unwrap();
99 Ok(())
100 })
101 .and_then(|_| File::create(file_path2))
102 .and_then(|_| tokio_fs::metadata(file_path3))
103 .then(|r| {
104 assert!(r.unwrap().is_file());
105 tx.send(())
106 })
107 });
108
109 rx.wait().unwrap();
110 }
111
112 #[test]
seek()113 fn seek() {
114 let dir = TempDir::new("tokio-fs-tests").unwrap();
115 let file_path = dir.path().join("seek.txt");
116
117 let pool = Builder::new().pool_size(1).build();
118
119 let (tx, rx) = oneshot::channel();
120
121 pool.spawn(
122 OpenOptions::new()
123 .create(true)
124 .read(true)
125 .write(true)
126 .open(file_path)
127 .and_then(|file| io::write_all(file, "Hello, world!"))
128 .and_then(|(file, _)| file.seek(SeekFrom::End(-6)))
129 .and_then(|(file, _)| io::read_exact(file, vec![0; 5]))
130 .and_then(|(file, buf)| {
131 assert_eq!(buf, b"world");
132 file.seek(SeekFrom::Start(0))
133 })
134 .and_then(|(file, _)| io::read_exact(file, vec![0; 5]))
135 .and_then(|(_, buf)| {
136 assert_eq!(buf, b"Hello");
137 Ok(())
138 })
139 .then(|r| {
140 let _ = r.unwrap();
141 tx.send(())
142 }),
143 );
144
145 rx.wait().unwrap();
146 }
147