Skip to content

Commit 327f44e

Browse files
committed
Concurrent tests wait for threads to be ready (#42083)
This change updates tests that use a CountDownLatch to synchronize the running of threads when testing concurrent operations so that we ensure the thread has been fully created and run by the scheduler. Previously, these tests used a latch with a value of 1 and the test thread counted down while the threads performing concurrent operations just waited. This change updates the value of the latch to be 1 + the number of threads. Each thread counts down and then waits. This means that each thread has been constructed and has started running. All threads will have a common start point now.
1 parent 367e027 commit 327f44e

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

server/src/test/java/org/elasticsearch/common/util/concurrent/CountDownTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ public void testConcurrent() throws InterruptedException {
3434
final AtomicInteger count = new AtomicInteger(0);
3535
final CountDown countDown = new CountDown(scaledRandomIntBetween(10, 1000));
3636
Thread[] threads = new Thread[between(3, 10)];
37-
final CountDownLatch latch = new CountDownLatch(1);
37+
final CountDownLatch latch = new CountDownLatch(1 + threads.length);
3838
for (int i = 0; i < threads.length; i++) {
3939
threads[i] = new Thread() {
4040

4141
@Override
4242
public void run() {
43+
latch.countDown();
4344
try {
4445
latch.await();
4546
} catch (InterruptedException e) {

server/src/test/java/org/elasticsearch/common/util/concurrent/KeyedLockTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public void testIfMapEmptyAfterLotsOfAcquireAndReleases() throws InterruptedExce
4545
for (int i = 0; i < names.length; i++) {
4646
names[i] = randomRealisticUnicodeOfLengthBetween(10, 20);
4747
}
48-
CountDownLatch startLatch = new CountDownLatch(1);
4948
int numThreads = randomIntBetween(3, 10);
49+
final CountDownLatch startLatch = new CountDownLatch(1 + numThreads);
5050
AcquireAndReleaseThread[] threads = new AcquireAndReleaseThread[numThreads];
5151
for (int i = 0; i < numThreads; i++) {
5252
threads[i] = new AcquireAndReleaseThread(startLatch, connectionLock, names, counter, safeCounter);
@@ -157,6 +157,7 @@ public AcquireAndReleaseThread(CountDownLatch startLatch, KeyedLock<String> conn
157157

158158
@Override
159159
public void run() {
160+
startLatch.countDown();
160161
try {
161162
startLatch.await();
162163
} catch (InterruptedException e) {

server/src/test/java/org/elasticsearch/common/util/concurrent/RunOnceTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public void testRunOnceConcurrently() throws InterruptedException {
4545
final RunOnce runOnce = new RunOnce(counter::incrementAndGet);
4646

4747
final Thread[] threads = new Thread[between(3, 10)];
48-
final CountDownLatch latch = new CountDownLatch(1);
48+
final CountDownLatch latch = new CountDownLatch(1 + threads.length);
4949
for (int i = 0; i < threads.length; i++) {
5050
threads[i] = new Thread(() -> {
51+
latch.countDown();
5152
try {
5253
latch.await();
5354
} catch (InterruptedException e) {

server/src/test/java/org/elasticsearch/node/ResponseCollectorServiceTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ public void testNodeStats() throws Exception {
7777
public void testConcurrentAddingAndRemoving() throws Exception {
7878
String[] nodes = new String[] {"a", "b", "c", "d"};
7979

80-
final CountDownLatch latch = new CountDownLatch(1);
80+
final CountDownLatch latch = new CountDownLatch(5);
8181

8282
Runnable f = () -> {
83+
latch.countDown();
8384
try {
8485
latch.await();
8586
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)