Skip to content

Commit 46664e9

Browse files
committed
Fix race condition in WanCopyRegionFunctionServiceTest
The test severalExecuteWithDifferentRegionOrSenderAreAllowed was failing intermittently with 'expected: 5 but was: 3' due to a race condition. The test creates 5 CompletableFuture tasks using supplyAsync(), but there was no guarantee that all tasks would start executing before the assertion checked the execution count. Solution: - Added a startLatch CountDownLatch to synchronize task startup - Each callable now counts down the startLatch when it begins execution - The test waits for all tasks to start before asserting on the count - Added throws InterruptedException to the test method signature This ensures all 5 tasks have called service.execute() and been added to the executions map before we verify the count, eliminating the race condition.
1 parent 9f776b9 commit 46664e9

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

geode-wan/src/test/java/org/apache/geode/cache/wan/internal/WanCopyRegionFunctionServiceTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ public void cancelAllExecutionsWithRunningExecutionsReturnsCanceledExecutions()
154154
}
155155

156156
@Test
157-
public void severalExecuteWithDifferentRegionOrSenderAreAllowed() {
157+
public void severalExecuteWithDifferentRegionOrSenderAreAllowed() throws InterruptedException {
158158
int executions = 5;
159159
CountDownLatch latch = new CountDownLatch(executions);
160+
CountDownLatch startLatch = new CountDownLatch(executions);
160161
for (int i = 0; i < executions; i++) {
161162
Callable<CliFunctionResult> firstExecution = () -> {
163+
startLatch.countDown();
162164
latch.await(GeodeAwaitility.getTimeout().getSeconds(), TimeUnit.SECONDS);
163165
return null;
164166
};
@@ -174,6 +176,9 @@ public void severalExecuteWithDifferentRegionOrSenderAreAllowed() {
174176
});
175177
}
176178

179+
// Wait for all tasks to start execution
180+
startLatch.await(GeodeAwaitility.getTimeout().getSeconds(), TimeUnit.SECONDS);
181+
177182
// Wait for the functions to start execution
178183
await().untilAsserted(
179184
() -> assertThat(service.getNumberOfCurrentExecutions()).isEqualTo(executions));

0 commit comments

Comments
 (0)