Fix flaky CompactionWorkerTest by replacing fixed sleep with CountDownLatch - #18498
Merged
Merged
Conversation
…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.
|
JackieTien97
approved these changes
Aug 20, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




Problem
CompactionWorkerTest.testFailedToAllocateFileNumInCrossTaskandtestFailedToCheckValidInCrossTaskintermittently fail with:The tests use
thread.join(2s)to wait for the compaction task to be dropped. However, in these failure casesqueue.take()never returns: the task is dropped byCompactionTaskQueue.prepareTask -> dropCompactionTask, which callsresetCompactionCandidateStatusForAllSourceFiles()to change the source files fromCOMPACTION_CANDIDATEback toNORMAL. 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
CountDownLatchtriggered inside the worker thread at the exact state-transition point. A MockitodoAnswerhook callscountDown()right afterresetCompactionCandidateStatusForAllSourceFiles()completes: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
CompactionWorkerTestthat used the same pattern.