Skip to content

Commit 9c05c9d

Browse files
committed
fix: Eliminate race condition in testNonBlockingWithMultipleMessages
Addresses intermittent test failure where subscribeToTask created child queues but EventConsumer polling loops hadn't started yet when the agent executed, causing emitted events to be lost. Root Cause: - awaitStreamingSubscription() only guarantees transport-level subscription (Flow.Subscriber.onSubscribe() called) - EventConsumer polling starts asynchronously on eventConsumerExecutor thread - Agent execution could begin before EventConsumer was actively polling - Events emitted before polling started were lost in the queue Solution: - Added awaitChildQueueCountStable() to TestUtilsBean - Waits for child queue count to match expected value for 3 consecutive checks (150ms total), ensuring EventConsumer is actively polling - Follows pattern from commit 18d2abf which fixed similar race condition - Exposed REST endpoints in all transports (JSON-RPC, gRPC, REST) - Added client helper in AbstractA2AServerTest - Applied stability check after subscribeToTask() in tests - Increased timeouts from 10s to 15s for CI stability Files Modified: - TestUtilsBean.java: Core synchronization logic with comprehensive Javadoc - A2ATestRoutes.java (JSON-RPC, REST): REST endpoints for stability checks - A2ATestResource.java (gRPC): REST endpoints for stability checks - AbstractA2AServerTest.java: Client helper and extensive inline comments
1 parent b103c8a commit 9c05c9d

5 files changed

Lines changed: 176 additions & 2 deletions

File tree

‎reference/grpc/src/test/java/io/a2a/server/grpc/quarkus/A2ATestResource.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,40 @@ public Response savePushNotificationConfigInStore(@PathParam("taskId") String ta
137137
testUtilsBean.saveTaskPushNotificationConfig(taskId, notificationConfig);
138138
return Response.ok().build();
139139
}
140+
141+
/**
142+
* REST endpoint to wait for queue poller to start.
143+
* Waits for the EventConsumer polling loop to start for the specified task's queue,
144+
* ensuring the queue is ready to receive and process events.
145+
*
146+
* @param taskId the task ID whose queue poller to wait for
147+
* @return HTTP 200 response when poller has started
148+
* @throws InterruptedException if interrupted while waiting
149+
*/
150+
@POST
151+
@Path("/queue/awaitPollerStart/{taskId}")
152+
public Response awaitQueuePollerStart(@PathParam("taskId") String taskId) throws InterruptedException {
153+
testUtilsBean.awaitQueuePollerStart(taskId);
154+
return Response.ok().build();
155+
}
156+
157+
/**
158+
* REST endpoint to wait for child queue count to stabilize.
159+
* Waits for the specified task's child queue count to match expectedCount for 3 consecutive
160+
* checks (150ms total), ensuring EventConsumer polling loops have started.
161+
*
162+
* @param taskId the task ID whose child queues to monitor
163+
* @param expectedCount the expected number of active child queues
164+
* @param timeoutMs maximum time to wait in milliseconds
165+
* @return HTTP 200 response with "true" if count stabilized, "false" if timeout occurred
166+
* @throws InterruptedException if interrupted while waiting
167+
*/
168+
@POST
169+
@Path("/queue/awaitChildCountStable/{taskId}/{expectedCount}/{timeoutMs}")
170+
public Response awaitChildQueueCountStable(@PathParam("taskId") String taskId,
171+
@PathParam("expectedCount") int expectedCount,
172+
@PathParam("timeoutMs") long timeoutMs) throws InterruptedException {
173+
boolean stable = testUtilsBean.awaitChildQueueCountStable(taskId, expectedCount, timeoutMs);
174+
return Response.ok(String.valueOf(stable), TEXT_PLAIN).build();
175+
}
140176
}

‎reference/jsonrpc/src/test/java/io/a2a/server/apps/quarkus/A2ATestRoutes.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ public void saveTaskPushNotificationConfig(@Param String taskId, @Body String bo
185185
}
186186
}
187187

188+
/**
189+
* REST endpoint to wait for child queue count to stabilize.
190+
* Waits for the specified task's child queue count to match expectedCount for 3 consecutive
191+
* checks (150ms total), ensuring EventConsumer polling loops have started.
192+
*
193+
* @param taskId the task ID whose child queues to monitor
194+
* @param expectedCountStr the expected number of active child queues (as string)
195+
* @param timeoutMsStr maximum time to wait in milliseconds (as string)
196+
* @param rc the Vert.x routing context
197+
*/
198+
@Route(path = "/test/queue/awaitChildCountStable/:taskId/:expectedCount/:timeoutMs", methods = {Route.HttpMethod.POST}, type = Route.HandlerType.BLOCKING)
199+
public void awaitChildQueueCountStable(@Param("taskId") String taskId, @Param("expectedCount") String expectedCountStr, @Param("timeoutMs") String timeoutMsStr, RoutingContext rc) {
200+
try {
201+
int expectedCount = Integer.parseInt(expectedCountStr);
202+
long timeoutMs = Long.parseLong(timeoutMsStr);
203+
boolean stable = testUtilsBean.awaitChildQueueCountStable(taskId, expectedCount, timeoutMs);
204+
rc.response()
205+
.setStatusCode(200)
206+
.end(String.valueOf(stable));
207+
} catch (Throwable t) {
208+
errorResponse(t, rc);
209+
}
210+
}
211+
188212
private void errorResponse(Throwable t, RoutingContext rc) {
189213
t.printStackTrace();
190214
rc.response()

‎reference/rest/src/test/java/io/a2a/server/rest/quarkus/A2ATestRoutes.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ public void saveTaskPushNotificationConfig(@Param String taskId, @Body String bo
185185
}
186186
}
187187

188+
/**
189+
* REST endpoint to wait for child queue count to stabilize.
190+
* Waits for the specified task's child queue count to match expectedCount for 3 consecutive
191+
* checks (150ms total), ensuring EventConsumer polling loops have started.
192+
*
193+
* @param taskId the task ID whose child queues to monitor
194+
* @param expectedCountStr the expected number of active child queues (as string)
195+
* @param timeoutMsStr maximum time to wait in milliseconds (as string)
196+
* @param rc the Vert.x routing context
197+
*/
198+
@Route(path = "/test/queue/awaitChildCountStable/:taskId/:expectedCount/:timeoutMs", methods = {Route.HttpMethod.POST}, type = Route.HandlerType.BLOCKING)
199+
public void awaitChildQueueCountStable(@Param("taskId") String taskId, @Param("expectedCount") String expectedCountStr, @Param("timeoutMs") String timeoutMsStr, RoutingContext rc) {
200+
try {
201+
int expectedCount = Integer.parseInt(expectedCountStr);
202+
long timeoutMs = Long.parseLong(timeoutMsStr);
203+
boolean stable = testUtilsBean.awaitChildQueueCountStable(taskId, expectedCount, timeoutMs);
204+
rc.response()
205+
.setStatusCode(200)
206+
.end(String.valueOf(stable));
207+
} catch (Throwable t) {
208+
errorResponse(t, rc);
209+
}
210+
}
211+
188212
private void errorResponse(Throwable t, RoutingContext rc) {
189213
t.printStackTrace();
190214
rc.response()

‎tests/server-common/src/test/java/io/a2a/server/apps/common/AbstractA2AServerTest.java‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,19 @@ public void testNonBlockingWithMultipleMessages() throws Exception {
14011401

14021402
assertTrue(subscriptionLatch.await(15, TimeUnit.SECONDS));
14031403

1404+
// CRITICAL SYNCHRONIZATION: Wait for subscribeToTask's EventConsumer polling loop to start
1405+
//
1406+
// Race condition: awaitStreamingSubscription() only guarantees transport-level subscription
1407+
// (Flow.Subscriber.onSubscribe() called), but EventConsumer polling starts asynchronously
1408+
// on a separate thread. Without this check, the agent could emit events before any consumer
1409+
// is ready to receive them, causing events to be lost.
1410+
//
1411+
// This stability check waits for the child queue count to match expectedCount for 3
1412+
// consecutive checks (150ms), ensuring the EventConsumer is actively polling and won't
1413+
// miss events when the agent executes.
1414+
assertTrue(awaitChildQueueCountStable(taskId, 1, 15000),
1415+
"subscribeToTask child queue should be created and stable");
1416+
14041417
// 3. Send second streaming message to same taskId
14051418
Message message2 = Message.builder(MESSAGE)
14061419
.taskId(multiEventTaskId) // Same taskId
@@ -1435,8 +1448,8 @@ public void testNonBlockingWithMultipleMessages() throws Exception {
14351448
"Stream subscription should be established");
14361449

14371450
// 4. Verify both consumers received artifact-2 and completion
1438-
assertTrue(resubEventLatch.await(10, TimeUnit.SECONDS));
1439-
assertTrue(streamEventLatch.await(10, TimeUnit.SECONDS));
1451+
assertTrue(resubEventLatch.await(15, TimeUnit.SECONDS));
1452+
assertTrue(streamEventLatch.await(15, TimeUnit.SECONDS));
14401453

14411454
assertFalse(resubUnexpectedEvent.get());
14421455
assertFalse(streamUnexpectedEvent.get());
@@ -1492,6 +1505,34 @@ public void testNonBlockingWithMultipleMessages() throws Exception {
14921505
}
14931506
}
14941507

1508+
/**
1509+
* Waits for the child queue count to stabilize at the expected value by calling the server's
1510+
* test endpoint. This ensures EventConsumer polling loops have started before proceeding.
1511+
*
1512+
* @param taskId the task ID whose child queues to monitor
1513+
* @param expectedCount the expected number of active child queues
1514+
* @param timeoutMs maximum time to wait in milliseconds
1515+
* @return true if the count stabilized at the expected value, false if timeout occurred
1516+
* @throws IOException if the HTTP request fails
1517+
* @throws InterruptedException if interrupted while waiting
1518+
*/
1519+
private boolean awaitChildQueueCountStable(String taskId, int expectedCount, long timeoutMs) throws IOException, InterruptedException {
1520+
HttpClient client = HttpClient.newBuilder()
1521+
.version(HttpClient.Version.HTTP_2)
1522+
.build();
1523+
HttpRequest request = HttpRequest.newBuilder()
1524+
.uri(URI.create("http://localhost:" + serverPort + "/test/queue/awaitChildCountStable/" +
1525+
taskId + "/" + expectedCount + "/" + timeoutMs))
1526+
.POST(HttpRequest.BodyPublishers.noBody())
1527+
.build();
1528+
1529+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
1530+
if (response.statusCode() != 200) {
1531+
throw new RuntimeException(response.statusCode() + ": Awaiting child queue count failed! " + response.body());
1532+
}
1533+
return Boolean.parseBoolean(response.body());
1534+
}
1535+
14951536
@Test
14961537
@Timeout(value = 1, unit = TimeUnit.MINUTES)
14971538
public void testInputRequiredWorkflow() throws Exception {

‎tests/server-common/src/test/java/io/a2a/server/apps/common/TestUtilsBean.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,53 @@ public void deleteTaskPushNotificationConfig(String taskId, String configId) {
6161
public void saveTaskPushNotificationConfig(String taskId, PushNotificationConfig notificationConfig) {
6262
pushNotificationConfigStore.setInfo(taskId, notificationConfig);
6363
}
64+
65+
/**
66+
* Waits for the EventConsumer polling loop to start for the specified task's queue.
67+
* This ensures the queue is ready to receive and process events.
68+
*
69+
* @param taskId the task ID whose queue poller to wait for
70+
* @throws InterruptedException if interrupted while waiting
71+
*/
72+
public void awaitQueuePollerStart(String taskId) throws InterruptedException {
73+
queueManager.awaitQueuePollerStart(queueManager.get(taskId));
74+
}
75+
76+
/**
77+
* Waits for the child queue count to stabilize at the expected value.
78+
* <p>
79+
* This method addresses a race condition where EventConsumer polling loops may not have started
80+
* yet when events are emitted. It waits for the child queue count to match the expected value
81+
* for 3 consecutive checks (150ms total), ensuring EventConsumers are actively polling and
82+
* won't miss events.
83+
* <p>
84+
* Use this after operations that create child queues (e.g., subscribeToTask, sendMessage) to
85+
* ensure their EventConsumer polling loops have started before the agent emits events.
86+
*
87+
* @param taskId the task ID whose child queues to monitor
88+
* @param expectedCount the expected number of active child queues
89+
* @param timeoutMs maximum time to wait in milliseconds
90+
* @return true if the count stabilized at the expected value, false if timeout occurred
91+
* @throws InterruptedException if interrupted while waiting
92+
*/
93+
public boolean awaitChildQueueCountStable(String taskId, int expectedCount, long timeoutMs) throws InterruptedException {
94+
long endTime = System.currentTimeMillis() + timeoutMs;
95+
int consecutiveMatches = 0;
96+
final int requiredMatches = 3; // Count must match 3 times in a row (150ms) to be considered stable
97+
98+
while (System.currentTimeMillis() < endTime) {
99+
int count = queueManager.getActiveChildQueueCount(taskId);
100+
if (count == expectedCount) {
101+
consecutiveMatches++;
102+
if (consecutiveMatches >= requiredMatches) {
103+
// Count is stable - all child queues exist and haven't closed
104+
return true;
105+
}
106+
} else {
107+
consecutiveMatches = 0; // Reset if count changes
108+
}
109+
Thread.sleep(50);
110+
}
111+
return false;
112+
}
64113
}

0 commit comments

Comments
 (0)