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?