Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CDL to block threads to avoid flaky tests. #14116

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ protected void doMerge(MergeSource mergeSource, MergePolicy.OneMerge merge)
dir.close();
}

@SuppressForbidden(reason = "Thread sleep")
public void testIntraMergeThreadPoolIsLimitedByMaxThreads() throws IOException {
ConcurrentMergeScheduler mergeScheduler = new ConcurrentMergeScheduler();
MergeScheduler.MergeSource mergeSource =
Expand Down Expand Up @@ -475,11 +474,12 @@ public void merge(MergePolicy.OneMerge merge) throws IOException {
Executor executor = mergeScheduler.intraMergeExecutor;
AtomicInteger threadsExecutedOnPool = new AtomicInteger();
AtomicInteger threadsExecutedOnSelf = new AtomicInteger();
for (int i = 0; i < 4; i++) {
CountDownLatch latch = new CountDownLatch(1);
final int totalThreads = 4;
for (int i = 0; i < totalThreads; i++) {
mergeScheduler.mergeThreads.add(
mergeScheduler.new MergeThread(mergeSource, merge) {
@Override
@SuppressForbidden(reason = "Thread sleep")
public void run() {
executor.execute(
() -> {
Expand All @@ -489,7 +489,7 @@ public void run() {
threadsExecutedOnPool.incrementAndGet();
}
try {
Thread.sleep(100);
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand All @@ -500,6 +500,10 @@ public void run() {
for (ConcurrentMergeScheduler.MergeThread thread : mergeScheduler.mergeThreads) {
thread.start();
}
while (threadsExecutedOnSelf.get() + threadsExecutedOnPool.get() < totalThreads) {
Thread.yield();
}
latch.countDown();
mergeScheduler.sync();
assertEquals(3, threadsExecutedOnSelf.get());
assertEquals(1, threadsExecutedOnPool.get());
Expand Down
Loading