Skip to content

Commit 5f4b30e

Browse files
SashaMITkabir
authored andcommitted
test(rest): cover SSE completion and parsing error outcomes
REST streams must deliver one terminal outcome: `null` for normal completion, or the original error when parsing fails. The production fix from this PR is already on `main` through #1236. After rebasing, this PR retains the contributor's listener tests and strengthens the malformed JSON and missing-payload cases to verify that a later `onComplete()` neither delivers a second callback nor replaces the error. The duplicate transport completion test has been removed because `main` already covers that behavior. Validation: - `mvn -B -pl client/transport/rest -am test` - Temporarily restoring the old direct parsing-error callbacks makes both strengthened tests fail with two terminal callbacks; restoring `signalTerminal` makes them pass. This fixes #1194
1 parent f538765 commit 5f4b30e

1 file changed

Lines changed: 65 additions & 4 deletions

File tree

‎client/transport/rest/src/test/java/org/a2aproject/sdk/client/transport/rest/sse/SSEEventListenerTest.java‎

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertSame;
57
import static org.junit.jupiter.api.Assertions.assertTrue;
68

79
import java.util.concurrent.Future;
810
import java.util.concurrent.TimeUnit;
911
import java.util.concurrent.atomic.AtomicBoolean;
12+
import java.util.concurrent.atomic.AtomicInteger;
1013
import java.util.concurrent.atomic.AtomicReference;
1114

1215
import org.a2aproject.sdk.client.http.ServerSentEvent;
@@ -244,12 +247,16 @@ public void testOnMessageWithCompletedTaskCancels() {
244247
}
245248

246249
@Test
247-
public void testOnMessageWithInvalidJsonCallsErrorHandler() {
250+
public void testOnMessageWithInvalidJsonThenOnCompleteKeepsTheError() {
248251
// Set up error handler
252+
AtomicInteger calls = new AtomicInteger();
249253
AtomicReference<Throwable> receivedError = new AtomicReference<>();
250254
SSEEventListener listener = new SSEEventListener(
251255
event -> {},
252-
error -> receivedError.set(error)
256+
error -> {
257+
calls.incrementAndGet();
258+
receivedError.set(error);
259+
}
253260
);
254261

255262
// Invalid JSON message
@@ -260,15 +267,25 @@ public void testOnMessageWithInvalidJsonCallsErrorHandler() {
260267

261268
// Verify error handler was called
262269
assertNotNull(receivedError.get());
270+
Throwable firstError = receivedError.get();
271+
272+
listener.onComplete();
273+
274+
assertEquals(1, calls.get());
275+
assertSame(firstError, receivedError.get());
263276
}
264277

265278
@Test
266-
public void testOnMessageWithInvalidPayloadCaseCallsErrorHandler() {
279+
public void testOnMessageWithInvalidPayloadCaseThenOnCompleteKeepsTheError() {
267280
// Set up error handler
281+
AtomicInteger calls = new AtomicInteger();
268282
AtomicReference<Throwable> receivedError = new AtomicReference<>();
269283
SSEEventListener listener = new SSEEventListener(
270284
event -> {},
271-
error -> receivedError.set(error)
285+
error -> {
286+
calls.incrementAndGet();
287+
receivedError.set(error);
288+
}
272289
);
273290

274291
// JSON with no recognized payload type (empty object)
@@ -281,6 +298,50 @@ public void testOnMessageWithInvalidPayloadCaseCallsErrorHandler() {
281298
assertNotNull(receivedError.get());
282299
assertTrue(receivedError.get() instanceof IllegalStateException);
283300
assertTrue(receivedError.get().getMessage().contains("Invalid stream response"));
301+
Throwable firstError = receivedError.get();
302+
303+
listener.onComplete();
304+
305+
assertEquals(1, calls.get());
306+
assertSame(firstError, receivedError.get());
307+
}
308+
309+
@Test
310+
public void testOnCompleteSignalsNormalCompletion() {
311+
AtomicReference<Throwable> received = new AtomicReference<>(new RuntimeException("unset"));
312+
AtomicBoolean called = new AtomicBoolean(false);
313+
SSEEventListener listener = new SSEEventListener(
314+
event -> {},
315+
error -> {
316+
called.set(true);
317+
received.set(error);
318+
}
319+
);
320+
321+
listener.onComplete();
322+
323+
assertTrue(called.get());
324+
assertNull(received.get());
325+
}
326+
327+
@Test
328+
public void testOnErrorThenOnCompleteKeepsTheError() {
329+
AtomicInteger calls = new AtomicInteger();
330+
AtomicReference<Throwable> received = new AtomicReference<>();
331+
IllegalStateException boom = new IllegalStateException("first");
332+
SSEEventListener listener = new SSEEventListener(
333+
event -> {},
334+
error -> {
335+
calls.incrementAndGet();
336+
received.set(error);
337+
}
338+
);
339+
340+
listener.onError(boom, null);
341+
listener.onComplete();
342+
343+
assertEquals(1, calls.get());
344+
assertEquals(boom, received.get());
284345
}
285346

286347
@Test

0 commit comments

Comments
 (0)