shuwenwei opened a new pull request, #18498:
URL: https://github.com/apache/iotdb/pull/18498

   ## 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:
   
   ```java
   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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to