Description
Hello! I'm trying to run the test suite for one of my Rust programs with only a single thread, because each test changes state in an external database and needs to clean up after itself before the next test starts. (This clean up is implemented via the Drop trait on a struct being created in each test.)
However, each of my tests fails intermittently, even when running cargo test -j1
. I put print statements in the constructor and Drop::drop
to see if each struct was being dropped before the next one was being created. What I discovered was that test runs frequently printed multiple constructor calls before their corresponding drops. Perhaps I'm misunderstanding what's actually happening, but this seems to suggest that test cases are actually running in parallel even though I specified that only one job should be used.
Here is a small program that demonstrates the issue:
struct Foo;
impl Foo {
fn new() -> Foo {
println!("new");
Foo
}
}
impl Drop for Foo {
fn drop(&mut self) {
println!("drop");
}
}
#[test]
fn one() {
Foo::new();
}
#[test]
fn two() {
Foo::new();
}
#[test]
fn three() {
Foo::new();
}
#[test]
fn four() {
Foo::new();
}
Example output:
$ cargo test --jobs 1 -- --nocapture
Running target/debug/thread_test-8ec58673041f054d
running 4 tests
new
new
drop
drop
new
drop
new
drop
test two ... ok
test three ... ok
test four ... ok
test one ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured
As you can see, a second call to "new" is being made before the previous one has called "drop."
Am I using the --jobs flag incorrectly? Am I misunderstanding what it actually does? How can I get my test cases to run sequentially? Thanks!