Skip to content

Commit 393c56a

Browse files
trxcllntsylvestre
authored andcommitted
refactor system and dist tests to isolated sccache clients so the tests can run in parallel
1 parent 9fb942e commit 393c56a

File tree

5 files changed

+1467
-1440
lines changed

5 files changed

+1467
-1440
lines changed

tests/dist.rs

Lines changed: 107 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,29 @@ extern crate sccache;
77
extern crate serde_json;
88

99
use crate::harness::{
10-
cargo_command, get_stats, init_cargo, sccache_command, start_local_daemon, stop_local_daemon,
11-
write_json_cfg, write_source,
10+
client::{sccache_client_cfg, SccacheClient},
11+
dist::{cargo_command, sccache_dist_path, DistSystem},
12+
init_cargo, write_source,
1213
};
1314
use assert_cmd::prelude::*;
1415
use sccache::config::HTTPUrl;
1516
use sccache::dist::{
1617
AssignJobResult, CompileCommand, InputsReader, JobId, JobState, RunJobResult, ServerIncoming,
1718
ServerOutgoing, SubmitToolchainResult, Toolchain, ToolchainReader,
1819
};
19-
use std::ffi::OsStr;
2020
use std::path::Path;
2121
use std::process::Output;
2222

2323
use sccache::errors::*;
2424

2525
mod harness;
2626

27-
fn basic_compile(tmpdir: &Path, sccache_cfg_path: &Path, sccache_cached_cfg_path: &Path) {
28-
let envs: Vec<(_, &OsStr)> = vec![
29-
("RUST_BACKTRACE", "1".as_ref()),
30-
("SCCACHE_LOG", "debug".as_ref()),
31-
("SCCACHE_CONF", sccache_cfg_path.as_ref()),
32-
("SCCACHE_CACHED_CONF", sccache_cached_cfg_path.as_ref()),
33-
];
27+
fn basic_compile(client: &SccacheClient, tmpdir: &Path) {
3428
let source_file = "x.c";
3529
let obj_file = "x.o";
3630
write_source(tmpdir, source_file, "#if !defined(SCCACHE_TEST_DEFINE)\n#error SCCACHE_TEST_DEFINE is not defined\n#endif\nint x() { return 5; }");
37-
sccache_command()
31+
client
32+
.cmd()
3833
.args([
3934
std::env::var("CC")
4035
.unwrap_or_else(|_| "gcc".to_string())
@@ -45,21 +40,13 @@ fn basic_compile(tmpdir: &Path, sccache_cfg_path: &Path, sccache_cached_cfg_path
4540
.arg(tmpdir.join(source_file))
4641
.arg("-o")
4742
.arg(tmpdir.join(obj_file))
48-
.envs(envs)
43+
.env("RUST_BACKTRACE", "1")
44+
.env("SCCACHE_RECACHE", "1")
4945
.assert()
5046
.success();
5147
}
5248

53-
fn rust_compile(tmpdir: &Path, sccache_cfg_path: &Path, sccache_cached_cfg_path: &Path) -> Output {
54-
let sccache_path = assert_cmd::cargo::cargo_bin("sccache").into_os_string();
55-
let envs: Vec<(_, &OsStr)> = vec![
56-
("RUSTC_WRAPPER", sccache_path.as_ref()),
57-
("CARGO_TARGET_DIR", "target".as_ref()),
58-
("RUST_BACKTRACE", "1".as_ref()),
59-
("SCCACHE_LOG", "debug".as_ref()),
60-
("SCCACHE_CONF", sccache_cfg_path.as_ref()),
61-
("SCCACHE_CACHED_CONF", sccache_cached_cfg_path.as_ref()),
62-
];
49+
fn rust_compile(client: &SccacheClient, tmpdir: &Path) -> Output {
6350
let cargo_name = "sccache-dist-test";
6451
let cargo_path = init_cargo(tmpdir, cargo_name);
6552

@@ -87,7 +74,16 @@ fn rust_compile(tmpdir: &Path, sccache_cfg_path: &Path, sccache_cached_cfg_path:
8774
cargo_command()
8875
.current_dir(cargo_path)
8976
.args(["build", "--release"])
90-
.envs(envs)
77+
.envs(
78+
client
79+
.cmd()
80+
.get_envs()
81+
.map(|(k, v)| (k, v.unwrap_or_default())),
82+
)
83+
.env("RUSTC_WRAPPER", &client.path)
84+
.env("CARGO_TARGET_DIR", "target")
85+
.env("RUST_BACKTRACE", "1")
86+
.env("SCCACHE_RECACHE", "1")
9187
.output()
9288
.unwrap()
9389
}
@@ -96,7 +92,7 @@ pub fn dist_test_sccache_client_cfg(
9692
tmpdir: &Path,
9793
scheduler_url: HTTPUrl,
9894
) -> sccache::config::FileConfig {
99-
let mut sccache_cfg = harness::sccache_client_cfg(tmpdir, false);
95+
let mut sccache_cfg = sccache_client_cfg(tmpdir, false);
10096
sccache_cfg.cache.disk.as_mut().unwrap().size = 0;
10197
sccache_cfg.dist.scheduler_url = Some(scheduler_url);
10298
sccache_cfg
@@ -110,29 +106,27 @@ fn test_dist_basic() {
110106
.tempdir()
111107
.unwrap();
112108
let tmpdir = tmpdir.path();
113-
let sccache_dist = harness::sccache_dist_path();
109+
let sccache_dist = sccache_dist_path();
114110

115-
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
111+
let mut system = DistSystem::new(&sccache_dist, tmpdir);
116112
system.add_scheduler();
117113
system.add_server();
118114

119-
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
120-
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
121-
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
122-
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
123-
124-
stop_local_daemon();
125-
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
126-
basic_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
127-
128-
get_stats(|info| {
129-
assert_eq!(1, info.stats.dist_compiles.values().sum::<usize>());
130-
assert_eq!(0, info.stats.dist_errors);
131-
assert_eq!(1, info.stats.compile_requests);
132-
assert_eq!(1, info.stats.requests_executed);
133-
assert_eq!(0, info.stats.cache_hits.all());
134-
assert_eq!(1, info.stats.cache_misses.all());
135-
});
115+
let client = system.new_client(&dist_test_sccache_client_cfg(
116+
tmpdir,
117+
system.scheduler_url(),
118+
));
119+
120+
basic_compile(&client, tmpdir);
121+
122+
let stats = client.stats().unwrap();
123+
124+
assert_eq!(1, stats.dist_compiles.values().sum::<usize>());
125+
assert_eq!(0, stats.dist_errors);
126+
assert_eq!(1, stats.compile_requests);
127+
assert_eq!(1, stats.requests_executed);
128+
assert_eq!(0, stats.cache_hits.all());
129+
assert_eq!(1, stats.cache_misses.all());
136130
}
137131

138132
#[test]
@@ -143,32 +137,31 @@ fn test_dist_restartedserver() {
143137
.tempdir()
144138
.unwrap();
145139
let tmpdir = tmpdir.path();
146-
let sccache_dist = harness::sccache_dist_path();
140+
let sccache_dist = sccache_dist_path();
147141

148-
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
142+
let mut system = DistSystem::new(&sccache_dist, tmpdir);
149143
system.add_scheduler();
150144
let server_handle = system.add_server();
151145

152-
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
153-
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
154-
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
155-
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
146+
let client = system.new_client(&dist_test_sccache_client_cfg(
147+
tmpdir,
148+
system.scheduler_url(),
149+
));
156150

157-
stop_local_daemon();
158-
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
159-
basic_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
151+
basic_compile(&client, tmpdir);
160152

161153
system.restart_server(&server_handle);
162-
basic_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
163-
164-
get_stats(|info| {
165-
assert_eq!(2, info.stats.dist_compiles.values().sum::<usize>());
166-
assert_eq!(0, info.stats.dist_errors);
167-
assert_eq!(2, info.stats.compile_requests);
168-
assert_eq!(2, info.stats.requests_executed);
169-
assert_eq!(0, info.stats.cache_hits.all());
170-
assert_eq!(2, info.stats.cache_misses.all());
171-
});
154+
155+
basic_compile(&client, tmpdir);
156+
157+
let stats = client.stats().unwrap();
158+
159+
assert_eq!(2, stats.dist_compiles.values().sum::<usize>());
160+
assert_eq!(0, stats.dist_errors);
161+
assert_eq!(2, stats.compile_requests);
162+
assert_eq!(2, stats.requests_executed);
163+
assert_eq!(0, stats.cache_hits.all());
164+
assert_eq!(2, stats.cache_misses.all());
172165
}
173166

174167
#[test]
@@ -179,28 +172,26 @@ fn test_dist_nobuilder() {
179172
.tempdir()
180173
.unwrap();
181174
let tmpdir = tmpdir.path();
182-
let sccache_dist = harness::sccache_dist_path();
175+
let sccache_dist = sccache_dist_path();
183176

184-
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
177+
let mut system = DistSystem::new(&sccache_dist, tmpdir);
185178
system.add_scheduler();
186179

187-
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
188-
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
189-
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
190-
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
191-
192-
stop_local_daemon();
193-
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
194-
basic_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
195-
196-
get_stats(|info| {
197-
assert_eq!(0, info.stats.dist_compiles.values().sum::<usize>());
198-
assert_eq!(1, info.stats.dist_errors);
199-
assert_eq!(1, info.stats.compile_requests);
200-
assert_eq!(1, info.stats.requests_executed);
201-
assert_eq!(0, info.stats.cache_hits.all());
202-
assert_eq!(1, info.stats.cache_misses.all());
203-
});
180+
let client = system.new_client(&dist_test_sccache_client_cfg(
181+
tmpdir,
182+
system.scheduler_url(),
183+
));
184+
185+
basic_compile(&client, tmpdir);
186+
187+
let stats = client.stats().unwrap();
188+
189+
assert_eq!(0, stats.dist_compiles.values().sum::<usize>());
190+
assert_eq!(1, stats.dist_errors);
191+
assert_eq!(1, stats.compile_requests);
192+
assert_eq!(1, stats.requests_executed);
193+
assert_eq!(0, stats.cache_hits.all());
194+
assert_eq!(1, stats.cache_misses.all());
204195
}
205196

206197
struct FailingServer;
@@ -244,97 +235,64 @@ fn test_dist_failingserver() {
244235
.tempdir()
245236
.unwrap();
246237
let tmpdir = tmpdir.path();
247-
let sccache_dist = harness::sccache_dist_path();
238+
let sccache_dist = sccache_dist_path();
248239

249-
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
240+
let mut system = DistSystem::new(&sccache_dist, tmpdir);
250241
system.add_scheduler();
251242
system.add_custom_server(FailingServer);
252243

253-
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
254-
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
255-
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
256-
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
257-
258-
stop_local_daemon();
259-
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
260-
basic_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
261-
262-
get_stats(|info| {
263-
assert_eq!(0, info.stats.dist_compiles.values().sum::<usize>());
264-
assert_eq!(1, info.stats.dist_errors);
265-
assert_eq!(1, info.stats.compile_requests);
266-
assert_eq!(1, info.stats.requests_executed);
267-
assert_eq!(0, info.stats.cache_hits.all());
268-
assert_eq!(1, info.stats.cache_misses.all());
269-
});
270-
}
271-
272-
#[test]
273-
#[cfg_attr(not(feature = "dist-tests"), ignore)]
274-
fn test_dist_cargo_build() {
275-
let tmpdir = tempfile::Builder::new()
276-
.prefix("sccache_dist_test")
277-
.tempdir()
278-
.unwrap();
279-
let tmpdir = tmpdir.path();
280-
let sccache_dist = harness::sccache_dist_path();
244+
let client = system.new_client(&dist_test_sccache_client_cfg(
245+
tmpdir,
246+
system.scheduler_url(),
247+
));
281248

282-
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
283-
system.add_scheduler();
284-
let _server_handle = system.add_server();
249+
basic_compile(&client, tmpdir);
285250

286-
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
287-
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
288-
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
289-
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
251+
let stats = client.stats().unwrap();
290252

291-
stop_local_daemon();
292-
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
293-
rust_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path)
294-
.assert()
295-
.success();
296-
get_stats(|info| {
297-
assert_eq!(1, info.stats.dist_compiles.values().sum::<usize>());
298-
assert_eq!(0, info.stats.dist_errors);
299-
assert_eq!(5, info.stats.compile_requests);
300-
assert_eq!(1, info.stats.requests_executed);
301-
assert_eq!(0, info.stats.cache_hits.all());
302-
assert_eq!(1, info.stats.cache_misses.all());
303-
});
253+
assert_eq!(0, stats.dist_compiles.values().sum::<usize>());
254+
assert_eq!(1, stats.dist_errors);
255+
assert_eq!(1, stats.compile_requests);
256+
assert_eq!(1, stats.requests_executed);
257+
assert_eq!(0, stats.cache_hits.all());
258+
assert_eq!(1, stats.cache_misses.all());
304259
}
305260

306261
#[test]
307262
#[cfg_attr(not(feature = "dist-tests"), ignore)]
308-
fn test_dist_cargo_makeflags() {
263+
fn test_dist_cargo_build() {
309264
let tmpdir = tempfile::Builder::new()
310265
.prefix("sccache_dist_test")
311266
.tempdir()
312267
.unwrap();
313268
let tmpdir = tmpdir.path();
314-
let sccache_dist = harness::sccache_dist_path();
269+
let sccache_dist = sccache_dist_path();
315270

316-
let mut system = harness::DistSystem::new(&sccache_dist, tmpdir);
271+
let mut system = DistSystem::new(&sccache_dist, tmpdir);
317272
system.add_scheduler();
318273
let _server_handle = system.add_server();
319274

320-
let sccache_cfg = dist_test_sccache_client_cfg(tmpdir, system.scheduler_url());
321-
let sccache_cfg_path = tmpdir.join("sccache-cfg.json");
322-
write_json_cfg(tmpdir, "sccache-cfg.json", &sccache_cfg);
323-
let sccache_cached_cfg_path = tmpdir.join("sccache-cached-cfg");
275+
let client = system.new_client(&dist_test_sccache_client_cfg(
276+
tmpdir,
277+
system.scheduler_url(),
278+
));
324279

325-
stop_local_daemon();
326-
start_local_daemon(&sccache_cfg_path, &sccache_cached_cfg_path);
327-
let compile_output = rust_compile(tmpdir, &sccache_cfg_path, &sccache_cached_cfg_path);
280+
let compile_output = rust_compile(&client, tmpdir);
328281

282+
// Ensure sccache ignores inherited jobservers in CARGO_MAKEFLAGS
329283
assert!(!String::from_utf8_lossy(&compile_output.stderr)
330284
.contains("warning: failed to connect to jobserver from environment variable"));
331285

332-
get_stats(|info| {
333-
assert_eq!(1, info.stats.dist_compiles.values().sum::<usize>());
334-
assert_eq!(0, info.stats.dist_errors);
335-
assert_eq!(5, info.stats.compile_requests);
336-
assert_eq!(1, info.stats.requests_executed);
337-
assert_eq!(0, info.stats.cache_hits.all());
338-
assert_eq!(1, info.stats.cache_misses.all());
339-
});
286+
// Assert compilation succeeded
287+
compile_output.assert().success();
288+
289+
let stats = client.stats().unwrap();
290+
291+
assert_eq!(1, stats.dist_compiles.values().sum::<usize>());
292+
assert_eq!(0, stats.dist_errors);
293+
// check >= 5 because cargo >=1.82 does additional requests with -vV
294+
assert!(stats.compile_requests >= 5);
295+
assert_eq!(1, stats.requests_executed);
296+
assert_eq!(0, stats.cache_hits.all());
297+
assert_eq!(1, stats.cache_misses.all());
340298
}

0 commit comments

Comments
 (0)