Skip to content

Commit 014a326

Browse files
committed
test(transport): cover SSE parse and consumer isolation
1 parent 13327da commit 014a326

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎src/test/java/io/github/easy4j/hermes/api/sse/SseLifecycleContractTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import java.util.Collections;
1616
import java.util.concurrent.CountDownLatch;
1717
import java.util.concurrent.TimeUnit;
18+
import java.util.concurrent.atomic.AtomicInteger;
1819
import java.util.concurrent.atomic.AtomicReference;
1920

21+
import okhttp3.extension.logging.HttpLogLevel;
22+
2023
import static org.junit.jupiter.api.Assertions.assertEquals;
2124
import static org.junit.jupiter.api.Assertions.assertNotNull;
2225
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -54,6 +57,44 @@ void standardChatChunkDoesNotRequireNestedDataField() throws Exception {
5457
}
5558
}
5659

60+
@Test
61+
void malformedJsonAndConsumerFailureRemainIsolated() throws Exception {
62+
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(chain -> {
63+
String body = "data: invalid\\n\\n"
64+
+ "data: {\\\"delta\\\":\\\"hello\\\"}\\n\\n"
65+
+ "data: [DONE]\\n\\n";
66+
return new Response.Builder()
67+
.request(chain.request())
68+
.protocol(Protocol.HTTP_1_1)
69+
.code(200)
70+
.message("OK")
71+
.body(ResponseBody.create(body, MediaType.get("text/event-stream")))
72+
.build();
73+
}).build();
74+
75+
HermesHttpClientConfig config = new HermesHttpClientConfig();
76+
config.markUnsafeBaseUrlOverriddenForTest(true);
77+
config.getDebug().setEnabled(true);
78+
config.getDebug().setLevel(HttpLogLevel.BODY);
79+
config.getDebug().setMaxContentLength(4);
80+
81+
ChatRequest request = new ChatRequest();
82+
request.setMessages(Collections.singletonList(new ChatRequest.Message("user", "hello")));
83+
AtomicInteger consumerCalls = new AtomicInteger();
84+
85+
try (HermesSseClient sse = new HermesSseClient(config, null, client)) {
86+
CountDownLatch complete = new CountDownLatch(1);
87+
sse.subscribeChat(request, ignored -> {
88+
consumerCalls.incrementAndGet();
89+
throw new IllegalStateException("consumer failure");
90+
}, complete::countDown, ignored -> { });
91+
92+
assertTrue(complete.await(3, TimeUnit.SECONDS));
93+
assertEquals(1, consumerCalls.get(),
94+
"malformed JSON must not be delivered to the business consumer");
95+
}
96+
}
97+
5798
@Test
5899
void sessionDisconnectDoesNotReplayOriginalPost() throws Exception {
59100
try (MockWebServer server = new MockWebServer()) {

0 commit comments

Comments
 (0)