Skip to content

Commit 4159d5b

Browse files
committed
test(build-std): Isolate output test to avoid spurious [BLOCKING] messages from concurrent runs
47c2095 didn't really fix the flakiness. build-std tests use the users `CARGO_HOME` for downloading registry dependencies of the standard library. This reduces disk needs of the tests, speeds up the tests, and reduces the number of network requests that could fail. However, this means all of the tests access the same locks for the package cache. In one test, we assert on the output and a `[BLOCKING]` message can show up, depending on test execution time from concurrent test runs. We are going to hack around this by having the one test that asserts on test output to use the standard `cargo-test-support` `CARGO_HOME`, rather than the users `CARGO_HOME`. There will then only be one process accessing the lock and no `[BLOCKING]` messages.
1 parent addcc8c commit 4159d5b

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

tests/build-std/main.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ use cargo_test_support::{basic_manifest, paths, project, rustc_host, str, Execs}
2525
use std::env;
2626
use std::path::Path;
2727

28-
fn enable_build_std(e: &mut Execs, arg: Option<&str>) {
29-
e.env_remove("CARGO_HOME");
30-
e.env_remove("HOME");
28+
fn enable_build_std(e: &mut Execs, arg: Option<&str>, isolated: bool) {
29+
if !isolated {
30+
e.env_remove("CARGO_HOME");
31+
e.env_remove("HOME");
32+
}
3133

3234
// And finally actually enable `build-std` for now
3335
let arg = match arg {
@@ -42,17 +44,28 @@ fn enable_build_std(e: &mut Execs, arg: Option<&str>) {
4244
trait BuildStd: Sized {
4345
fn build_std(&mut self) -> &mut Self;
4446
fn build_std_arg(&mut self, arg: &str) -> &mut Self;
47+
48+
/// Use an isolated `CARGO_HOME` environment to avoid package cache lock contention.
49+
///
50+
/// Don't use this unless you really need to assert the full stderr
51+
/// to avoid `[BLOCKING]` messages.
52+
fn build_std_isolated(&mut self) -> &mut Self;
4553
fn target_host(&mut self) -> &mut Self;
4654
}
4755

4856
impl BuildStd for Execs {
4957
fn build_std(&mut self) -> &mut Self {
50-
enable_build_std(self, None);
58+
enable_build_std(self, None, false);
5159
self
5260
}
5361

5462
fn build_std_arg(&mut self, arg: &str) -> &mut Self {
55-
enable_build_std(self, Some(arg));
63+
enable_build_std(self, Some(arg), false);
64+
self
65+
}
66+
67+
fn build_std_isolated(&mut self) -> &mut Self {
68+
enable_build_std(self, None, true);
5669
self
5770
}
5871

@@ -107,9 +120,12 @@ fn basic() {
107120
)
108121
.build();
109122

110-
p.cargo("check").build_std().target_host().run();
123+
// HACK: use an isolated the isolated CARGO_HOME environment (`build_std_isolated`)
124+
// to avoid `[BLOCKING]` messages (from lock contention with other tests)
125+
// from getting in this test's asserts
126+
p.cargo("check").build_std_isolated().target_host().run();
111127
p.cargo("build")
112-
.build_std()
128+
.build_std_isolated()
113129
.target_host()
114130
// Importantly, this should not say [UPDATING]
115131
// There have been multiple bugs where every build triggers and update.
@@ -120,7 +136,7 @@ fn basic() {
120136
"#]])
121137
.run();
122138
p.cargo("run")
123-
.build_std()
139+
.build_std_isolated()
124140
.target_host()
125141
.with_stderr_data(str![[r#"
126142
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -129,7 +145,7 @@ fn basic() {
129145
"#]])
130146
.run();
131147
p.cargo("test")
132-
.build_std()
148+
.build_std_isolated()
133149
.target_host()
134150
.with_stderr_data(str![[r#"
135151
[COMPILING] rustc-std-workspace-std [..]
@@ -379,13 +395,11 @@ fn test_proc_macro() {
379395
.file("src/lib.rs", "")
380396
.build();
381397

382-
// Download dependencies first,
383-
// so we can compare `cargo test` output without any wildcard
384-
p.cargo("fetch").build_std().run();
385398
p.cargo("test --lib")
386399
.env_remove(cargo_util::paths::dylib_path_envvar())
387400
.build_std()
388401
.with_stderr_data(str![[r#"
402+
...
389403
[COMPILING] foo v0.0.0 ([ROOT]/foo)
390404
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
391405
[RUNNING] unittests src/lib.rs (target/debug/deps/foo-[HASH])

0 commit comments

Comments
 (0)