How to use concurrency in a single rails test?

270 views Asked by At

In order to detect concurrency issues, we tried to write a few concurrent tests of the form (pseudo code): parallel_for_10_times { database.insert(name: i) }, then check if all 10 elements are correct.

Since Rails' test fixtures never really commit (in order to isolate tests from each other), DB inserts get never seen by threads other than the one creating them. Therefore, in the second step (check if the elements were created all right), they are already gone and the test fails.

So we have decided to turn fixtures off on the test (using self.use_transactional_fixtures = false as the first line of the test class) and to make sure the test DB is cleaned by putting clean statements into an ensure after the test.

Since we have done this, we randomly get very strange failures; some other tests fail due to elements created in the concurrent test only. We are pretty sure that our clean statements are working fine.

Since we are certainly not alone with this problem: What is the state-of-the-art way to write tests with multiple threads for a Rails app? Thanks in advance.

0

There are 0 answers