Skip to content

Fix flaky CompactionWorkerTest by replacing fixed sleep with CountDownLatch - #18498

Merged
JackieTien97 merged 1 commit into
masterfrom
fix-ut-compaction-candidate-20260820
Aug 20, 2026
Merged

Fix flaky CompactionWorkerTest by replacing fixed sleep with CountDownLatch#18498
JackieTien97 merged 1 commit into
masterfrom
fix-ut-compaction-candidate-20260820

Conversation

@shuwenwei

Copy link
Copy Markdown
Member

Problem

CompactionWorkerTest.testFailedToAllocateFileNumInCrossTask and testFailedToCheckValidInCrossTask intermittently fail with:

expected:<NORMAL> but was:<COMPACTION_CANDIDATE>

The tests use thread.join(2s) to wait for the compaction task to be dropped. However, in these failure cases queue.take() never returns: the task is dropped by CompactionTaskQueue.prepareTask -> dropCompactionTask, which calls resetCompactionCandidateStatusForAllSourceFiles() to change the source files from COMPACTION_CANDIDATE back to NORMAL. The worker thread then loops back and waits on the empty queue.

The fixed 2-second join only waits for elapsed time, not for the actual state transition. Under load or scheduling delay, the main thread can assert before the worker thread performs the status reset, producing the flaky failure.

Fix

Replace the timing-based wait with a CountDownLatch triggered inside the worker thread at the exact state-transition point. A Mockito doAnswer hook calls countDown() right after resetCompactionCandidateStatusForAllSourceFiles() completes:

CountDownLatch statusResetLatch = new CountDownLatch(1);
Mockito.doAnswer(invocation -> {
  invocation.callRealMethod();
  statusResetLatch.countDown();
  return null;
}).when(taskMock).resetCompactionCandidateStatusForAllSourceFiles();

thread.start();
Assert.assertTrue(statusResetLatch.await(5, TimeUnit.SECONDS));

This waits for the exact condition (source files back to NORMAL) with a 5-second timeout, making the tests deterministic instead of relying on arbitrary sleep.

Applied to all four task-drop tests in CompactionWorkerTest that used the same pattern.

…ch instead of fixed 2s sleep to wait for task drop status reset

The two failing UTs (testFailedToAllocateFileNumInCrossTask and testFailedToCheckValidInCrossTask) were flaky because they used thread.join(2s) to wait for the task to be dropped. However, in these cases queue.take() never returns - the task is dropped by dropCompactionTask which calls resetCompactionCandidateStatusForAllSourceFiles(). The 2s sleep was not reliable under load.

Change to CountDownLatch attached to the resetCompactionCandidateStatusForAllSourceFiles() hook, with 5s timeout. This makes the test wait for the exact condition (status back to NORMAL) instead of arbitrary time.
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@JackieTien97
JackieTien97 merged commit 5221cf4 into master Aug 20, 2026
41 of 43 checks passed
@JackieTien97
JackieTien97 deleted the fix-ut-compaction-candidate-20260820 branch August 20, 2026 03:08
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 43.95%. Comparing base (ada62cb) to head (03920be).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #18498      +/-   ##
============================================
- Coverage     43.95%   43.95%   -0.01%     
  Complexity      374      374              
============================================
  Files          5402     5402              
  Lines        388198   388198              
  Branches      50694    50694              
============================================
- Hits         170633   170623      -10     
- Misses       217565   217575      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants