Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stagger parallel test start in junit?

Conext: I have a junit test class, run by Maven's failsafe integration test plugin, with several tests that can be effectively infinitely parallelized. They're run against an already-deployed cloud platform as a gate to build promotion. Each of these tests has a quick memory spike at the beginning, but its subsequent usage is very low and it basically polls until an action is complete.

I need all of the tests to run in parallel (because they put load on a queue that causes scaling actions which make the job run faster), which is easy in failsafe, but I also need to stagger the memory spikes at the beginning so I don't run out of memory.

How should I accomplish staggering them? Current implementation does a Thread.sleep(random) but there's still a decent statistical probability that enough of them overlap to run out of memory. Should I put a Semaphore in the test class and wait on it in a @Before method? Is this something I can do more easily with a custom JUnit runner?

like image 250
Jon O Avatar asked Apr 18 '26 23:04

Jon O


2 Answers

Why not get the the data that makes the memory spike from one single synchronized method? This way each test will spike memory one at a time.

like image 85
tsolakp Avatar answered Apr 20 '26 13:04

tsolakp


The Semaphore approach seems like a good idea to me, but you probably want finer control than using @Before and @After - e.g., make the Semaphore static to the test class, acquire a permit immediately before the memory spike, and release a permit as soon as the memory becomes GC'able. You can then control how many concurrent memory spikes are allowed to happen by the number of permits available at construction of the Semaphore.

like image 39
CodeBlind Avatar answered Apr 20 '26 13:04

CodeBlind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!