Skip to content

Commit f2ba074

Browse files
committed
test(sse): lock readiness and transport failure contracts
1 parent 9a86100 commit f2ba074

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.github.easy4j.opencode.api;
2+
3+
import io.github.easy4j.opencode.OpenCodeHttpClientConfig;
4+
import io.github.easy4j.opencode.api.sse.SseSubscription;
5+
import okhttp3.mockwebserver.MockResponse;
6+
import okhttp3.mockwebserver.MockWebServer;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.lang.reflect.Method;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.ExecutionException;
12+
import java.util.concurrent.TimeUnit;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
class OpenCodeSseReadinessContractTest {
17+
18+
@Test
19+
void subscriptionMustExposeConnectedReadiness() throws Exception {
20+
try (MockWebServer server = new MockWebServer()) {
21+
server.start();
22+
server.enqueue(new MockResponse()
23+
.setHeader("Content-Type", "text/event-stream")
24+
.setBody("data: {\"type\":\"ready\",\"properties\":{}}\n\n"));
25+
26+
OpenCodeHttpClientConfig config = new OpenCodeHttpClientConfig();
27+
config.setBaseUrl(server.url("/").toString().replaceAll("/$", ""));
28+
29+
try (OpenCodeSseClient client = new OpenCodeSseClient(config)) {
30+
SseSubscription subscription = client.subscribeEvents(event -> { });
31+
32+
Method getReady = assertDoesNotThrow(
33+
() -> SseSubscription.class.getMethod("getReady"));
34+
@SuppressWarnings("unchecked")
35+
CompletableFuture<Void> ready =
36+
(CompletableFuture<Void>) getReady.invoke(subscription);
37+
38+
assertDoesNotThrow(() -> ready.get(2, TimeUnit.SECONDS));
39+
subscription.cancel();
40+
}
41+
}
42+
}
43+
44+
@Test
45+
void connectionFailureMustFailReadinessAndExposeFailure() throws Exception {
46+
try (MockWebServer server = new MockWebServer()) {
47+
server.start();
48+
server.enqueue(new MockResponse().setResponseCode(500).setBody("boom"));
49+
50+
OpenCodeHttpClientConfig config = new OpenCodeHttpClientConfig();
51+
config.setBaseUrl(server.url("/").toString().replaceAll("/$", ""));
52+
53+
try (OpenCodeSseClient client = new OpenCodeSseClient(config)) {
54+
SseSubscription subscription = client.subscribeEvents(event -> { });
55+
56+
Method getReady = assertDoesNotThrow(
57+
() -> SseSubscription.class.getMethod("getReady"));
58+
Method getFailure = assertDoesNotThrow(
59+
() -> SseSubscription.class.getMethod("getFailure"));
60+
61+
@SuppressWarnings("unchecked")
62+
CompletableFuture<Void> ready =
63+
(CompletableFuture<Void>) getReady.invoke(subscription);
64+
@SuppressWarnings("unchecked")
65+
CompletableFuture<Throwable> failure =
66+
(CompletableFuture<Throwable>) getFailure.invoke(subscription);
67+
68+
assertThrows(ExecutionException.class,
69+
() -> ready.get(2, TimeUnit.SECONDS));
70+
assertNotNull(failure.get(2, TimeUnit.SECONDS));
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)