forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.rs
219 lines (174 loc) · 5.21 KB
/
file.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#![warn(rust_2018_idioms)]
use tokio::fs::File;
use tokio::prelude::*;
use std::io::prelude::*;
use tempfile::NamedTempFile;
/*
use rand::{distributions, thread_rng, Rng};
use std::fs;
use std::io::SeekFrom;
use tempfile::Builder as TmpBuilder;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_fs::*;
*/
const HELLO: &[u8] = b"hello world...";
#[tokio::test]
async fn basic_read() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
let mut file = File::open(tempfile.path()).await.unwrap();
let mut buf = [0; 1024];
let n = file.read(&mut buf).await.unwrap();
assert_eq!(n, HELLO.len());
assert_eq!(&buf[..n], HELLO);
}
#[tokio::test]
async fn basic_write() {
let tempfile = tempfile();
let mut file = File::create(tempfile.path()).await.unwrap();
file.write_all(HELLO).await.unwrap();
file.flush().await.unwrap();
let file = std::fs::read(tempfile.path()).unwrap();
assert_eq!(file, HELLO);
}
#[tokio::test]
#[cfg(unix)]
async fn unix_fd() {
use std::os::unix::io::AsRawFd;
let tempfile = tempfile();
let file = File::create(tempfile.path()).await.unwrap();
assert!(file.as_raw_fd() as u64 > 0);
}
#[tokio::test]
#[cfg(windows)]
async fn windows_handle() {
use std::os::windows::io::AsRawHandle;
let tempfile = tempfile();
let file = File::create(tempfile.path()).await.unwrap();
assert!(file.as_raw_handle() as u64 > 0);
}
fn tempfile() -> NamedTempFile {
NamedTempFile::new().unwrap()
}
/*
mod pool;
#[test]
fn read_write() {
const NUM_CHARS: usize = 16 * 1_024;
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()
.sample_iter(&distributions::Alphanumeric)
.take(NUM_CHARS)
.collect::<String>()
.into();
let file_path_2 = file_path.clone();
let contents_2 = contents.clone();
pool::run(async move {
let mut file = File::create(file_path).await?;
let metadata = file.metadata().await?;
assert!(metadata.is_file());
file.write(&contents).await?;
file.sync_all().await?;
Ok(())
});
let dst = fs::read(&file_path_2).unwrap();
assert_eq!(dst, contents_2);
pool::run(async move {
let buf = read(file_path_2).await?;
assert_eq!(buf, contents_2);
Ok(())
});
}
#[test]
fn read_write_helpers() {
const NUM_CHARS: usize = 16 * 1_024;
let dir = TmpBuilder::new()
.prefix("tokio-fs-tests")
.tempdir()
.unwrap();
let file_path = dir.path().join("read_write_all.txt");
let contents: Vec<u8> = thread_rng()
.sample_iter(&distributions::Alphanumeric)
.take(NUM_CHARS)
.collect::<String>()
.into();
let file_path_2 = file_path.clone();
let contents_2 = contents.clone();
pool::run(async move {
write(file_path, contents).await?;
Ok(())
});
let dst = fs::read(&file_path_2).unwrap();
assert_eq!(dst, contents_2);
pool::run(async move {
let buf = read(file_path_2).await?;
assert_eq!(buf, contents_2);
Ok(())
});
}
#[test]
fn metadata() {
let dir = TmpBuilder::new()
.prefix("tokio-fs-tests")
.tempdir()
.unwrap();
let file_path = dir.path().join("metadata.txt");
pool::run(async move {
assert!(tokio_fs::metadata(file_path.clone()).await.is_err());
File::create(file_path.clone()).await?;
let metadata = tokio_fs::metadata(file_path.clone()).await?;
assert!(metadata.is_file());
Ok(())
});
}
#[test]
fn seek() {
let dir = TmpBuilder::new()
.prefix("tokio-fs-tests")
.tempdir()
.unwrap();
let file_path = dir.path().join("seek.txt");
pool::run(async move {
let mut options = OpenOptions::new();
options.create(true).read(true).write(true);
let mut file = options.open(file_path).await.unwrap();
assert!(file.write(b"Hello, world!").await.is_ok());
file.seek(SeekFrom::End(-6)).await.unwrap();
let mut buf = vec![0; 5];
assert!(file.read(buf.as_mut()).await.is_ok());
assert_eq!(buf, b"world");
file.seek(SeekFrom::Start(0)).await.unwrap();
let mut buf = vec![0; 5];
assert!(file.read(buf.as_mut()).await.is_ok());
assert_eq!(buf, b"Hello");
Ok(())
});
}
#[test]
fn clone() {
use std::io::prelude::*;
let dir = TmpBuilder::new()
.prefix("tokio-fs-tests")
.tempdir()
.unwrap();
let file_path = dir.path().join("clone.txt");
let file_path_2 = file_path.clone();
pool::run(async move {
let mut file = File::create(file_path.clone()).await.unwrap();
let mut clone = file.try_clone().await.unwrap();
assert!(AsyncWriteExt::write(&mut file, b"clone ").await.is_ok());
assert!(AsyncWriteExt::write(&mut clone, b"successful")
.await
.is_ok());
Ok(())
});
let mut file = std::fs::File::open(&file_path_2).unwrap();
let mut dst = vec![];
file.read_to_end(&mut dst).unwrap();
assert_eq!(dst, b"clone successful")
}
*/