From c34598a3cd094888c9155360c3e1c272161483ad Mon Sep 17 00:00:00 2001 From: jaymode Date: Mon, 28 Mar 2016 08:01:34 -0400 Subject: [PATCH] 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@e777fb5c28589d18c5768c4ff9415ed4694bcbca --- .../marvel/action/MonitoringBulkTests.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkTests.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkTests.java index d5644708f6a..5a00c6c41bd 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkTests.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/action/MonitoringBulkTests.java @@ -17,6 +17,7 @@ import org.elasticsearch.search.SearchHit; import java.util.List; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; 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. */ 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 exceptions = new CopyOnWriteArrayList<>(); AtomicInteger total = new AtomicInteger(0); @@ -82,6 +85,8 @@ public class MonitoringBulkTests extends MarvelIntegTestCase { @Override protected void doRun() throws Exception { + latch.countDown(); + latch.await(); for (int j = 0; j < nbRequests; j++) { MonitoringBulkRequestBuilder requestBuilder = monitoringClient().prepareMonitoringBulk(); @@ -102,6 +107,11 @@ public class MonitoringBulkTests extends MarvelIntegTestCase { threads[i].start(); } + // wait for all threads to be ready + latch.countDown(); + latch.await(); + + // wait for all threads to finish for (Thread thread : threads) { thread.join(); }