How tests are run: threads



are all tests run in parallel or just the different files in the tests directory?

I have a library that creates databases and files in a ‘standard’ location.
Each tests cleans out this standard location after it is run and so the tests can’t run in parallel.
If I have tests/ and tests/ and inside the files the methods F11, F12 and F21, F2.
Are F11 and F12 also running in parallel?


ps: I am in the process to change the location used by the tests so that they are different for each file.
Would be nice if I did not have to change the location for each individual test.



By default, all tests are run in parallel. This can be altered with the
RUST_TEST_THREADS environment variable when running tests (set it to 1).



Practical tip here: Let’s say you have some kind of text fixture (no matter if it something some of the test-helper crates like galvanic provide or just a helper function returning a handle to the database). Put some kind of lock in there too, so the tests will wait for the other one to finish first:

lazy_static! { static TEST_LOCK: Mutex<()> = Mutex::new(()); }

struct Db { realDb: DbConnection, lock: MutexLock, }

impl Db {
  fn new() -> Self {
     let lock = TEST_LOCK.lock();
     let realDb = DbConnection::new();
     Self { lock, realDb }

impl Drop for Db {
  fn drop(&mut self) { self.realDb.clean_up(); }

impl Deref for Db { type TARGET = DbConnection; ... }

If you need it across multiple test binaries, you can use some file-based locking.

Other option is for each test to create a new DB in a random, temporary location, so they can actually run in parallel.



Thanks, that is how it is currently done but there are many tests that can run in parallel while there are two groups of tests that cannot run in parallel because they operate on the same directory.
I am currently changing this so that all tests use their own directory for storage but there are hundreds of tests so this is quite a burden and I am looking for ways that might give me greater control over which tests run in parallel and which are not. Also something like @Before and @After like in JUNIT would be great.




f.y.i. here is the code:



Thanks for the Mutex suggestion. That sounds like the thing for the standard location that I have to test also I’ll try to move most tests each to its own location for the files.