-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Tests fail with --test-threads 1 #10479
Description
Observation
Many tests fail when run with --test-threads 1. I only tried to run the pallet tests, eg:
cargo test -p "pallet-*" --no-fail-fast -- --test-threads 1It also seems to happen with --features=runtime-benchmarks.
Maybe someone can check it for tests outside of pallet-*. Polkadot and Cumulus took too long on my machine to compile.
Assumed problem(s)
Thread local vars
The first test that fails (assets/freezer_should_work) should be fixed after #10473 by line which resets a dirty thread local var.
I first spotted it by running Tarpaulin on Substrate, but Tarpaulin was not the problem.
The fact that the test used thread local vars and tarpaulin implicitly forces --test-threads 1 seems to cause at least some of the failures.
The following tests only fails with --test-threads 1:
use std::cell::RefCell;
thread_local! {
static STICKY: RefCell<bool> = RefCell::new(false);
}
#[test]
fn tarpauline() {
// STICKY must be false since we just started the test.
assert!(!STICKY.with(|s| *s.borrow()));
// Set to true.
STICKY.with(|s| s.replace(true));
}
#[test]
fn tarpauline2() {
// STICKY must be false since we just started the test.
assert!(!STICKY.with(|s| *s.borrow())); // <- FAILS HERE
// Set to true.
STICKY.with(|s| s.replace(true));
}As far as I understand it: cargo test normally schedules the tests on multiple threads, which means that the STICKY var is false for each #[test].
When --test-threads 1 is used, the tests run consecutively and the second test that is executed gets the sticky/dirty value of the first test function; causing the failure.
Other causes
I did not have the time to look into the other failures to see if they could all be failing because of dirty thread locals.
Maybe there is more amiss.
Next
Please confirm and decide if this should be fixed.
Theoretically this could also be changed in cargo test, such that it resets thread locals before each #[test] 🤔
Metadata
Metadata
Assignees
Labels
Type
Projects
Status