test: wait until threads are ready in MonitoringBulkTests#testConcurrentRequests

This commit synchronizes the start of the threads that are executing monitoring bulk requests concurrently
to ensure all threads are ready before starting. Without this some threads will execute requests while
other threads are still being constructed.

Original commit: elastic/x-pack-elasticsearch@e777fb5c28
This commit is contained in:
jaymode 2016-03-28 08:01:34 -04:00
parent 5ab407b2e0
commit c34598a3cd
1 changed files with 11 additions and 1 deletions

View File

@ -17,6 +17,7 @@ import org.elasticsearch.search.SearchHit;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -64,7 +65,9 @@ public class MonitoringBulkTests extends MarvelIntegTestCase {
* This test creates N threads that execute a random number of monitoring bulk requests. * This test creates N threads that execute a random number of monitoring bulk requests.
*/ */
public void testConcurrentRequests() throws Exception { public void testConcurrentRequests() throws Exception {
final Thread[] threads = new Thread[3 + randomInt(7)]; final int numberThreads = randomIntBetween(3, 10);
final Thread[] threads = new Thread[numberThreads];
final CountDownLatch latch = new CountDownLatch(numberThreads + 1);
final List<Throwable> exceptions = new CopyOnWriteArrayList<>(); final List<Throwable> exceptions = new CopyOnWriteArrayList<>();
AtomicInteger total = new AtomicInteger(0); AtomicInteger total = new AtomicInteger(0);
@ -82,6 +85,8 @@ public class MonitoringBulkTests extends MarvelIntegTestCase {
@Override @Override
protected void doRun() throws Exception { protected void doRun() throws Exception {
latch.countDown();
latch.await();
for (int j = 0; j < nbRequests; j++) { for (int j = 0; j < nbRequests; j++) {
MonitoringBulkRequestBuilder requestBuilder = monitoringClient().prepareMonitoringBulk(); MonitoringBulkRequestBuilder requestBuilder = monitoringClient().prepareMonitoringBulk();
@ -102,6 +107,11 @@ public class MonitoringBulkTests extends MarvelIntegTestCase {
threads[i].start(); threads[i].start();
} }
// wait for all threads to be ready
latch.countDown();
latch.await();
// wait for all threads to finish
for (Thread thread : threads) { for (Thread thread : threads) {
thread.join(); thread.join();
} }