How tests are run: threads

#1

Hi,

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/f1.rs and tests/f2.rs and inside the files the methods F11, F12 and F21, F2.
Are F11 and F12 also running in parallel?

Thanks,
Axel

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.

0 Likes

#2

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).

https://doc.rust-lang.org/1.1.0/src/test/lib.rs.html#336

0 Likes

#3

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.

0 Likes

#4

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.
Thanks,
Axel

0 Likes

#5
0 Likes

#6

f.y.i. here is the code: https://github.com/hyperledger/indy-sdk/tree/master/libindy/tests

0 Likes

#7

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.

0 Likes