mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-09 14:34:43 +00:00
[TEST] Add CountDownLatches to ensure order of operation.
This change adds countdownlatches to the `FairKeyedLock` tests on `WatchLockService`. Original commit: elastic/x-pack-elasticsearch@2bf36e10d2
This commit is contained in:
parent
6c54251e61
commit
ca38fd6a89
@ -11,6 +11,7 @@ import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
@ -66,6 +67,7 @@ public class WatchLockServiceTests extends ElasticsearchTestCase {
|
||||
class FairRunner implements Runnable {
|
||||
|
||||
final int expectedValue;
|
||||
final CountDownLatch startLatch = new CountDownLatch(1);
|
||||
|
||||
FairRunner(int expectedValue) {
|
||||
this.expectedValue = expectedValue;
|
||||
@ -73,6 +75,7 @@ public class WatchLockServiceTests extends ElasticsearchTestCase {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
startLatch.countDown();
|
||||
WatchLockService.Lock lock = lockService.acquire("_name");
|
||||
try {
|
||||
int actualValue = value.getAndIncrement();
|
||||
@ -85,13 +88,17 @@ public class WatchLockServiceTests extends ElasticsearchTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
List<FairRunner> runners = new ArrayList<>();
|
||||
|
||||
for(int i = 0; i < 100; ++i) {
|
||||
FairRunner f = new FairRunner(i);
|
||||
runners.add(f);
|
||||
threads.add(new Thread(f));
|
||||
}
|
||||
|
||||
for(Thread t : threads) {
|
||||
t.start();
|
||||
for(int i = 0; i < threads.size(); ++i) {
|
||||
threads.get(i).start();
|
||||
runners.get(i).startLatch.await();
|
||||
Thread.sleep(10);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user