Skip to content

Commit 387ea92

Browse files
authored
[fix][test] Stabilize FunctionAssignmentTailerTest.testErrorNotifier by synchronizing mock stubbing with CountDownLatch (#24875)
1 parent 7c6a4aa commit 387ea92

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pulsar-functions/worker/src/test/java/org/apache/pulsar/functions/worker/FunctionAssignmentTailerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.mockito.ArgumentMatchers.anyInt;
2424
import static org.mockito.ArgumentMatchers.anyString;
2525
import static org.mockito.ArgumentMatchers.eq;
26+
import static org.mockito.Mockito.doAnswer;
2627
import static org.mockito.Mockito.doReturn;
2728
import static org.mockito.Mockito.doThrow;
2829
import static org.mockito.Mockito.mock;
@@ -36,6 +37,7 @@
3637
import java.util.Map;
3738
import java.util.concurrent.ArrayBlockingQueue;
3839
import java.util.concurrent.CompletableFuture;
40+
import java.util.concurrent.CountDownLatch;
3941
import java.util.concurrent.TimeUnit;
4042
import lombok.extern.slf4j.Slf4j;
4143
import org.apache.pulsar.client.admin.PulsarAdmin;
@@ -148,6 +150,17 @@ public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
148150
// test new assignment add functions
149151
FunctionRuntimeManager functionRuntimeManager = mock(FunctionRuntimeManager.class);
150152

153+
// Use CountDownLatch to park the background thread after first processing and before re-stubbing
154+
CountDownLatch firstProcessed = new java.util.concurrent.CountDownLatch(1);
155+
CountDownLatch release = new java.util.concurrent.CountDownLatch(1);
156+
157+
// On first processing of message1, block and wait for re-stubbing
158+
doAnswer(inv -> {
159+
firstProcessed.countDown();
160+
release.await(5, TimeUnit.SECONDS);
161+
return null;
162+
}).when(functionRuntimeManager).processAssignmentMessage(eq(message1));
163+
151164
FunctionAssignmentTailer functionAssignmentTailer =
152165
spy(new FunctionAssignmentTailer(functionRuntimeManager, readerBuilder, workerConfig, errorNotifier));
153166

@@ -157,12 +170,17 @@ public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
157170
verify(errorNotifier, times(0)).triggerError(any());
158171

159172
messageList.add(message1);
173+
Assert.assertTrue(firstProcessed.await(5, TimeUnit.SECONDS),
174+
"First processing did not reach the blocking point");
160175

161176
verify(errorNotifier, times(0)).triggerError(any());
162177

163178
// trigger an error to be thrown
164179
doThrow(new RuntimeException("test")).when(functionRuntimeManager).processAssignmentMessage(any());
165180

181+
// Release the first processing
182+
release.countDown();
183+
166184
messageList.add(message2);
167185

168186
try {

0 commit comments

Comments
 (0)