|
| 1 | +package com.github.kuangcp.http.sse; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import okhttp3.OkHttpClient; |
| 6 | +import okhttp3.Request; |
| 7 | +import okhttp3.RequestBody; |
| 8 | +import okhttp3.Response; |
| 9 | +import okhttp3.mockwebserver.MockResponse; |
| 10 | +import okhttp3.mockwebserver.MockWebServer; |
| 11 | +import okhttp3.sse.EventSource; |
| 12 | +import okhttp3.sse.EventSourceListener; |
| 13 | +import okhttp3.sse.EventSources; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.SocketTimeoutException; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | +import static org.junit.Assert.fail; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Kuangcp |
| 29 | + * 2025-02-07 10:21 |
| 30 | + */ |
| 31 | +@Slf4j |
| 32 | +public class ReadTest { |
| 33 | + private MockWebServer server; |
| 34 | + private OkHttpClient client; |
| 35 | + |
| 36 | + ObjectMapper mapper = new ObjectMapper(); |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() { |
| 40 | + server = new MockWebServer(); |
| 41 | + try { |
| 42 | + server.start(); |
| 43 | + } catch (IOException e) { |
| 44 | + throw new RuntimeException(e); |
| 45 | + } |
| 46 | + |
| 47 | + // 设置 OkHttpClient 超时参数 |
| 48 | + client = new OkHttpClient.Builder() |
| 49 | + .connectTimeout(5, TimeUnit.SECONDS) // 连接超时5秒 |
| 50 | + .writeTimeout(5, TimeUnit.SECONDS) // 写入超时5秒 |
| 51 | + .readTimeout(5, TimeUnit.SECONDS) // 读取超时5秒 |
| 52 | + .build(); |
| 53 | + } |
| 54 | + |
| 55 | + @After |
| 56 | + public void tearDown() throws IOException { |
| 57 | + server.shutdown(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testConnectTimeout() throws IOException { |
| 62 | + // 模拟服务器延迟响应 |
| 63 | + server.enqueue(new MockResponse().setBodyDelay(6, TimeUnit.SECONDS)); |
| 64 | + |
| 65 | + Request request = new Request.Builder() |
| 66 | + .url(server.url("/")) |
| 67 | + .build(); |
| 68 | + |
| 69 | + try { |
| 70 | + client.newCall(request).execute(); |
| 71 | + fail("Expected a SocketTimeoutException to be thrown for connect timeout"); |
| 72 | + } catch (SocketTimeoutException e) { |
| 73 | + // 连接超时,测试通过 |
| 74 | + assertTrue(true); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testWriteTimeout() throws IOException { |
| 80 | + // 这个测试依赖于服务器的配置,确保服务器不会立即发送响应头 |
| 81 | + server.enqueue(new MockResponse().setBodyDelay(6, TimeUnit.SECONDS)); |
| 82 | + |
| 83 | + Request request = new Request.Builder() |
| 84 | + .url(server.url("/")) |
| 85 | + .post(RequestBody.create(null, "This is the request body")) |
| 86 | + .build(); |
| 87 | + |
| 88 | + try { |
| 89 | + client.newCall(request).execute(); |
| 90 | + fail("Expected a SocketTimeoutException to be thrown for write timeout"); |
| 91 | + } catch (SocketTimeoutException e) { |
| 92 | + // 写入超时,测试通过 |
| 93 | + assertTrue(true); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testReadTimeout() throws IOException { |
| 99 | + // 模拟服务器立即发送响应头,但延迟发送响应体 |
| 100 | + server.enqueue(new MockResponse() |
| 101 | + .setBody("Response body") |
| 102 | +// .setHeadersDelay(10,TimeUnit.SECONDS) |
| 103 | + .setBodyDelay(10, TimeUnit.SECONDS)); |
| 104 | + |
| 105 | + Request request = new Request.Builder() |
| 106 | + .url(server.url("/")) |
| 107 | + .build(); |
| 108 | + try { |
| 109 | + Response response = client.newCall(request).execute(); |
| 110 | + String responseBody = response.body().string(); |
| 111 | +// System.out.println(responseBody); |
| 112 | + // 读取超时,测试通过 |
| 113 | + fail("Expected a SocketTimeoutException to be thrown for read timeout"); |
| 114 | + } catch (SocketTimeoutException e) { |
| 115 | + // 写入超时,测试通过 |
| 116 | + assertTrue(true); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testReadTimeoutWithEventSource() throws IOException, InterruptedException { |
| 122 | + // 模拟服务器立即发送响应头,但延迟发送响应体 |
| 123 | + server.enqueue(new MockResponse() |
| 124 | + .setBody(": keep-alive") // SSE需要发送特定的响应头 |
| 125 | + .addHeader("Content-Type", "text/event-stream") // 设置正确的MIME类型 |
| 126 | + .setBodyDelay(5, TimeUnit.SECONDS) |
| 127 | + ); // 延迟发送事件 |
| 128 | + |
| 129 | + Request request = new Request.Builder() |
| 130 | + .url(server.url("/")) |
| 131 | + .build(); |
| 132 | + |
| 133 | + EventSource.Factory factory = EventSources.createFactory(client); |
| 134 | + |
| 135 | + EventSourceListener eventSourceListener = new EventSourceListener() { |
| 136 | + @Override |
| 137 | + public void onClosed(@NotNull EventSource eventSource) { |
| 138 | + log.info("closed {}", eventSource); |
| 139 | + super.onClosed(eventSource); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onEvent(@NotNull EventSource eventSource, @Nullable String id, @Nullable String type, @NotNull String data) { |
| 144 | + log.info("event"); |
| 145 | + super.onEvent(eventSource, id, type, data); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void onFailure(@NotNull EventSource eventSource, @Nullable Throwable t, @Nullable Response response) { |
| 150 | + assert t != null; |
| 151 | + log.error("返回异常信息:{}", t.getMessage()); |
| 152 | + |
| 153 | + try { |
| 154 | + log.error("response:{}", mapper.writeValueAsString(response)); |
| 155 | + } catch (Exception e) { |
| 156 | + log.error("", e); |
| 157 | + } |
| 158 | +// super.onFailure(eventSource, t, response); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onOpen(@NotNull EventSource eventSource, @NotNull Response response) { |
| 163 | + log.info("open {}", response); |
| 164 | + super.onOpen(eventSource, response); |
| 165 | + } |
| 166 | + }; |
| 167 | + |
| 168 | + log.info("request"); |
| 169 | + final EventSource eventSource = factory.newEventSource(request, eventSourceListener); |
| 170 | + |
| 171 | + // 等待足够的时间以触发超时 |
| 172 | + Thread.sleep(15000); // 等待时间应大于服务器延迟时间 |
| 173 | + |
| 174 | + // 测试结束,关闭EventSource |
| 175 | + } |
| 176 | +} |
0 commit comments