forked from rust-lang/rustup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
208 lines (187 loc) · 6.03 KB
/
test.rs
File metadata and controls
208 lines (187 loc) · 6.03 KB
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
use std::collections::HashMap;
use anyhow::Result;
use super::{Executor, Item, Kind, get_executor};
use crate::process::TestProcess;
use crate::test::test_dir;
impl Item {
/// The length of the file, for files (for stats)
fn size(&self) -> Option<usize> {
match &self.kind {
Kind::File(buf) => Some(buf.len()),
_ => None,
}
}
}
fn test_incremental_file(io_threads: &str) -> Result<()> {
let work_dir = test_dir()?;
let mut vars = HashMap::new();
vars.insert("RUSTUP_IO_THREADS".to_string(), io_threads.to_string());
let tp = TestProcess::with_vars(vars);
let mut written = 0;
let mut file_finished = false;
let mut io_executor: Box<dyn Executor> =
get_executor(32 * 1024 * 1024, tp.process.io_thread_count()?);
let (item, mut sender) = Item::write_file_segmented(
work_dir.path().join("scratch"),
0o666,
io_executor.incremental_file_state(),
)?;
// The file should be open and incomplete, and no completed chunks
assert!(io_executor.execute(item).collect::<Vec<_>>().is_empty());
let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE);
chunk.extend(b"0123456789");
chunk = chunk.finished();
sender(chunk);
let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE);
chunk.extend(b"0123456789");
chunk = chunk.finished();
sender(chunk);
loop {
for work in io_executor.completed().collect::<Vec<_>>() {
match work {
super::CompletedIo::Chunk(size) => written += size,
super::CompletedIo::Item(item) => unreachable!("{:?}", item),
}
}
if written == 20 {
break;
}
}
// sending a zero length chunk closes the file
let mut chunk = io_executor.get_buffer(super::IO_CHUNK_SIZE);
chunk = chunk.finished();
sender(chunk);
loop {
for work in io_executor.completed().collect::<Vec<_>>() {
match work {
super::CompletedIo::Chunk(_) => {}
super::CompletedIo::Item(_) => {
file_finished = true;
}
}
}
if file_finished {
break;
}
}
// no more work should be outstanding
assert!(file_finished);
assert!(io_executor.join().collect::<Vec<_>>().is_empty());
assert_eq!(io_executor.buffer_used(), 0);
// We should be able to read back the file
assert_eq!(
std::fs::read_to_string(work_dir.path().join("scratch"))?,
"01234567890123456789".to_string()
);
Ok(())
}
fn test_complete_file(io_threads: &str) -> Result<()> {
let work_dir = test_dir()?;
let mut vars = HashMap::new();
vars.insert("RUSTUP_IO_THREADS".to_string(), io_threads.to_string());
let tp = TestProcess::with_vars(vars);
let mut io_executor: Box<dyn Executor> =
get_executor(32 * 1024 * 1024, tp.process.io_thread_count()?);
let mut chunk = io_executor.get_buffer(10);
chunk.extend(b"0123456789");
assert_eq!(chunk.len(), 10);
chunk = chunk.finished();
let item = Item::write_file(work_dir.path().join("scratch"), 0o666, chunk);
assert_eq!(item.size(), Some(10));
let mut items = 0;
let mut check_item = |item: Item| {
assert_eq!(item.size(), Some(10));
items += 1;
assert_eq!(1, items);
};
let mut finished = false;
for work in io_executor.execute(item).collect::<Vec<_>>() {
// The file might complete immediately
match work {
super::CompletedIo::Chunk(size) => unreachable!("{:?}", size),
super::CompletedIo::Item(item) => {
check_item(item);
finished = true;
}
}
}
if !finished {
loop {
for work in io_executor.completed().collect::<Vec<_>>() {
match work {
super::CompletedIo::Chunk(size) => unreachable!("{:?}", size),
super::CompletedIo::Item(item) => {
check_item(item);
finished = true;
}
}
}
if finished {
break;
}
}
}
assert!(items > 0);
// no more work should be outstanding
assert!(io_executor.join().collect::<Vec<_>>().is_empty());
// We should be able to read back the file with correct content
assert_eq!(
std::fs::read_to_string(work_dir.path().join("scratch"))?,
"0123456789".to_string()
);
Ok(())
}
#[test]
fn test_incremental_file_immediate() {
test_incremental_file("1").unwrap()
}
#[test]
fn test_incremental_file_threaded() {
test_incremental_file("2").unwrap()
}
#[test]
fn test_complete_file_immediate() {
test_complete_file("1").unwrap()
}
#[test]
fn test_complete_file_threaded() {
test_complete_file("2").unwrap()
}
#[test]
fn test_effective_thread_count() {
use super::{LOW_MEMORY_THRESHOLD, effective_thread_count};
use crate::process::IoThreadCount::{Default, UserSpecified};
// Already single-threaded: no change regardless of budget
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD / 16, Default(1)),
1
);
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD / 16, Default(0)),
0
);
// Below threshold: forced to single-threaded
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD / 2, Default(8)),
1
);
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD / 2, Default(4)),
1
);
// At or above threshold: thread count unchanged
assert_eq!(effective_thread_count(LOW_MEMORY_THRESHOLD, Default(4)), 4);
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD * 2, Default(8)),
8
);
// User-specified threads are always respected
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD / 16, UserSpecified(4)),
4
);
assert_eq!(
effective_thread_count(LOW_MEMORY_THRESHOLD / 2, UserSpecified(8)),
8
);
}