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.
This commit is contained in:
Jay Modi 2019-05-14 16:29:18 -04:00 committed by jaymode
parent 367e027962
commit 327f44e051
No known key found for this signature in database
GPG Key ID: D859847567B3493D
4 changed files with 8 additions and 4 deletions

View File

@ -34,12 +34,13 @@ public class CountDownTests extends ESTestCase {
final AtomicInteger count = new AtomicInteger(0); final AtomicInteger count = new AtomicInteger(0);
final CountDown countDown = new CountDown(scaledRandomIntBetween(10, 1000)); final CountDown countDown = new CountDown(scaledRandomIntBetween(10, 1000));
Thread[] threads = new Thread[between(3, 10)]; Thread[] threads = new Thread[between(3, 10)];
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1 + threads.length);
for (int i = 0; i < threads.length; i++) { for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread() { threads[i] = new Thread() {
@Override @Override
public void run() { public void run() {
latch.countDown();
try { try {
latch.await(); latch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -45,8 +45,8 @@ public class KeyedLockTests extends ESTestCase {
for (int i = 0; i < names.length; i++) { for (int i = 0; i < names.length; i++) {
names[i] = randomRealisticUnicodeOfLengthBetween(10, 20); names[i] = randomRealisticUnicodeOfLengthBetween(10, 20);
} }
CountDownLatch startLatch = new CountDownLatch(1);
int numThreads = randomIntBetween(3, 10); int numThreads = randomIntBetween(3, 10);
final CountDownLatch startLatch = new CountDownLatch(1 + numThreads);
AcquireAndReleaseThread[] threads = new AcquireAndReleaseThread[numThreads]; AcquireAndReleaseThread[] threads = new AcquireAndReleaseThread[numThreads];
for (int i = 0; i < numThreads; i++) { for (int i = 0; i < numThreads; i++) {
threads[i] = new AcquireAndReleaseThread(startLatch, connectionLock, names, counter, safeCounter); threads[i] = new AcquireAndReleaseThread(startLatch, connectionLock, names, counter, safeCounter);
@ -157,6 +157,7 @@ public class KeyedLockTests extends ESTestCase {
@Override @Override
public void run() { public void run() {
startLatch.countDown();
try { try {
startLatch.await(); startLatch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -45,9 +45,10 @@ public class RunOnceTests extends ESTestCase {
final RunOnce runOnce = new RunOnce(counter::incrementAndGet); final RunOnce runOnce = new RunOnce(counter::incrementAndGet);
final Thread[] threads = new Thread[between(3, 10)]; final Thread[] threads = new Thread[between(3, 10)];
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1 + threads.length);
for (int i = 0; i < threads.length; i++) { for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(() -> { threads[i] = new Thread(() -> {
latch.countDown();
try { try {
latch.await(); latch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -77,9 +77,10 @@ public class ResponseCollectorServiceTests extends ESTestCase {
public void testConcurrentAddingAndRemoving() throws Exception { public void testConcurrentAddingAndRemoving() throws Exception {
String[] nodes = new String[] {"a", "b", "c", "d"}; String[] nodes = new String[] {"a", "b", "c", "d"};
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(5);
Runnable f = () -> { Runnable f = () -> {
latch.countDown();
try { try {
latch.await(); latch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {