You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document using a Mutex to sequentalise tests #43155
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priority
Yes, problem can be worked around using PoisonError::into_inner. However, trick becomes verbose and convoluted, we really need something built-in to avoid it.
I agree in general (that's why I made https://docs.rs/antidote), but here, we're not accessing anything in the mutex so you can just save off the Result<MutexGuard> in the guard variable and not worry about it at all.
I think I'm failing to understand why someone couldn't just use --test-threads=1 when running tests if they want them to be run in serial. I'm having trouble understanding why this is something we need to document
Reacted by Eloff, Daniel Bornkessel, Karmo Rosental, cassc and Guilherme Janczak
@frewsxcv because the canonical, obvious way to run tests is cargo test. If someone who doesn't know the internals of the project wants to run the tests, that's what they'll do. Besides cargo test --test-threads=1 is much too long to write.
And, possibly, only some tests need mutual exclusion (e.g. because they use some single, problematic POSIX API, like it's in nix in case of fork/wait) and the others can safely run in parallel.
Reacted by Jon Gjengset, h0n9xu, Emerentius, mukund, Jan [Yann], Eloff, jlgerber, David Laehnemann and Reilly Wood
Hitting the same problem here.
Database functions for which I'm currently creating virtual tables and force a connection hand-over instead of the normal pool approach, so I can test against this.
Hit a wall now trying to make parallel statements, leading to a tricky situation of borrowck vs multiple statements when trying to work with parallel tests.
This also prevents the pool cache for statements in my case. blackbeam/rust-mysql-simple#154 (comment)
I'll have to re-parse my data multiple times, load everything into memory, disable parallel tests or drop test altogether.
Hitting this issue trying to write integration tests against an MQTT server that does not like clients using the same credentials to open multiple connections. Having that macro boilerplate at the stop of my tests is super gross and apparently still failure prone.
I don't understand. We had a working PR into rust-lang to fix this in code back in 1.20, and it was summarily dropped. What happened at that meeting?
As the issue says, since this is possible without a new language feature, the new feature was not accepted.
In the light of recent calls to stop adding feature over feature to rust it makes sense.
Edit: Welp it's already the "document this" issue.. I'm not sure but just documenting a possible solution for this should already help.
@marmistrz It sounds like one goal of this hypothetical feature is to flag certain tests as a non-parallelizable, while the rest are. In which case it'd likely need to be some sort of attribute on an individual testcase or module
This definitely doesn't belong to the language itself. How about adding a field in Cargo.toml which would have the same effect as --test-threads=1?
That is a pretty nice idea and would help a lot.
I'm not sure but is it possible to create a configuration in cargo to let run xyz sequential and everything else parallel?
I've always had 3-4 tests which you can't run parallel (DB, global system calls like pulse audio etc) but all others can. So being able to configure this would a) let a new contributor just go ahead with cargo test (less ways to fail) and b) improve speed as not everything has to run sequential.
Edit: Of course there's always the possibility to define a test.sh, mark all sequentials as #[ignore] and define test.sh such that you first run a cargo test, then a cargo test --test-threads=1 followed by all sequential things. I think the downsides are obvious.
I have been reading through this entire thread and I have distilled these solutions:
1. Use RUST_TEST_THREADS=1 2. Use cargo test -- --test-threads=1 3. Add new flag to Cargo.toml 4. Use the serial_rust package
I think 1. and 2. are not really a long term solution. Each crate would have to specify somewhere in their README that they require users to use one of the two solutions. This can be easily overlooked which might lead to confused users with failing tests.
If I understood option 3. correctly, the idea was to add a Cargo.toml flag that enabled the tests to run sequentially. Option 3. seems a lot better since users are not required to do some special action. However it still suffers from the fact that all tests have to be executed sequentially, which might not be necessary. It might perfectly be the case that a large project contains only a handful of tests that have to be executed sequentially. Crates with large test suites would have significantly larger test times.
Option 4. (@yaymukund Thanks for mentioning this!) seems to solve both problems by not requiring any actions from users, but allowing only a specific set of tests to run sequentially. The crate even allow for groups of test functions to be executed sequentially!: i.e. #[serial(groupA)]
I think we can close this one now if you guys agree!
Reacted by Jeff Hiner, Proc, Abin Simon and thosteg
@marmistrz Maybe it would be nice to create a new issue for this? The current issue name does not really represent what we want to achieve by fixing this issue. If you want I can create a new issue (with explanation) and link to this discussion? I think that it would be nice to add it to the tests chapter as well!
We're not likely to add this to the Rust book, as it doesn't fit in with its goals. I agree that this doesn't really fit in anywhere, and your assesment of the options. Let's close.
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priority
Activity
durka commentedon Jul 10, 2017
I guess the trick is this:
Where the preamble could certainly be put in a support crate.
kriomant commentedon Jul 15, 2017
This approach has a problem: when one test fails with panic, remaining ones fail with
PoisonError.durka commentedon Jul 15, 2017
Hmm, good point. I suppose an implementation like this can fix that:
Another reason to put this in a support crate (or the standard test harness) to avoid this and likely other traps.
sfackler commentedon Jul 15, 2017
You don't need to do anything with
catch_unwind- just don't unwrap the result.durka commentedon Jul 15, 2017
@sfackler Does a poisoned mutex still... mutex?
sfackler commentedon Jul 15, 2017
Yes - the guard object is still inside of the error - https://doc.rust-lang.org/std/sync/struct.PoisonError.html#method.into_inner
kriomant commentedon Jul 16, 2017
Yes, problem can be worked around using
PoisonError::into_inner. However, trick becomes verbose and convoluted, we really need something built-in to avoid it.sfackler commentedon Jul 16, 2017
I agree in general (that's why I made https://docs.rs/antidote), but here, we're not accessing anything in the mutex so you can just save off the
Result<MutexGuard>in theguardvariable and not worry about it at all.marmistrz commentedon Jul 31, 2017
I couldn't agree more that we need something built-in. I tried adding the Mutex to my own tests, and this did not seem to work anyway. https://github.com/marmistrz/randmockery/blob/mutex/tests/test.rs
Setting
RUST_TEST_THREADS=1does the trick. Maybe it would simply be enough to allow setting this inCargo.toml?sfackler commentedon Jul 31, 2017
@marmistrz
let _ = mutex.lock()immediately discards the guard and unlocks the mutex.let _guard = mutex.lock()will do the right thing.frewsxcv commentedon Oct 28, 2017
I think I'm failing to understand why someone couldn't just use
--test-threads=1when running tests if they want them to be run in serial. I'm having trouble understanding why this is something we need to documentmarmistrz commentedon Oct 28, 2017
@frewsxcv because the canonical, obvious way to run tests is
cargo test. If someone who doesn't know the internals of the project wants to run the tests, that's what they'll do. Besidescargo test --test-threads=1is much too long to write.And, possibly, only some tests need mutual exclusion (e.g. because they use some single, problematic POSIX API, like it's in nix in case of fork/wait) and the others can safely run in parallel.
5 remaining items
0xpr03 commentedon Sep 4, 2018
Hitting the same problem here.
Database functions for which I'm currently creating virtual tables and force a connection hand-over instead of the normal pool approach, so I can test against this.
Hit a wall now trying to make parallel statements, leading to a tricky situation of borrowck vs multiple statements when trying to work with parallel tests.
This also prevents the pool cache for statements in my case.
blackbeam/rust-mysql-simple#154 (comment)
I'll have to re-parse my data multiple times, load everything into memory, disable parallel tests or drop test altogether.
jeff-hiner commentedon Dec 11, 2018
Hitting this issue trying to write integration tests against an MQTT server that does not like clients using the same credentials to open multiple connections. Having that macro boilerplate at the stop of my tests is super gross and apparently still failure prone.
I don't understand. We had a working PR into rust-lang to fix this in code back in 1.20, and it was summarily dropped. What happened at that meeting?
steveklabnik commentedon Jan 8, 2019
As the issue says, since this is possible without a new language feature, the new feature was not accepted.
0xpr03 commentedon Jan 8, 2019
In the light of recent calls to stop adding feature over feature to rust it makes sense.
Edit: Welp it's already the "document this" issue..
I'm not sure but just documenting a possible solution for this should already help.marmistrz commentedon Jan 9, 2019
This definitely doesn't belong to the language itself. How about adding a field in Cargo.toml which would have the same effect as
--test-threads=1?frewsxcv commentedon Jan 10, 2019
@marmistrz It sounds like one goal of this hypothetical feature is to flag certain tests as a non-parallelizable, while the rest are. In which case it'd likely need to be some sort of attribute on an individual testcase or module
frewsxcv commentedon Jan 10, 2019
lol, also I just realized I linked to a comment thinking it was someone else, but it's actually you 🙈
0xpr03 commentedon Jan 10, 2019
That is a pretty nice idea and would help a lot.
I'm not sure but is it possible to create a configuration in cargo to let run xyz sequential and everything else parallel?
I've always had 3-4 tests which you can't run parallel (DB, global system calls like pulse audio etc) but all others can. So being able to configure this would a) let a new contributor just go ahead with
cargo test(less ways to fail) and b) improve speed as not everything has to run sequential.Edit: Of course there's always the possibility to define a test.sh, mark all sequentials as
#[ignore]and define test.sh such that you first run acargo test, then acargo test --test-threads=1followed by all sequential things. I think the downsides are obvious.yaymukund commentedon Mar 17, 2019
If anyone else stumbled into this thread searching for a solution, there's the
serial_rustcrate (described here).DevQps commentedon Apr 5, 2019
I have been reading through this entire thread and I have distilled these solutions:
1. Use
RUST_TEST_THREADS=12. Use
cargo test -- --test-threads=13. Add new flag to Cargo.toml
4. Use the
serial_rustpackageI think 1. and 2. are not really a long term solution. Each crate would have to specify somewhere in their README that they require users to use one of the two solutions. This can be easily overlooked which might lead to confused users with failing tests.
If I understood option 3. correctly, the idea was to add a Cargo.toml flag that enabled the tests to run sequentially. Option 3. seems a lot better since users are not required to do some special action. However it still suffers from the fact that all tests have to be executed sequentially, which might not be necessary. It might perfectly be the case that a large project contains only a handful of tests that have to be executed sequentially. Crates with large test suites would have significantly larger test times.
Option 4. (@yaymukund Thanks for mentioning this!) seems to solve both problems by not requiring any actions from users, but allowing only a specific set of tests to run sequentially. The crate even allow for groups of test functions to be executed sequentially!: i.e.
#[serial(groupA)]I think we can close this one now if you guys agree!
DevQps commentedon May 4, 2019
@steveklabnik I think we can close this issue now!
marmistrz commentedon May 4, 2019
I still think it deserves a comment in the Rust book, the chapter on tests. (at least mention that it can be achieved using serial_rust)
DevQps commentedon May 4, 2019
@marmistrz Maybe it would be nice to create a new issue for this? The current issue name does not really represent what we want to achieve by fixing this issue. If you want I can create a new issue (with explanation) and link to this discussion? I think that it would be nice to add it to the tests chapter as well!
steveklabnik commentedon May 16, 2019
We're not likely to add this to the Rust book, as it doesn't fit in with its goals. I agree that this doesn't really fit in anywhere, and your assesment of the options. Let's close.