Skip to content

Commit 8dccb90

Browse files
committed
Get rid of serverUrl
1 parent 6b8cde2 commit 8dccb90

File tree

3 files changed

+49
-73
lines changed

3 files changed

+49
-73
lines changed

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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,7 @@ public void testAgentToAgentDelegation() throws Exception {
24852485
AtomicReference<Throwable> delegationErrorRef = new AtomicReference<>();
24862486

24872487
BiConsumer<ClientEvent, AgentCard> delegationConsumer =
2488-
createTaskCaptureConsumer(delegationResultRef, delegationLatch);
2488+
AgentToAgentClientFactory.createTaskCaptureConsumer(delegationResultRef, delegationLatch);
24892489

24902490
getClient().sendMessage(delegationMessage, List.of(delegationConsumer), error -> {
24912491
delegationErrorRef.set(error);
@@ -2547,7 +2547,7 @@ public void testAgentToAgentLocalHandling() throws Exception {
25472547
AtomicReference<Throwable> localErrorRef = new AtomicReference<>();
25482548

25492549
BiConsumer<ClientEvent, AgentCard> localConsumer =
2550-
createTaskCaptureConsumer(localResultRef, localLatch);
2550+
AgentToAgentClientFactory.createTaskCaptureConsumer(localResultRef, localLatch);
25512551

25522552
getClient().sendMessage(localMessage, List.of(localConsumer), error -> {
25532553
localErrorRef.set(error);
@@ -2573,31 +2573,6 @@ public void testAgentToAgentLocalHandling() throws Exception {
25732573
"Should be handled locally without delegation. Got: " + localText);
25742574
}
25752575

2576-
/**
2577-
* Creates a BiConsumer that captures the final task state.
2578-
* This helper method reduces code duplication in agent-to-agent tests.
2579-
*
2580-
* @param taskRef the AtomicReference to store the final task
2581-
* @param latch the CountDownLatch to signal completion
2582-
* @return a BiConsumer that captures completed tasks
2583-
*/
2584-
private BiConsumer<ClientEvent, AgentCard> createTaskCaptureConsumer(
2585-
AtomicReference<Task> taskRef, CountDownLatch latch) {
2586-
return (event, agentCard) -> {
2587-
Task task = null;
2588-
if (event instanceof TaskEvent taskEvent) {
2589-
task = taskEvent.getTask();
2590-
} else if (event instanceof TaskUpdateEvent taskUpdateEvent) {
2591-
task = taskUpdateEvent.getTask();
2592-
}
2593-
2594-
if (task != null && task.status().state().isFinal()) {
2595-
taskRef.set(task);
2596-
latch.countDown();
2597-
}
2598-
};
2599-
}
2600-
26012576
/**
26022577
* Extracts all text from a task's artifacts.
26032578
*

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

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,12 @@ private void handleAgentToAgentTest(RequestContext context, AgentEmitter agentEm
150150
return;
151151
}
152152

153-
String serverUrl = getServerUrl(transportProtocol);
154-
155153
// Extract user message
156154
String userInput = context.getUserInput("\n");
157155

158156
// Check for delegation pattern
159157
if (userInput.startsWith("delegate:")) {
160-
handleDelegation(userInput, transportProtocol, serverUrl, agentEmitter);
158+
handleDelegation(userInput, transportProtocol, agentEmitter);
161159
} else {
162160
handleLocally(userInput, agentEmitter);
163161
}
@@ -170,32 +168,21 @@ private void handleAgentToAgentTest(RequestContext context, AgentEmitter agentEm
170168
* Handles delegation by forwarding to another agent via client.
171169
*/
172170
private void handleDelegation(String userInput, TransportProtocol transportProtocol,
173-
String serverUrl, AgentEmitter agentEmitter) {
171+
AgentEmitter agentEmitter) {
174172
// Strip "delegate:" prefix
175173
String delegatedContent = userInput.substring("delegate:".length()).trim();
176174

177175
// Create client for same transport
178-
try (Client client = AgentToAgentClientFactory.createClient(agentCard, transportProtocol, serverUrl)) {
176+
try (Client client = AgentToAgentClientFactory.createClient(agentCard, transportProtocol)) {
179177
agentEmitter.startWork();
180178

181179
// Set up consumer to capture task result
182180
CountDownLatch latch = new CountDownLatch(1);
183181
AtomicReference<Task> resultRef = new AtomicReference<>();
184182
AtomicReference<Throwable> errorRef = new AtomicReference<>();
185183

186-
BiConsumer<ClientEvent, AgentCard> consumer = (event, agentCard) -> {
187-
Task task = null;
188-
if (event instanceof TaskEvent taskEvent) {
189-
task = taskEvent.getTask();
190-
} else if (event instanceof TaskUpdateEvent taskUpdateEvent) {
191-
task = taskUpdateEvent.getTask();
192-
}
193-
194-
if (task != null && task.status().state().isFinal()) {
195-
resultRef.set(task);
196-
latch.countDown();
197-
}
198-
};
184+
BiConsumer<ClientEvent, AgentCard> consumer =
185+
AgentToAgentClientFactory.createTaskCaptureConsumer(resultRef, latch);
199186

200187
// Delegate to another agent (new task on same server)
201188
// Add a marker so the receiving agent knows to complete the task
@@ -271,24 +258,4 @@ private String extractTextFromMessage(final Message message) {
271258
}
272259
return textBuilder.toString();
273260
}
274-
275-
/**
276-
* Gets the server URL for testing based on transport protocol.
277-
* Uses the same port property as AgentCardProducer.
278-
*
279-
* @param transportProtocol the transport protocol
280-
* @return server URL (e.g., "http://localhost:8081" or "localhost:9090")
281-
*/
282-
private static String getServerUrl(TransportProtocol transportProtocol) {
283-
// Use same property as AgentCardProducer
284-
String port = System.getProperty("test.agent.card.port", "8081");
285-
286-
// Construct URL using same logic as AgentCardProducer
287-
if (transportProtocol == TransportProtocol.GRPC) {
288-
return "localhost:" + port;
289-
} else {
290-
// JSONRPC and HTTP_JSON both use HTTP
291-
return "http://localhost:" + port;
292-
}
293-
}
294261
}

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package io.a2a.server.apps.common;
22

3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.atomic.AtomicReference;
5+
import java.util.function.BiConsumer;
6+
37
import io.a2a.client.Client;
48
import io.a2a.client.ClientBuilder;
9+
import io.a2a.client.ClientEvent;
10+
import io.a2a.client.TaskEvent;
11+
import io.a2a.client.TaskUpdateEvent;
512
import io.a2a.client.config.ClientConfig;
613
import io.a2a.client.transport.grpc.GrpcTransport;
714
import io.a2a.client.transport.grpc.GrpcTransportConfigBuilder;
@@ -11,6 +18,7 @@
1118
import io.a2a.client.transport.rest.RestTransportConfigBuilder;
1219
import io.a2a.spec.A2AClientException;
1320
import io.a2a.spec.AgentCard;
21+
import io.a2a.spec.Task;
1422
import io.a2a.spec.TransportProtocol;
1523
import io.grpc.ManagedChannel;
1624
import io.grpc.ManagedChannelBuilder;
@@ -21,16 +29,42 @@
2129
*/
2230
public class AgentToAgentClientFactory {
2331

32+
/**
33+
* Creates a BiConsumer that captures the final task state.
34+
* This utility method is used by both test classes and agent executors to avoid code duplication.
35+
*
36+
* @param taskRef the AtomicReference to store the final task
37+
* @param latch the CountDownLatch to signal completion
38+
* @return a BiConsumer that captures completed tasks
39+
*/
40+
public static BiConsumer<ClientEvent, AgentCard> createTaskCaptureConsumer(
41+
AtomicReference<Task> taskRef, CountDownLatch latch) {
42+
return (event, agentCard) -> {
43+
Task task = null;
44+
if (event instanceof TaskEvent taskEvent) {
45+
task = taskEvent.getTask();
46+
} else if (event instanceof TaskUpdateEvent taskUpdateEvent) {
47+
task = taskUpdateEvent.getTask();
48+
}
49+
50+
if (task != null && task.status().state().isFinal()) {
51+
taskRef.set(task);
52+
latch.countDown();
53+
}
54+
};
55+
}
56+
2457
/**
2558
* Creates a client for the specified transport protocol.
59+
* The agent card parameter already contains the correct local endpoint URLs
60+
* configured by the test's AgentCardProducer.
2661
*
27-
* @param agentCard the agentcard of the remote server
62+
* @param agentCard the agent card with correct local endpoints
2863
* @param transportProtocol the transport protocol to use
29-
* @param serverUrl the server URL (e.g., "http://localhost:8081" or "localhost:9090")
3064
* @return configured client
3165
* @throws A2AClientException if client creation fails
3266
*/
33-
public static Client createClient(AgentCard agentCard, TransportProtocol transportProtocol, String serverUrl)
67+
public static Client createClient(AgentCard agentCard, TransportProtocol transportProtocol)
3468
throws A2AClientException {
3569
ClientConfig clientConfig = ClientConfig.builder()
3670
.setStreaming(false)
@@ -46,7 +80,7 @@ public static Client createClient(AgentCard agentCard, TransportProtocol transpo
4680
default -> throw new IllegalArgumentException("Unsupported transport: " + transportProtocol);
4781
};
4882

49-
enhancer.enhance(clientBuilder, serverUrl);
83+
enhancer.enhance(clientBuilder);
5084
return clientBuilder.build();
5185
}
5286

@@ -55,12 +89,12 @@ public static Client createClient(AgentCard agentCard, TransportProtocol transpo
5589
* not on the classpath.
5690
*/
5791
interface ClientTransportEnhancer {
58-
void enhance(ClientBuilder clientBuilder, String serverUrl);
92+
void enhance(ClientBuilder clientBuilder);
5993
}
6094

6195
private static class GrpcClientEnhancer implements AgentToAgentClientFactory.ClientTransportEnhancer {
6296
@Override
63-
public void enhance(ClientBuilder clientBuilder, String serverUrl) {
97+
public void enhance(ClientBuilder clientBuilder) {
6498
clientBuilder.withTransport(GrpcTransport.class, new GrpcTransportConfigBuilder().channelFactory(target -> {
6599
ManagedChannel channel = ManagedChannelBuilder.forTarget(target).usePlaintext().build();
66100
return channel;
@@ -70,14 +104,14 @@ public void enhance(ClientBuilder clientBuilder, String serverUrl) {
70104

71105
private static class JsonRpcClientEnhancer implements AgentToAgentClientFactory.ClientTransportEnhancer {
72106
@Override
73-
public void enhance(ClientBuilder clientBuilder, String serverUrl) {
107+
public void enhance(ClientBuilder clientBuilder) {
74108
clientBuilder.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder());
75109
}
76110
}
77111

78112
private static class RestClientEnhancer implements AgentToAgentClientFactory.ClientTransportEnhancer {
79113
@Override
80-
public void enhance(ClientBuilder clientBuilder, String serverUrl) {
114+
public void enhance(ClientBuilder clientBuilder) {
81115
clientBuilder.withTransport(RestTransport.class, new RestTransportConfigBuilder());
82116
}
83117
}

0 commit comments

Comments
 (0)