fixed ai image timeout
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package com.krylov.refound.ai.client;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import com.krylov.refound.ai.config.AiProperties;
|
||||
import com.krylov.refound.ai.config.WebClientConfig;
|
||||
import com.krylov.refound.ai.exception.AiServerException;
|
||||
import com.krylov.refound.ai.exception.AiTimeoutException;
|
||||
import com.krylov.refound.ai.exception.AiUnavailableException;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
|
||||
class AiMultipartHttpClientTest {
|
||||
|
||||
private static final String URI = "/api/v1/moderation/image";
|
||||
|
||||
private HttpServer server;
|
||||
|
||||
@AfterEach
|
||||
void stopServer() {
|
||||
if (server != null) {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsResponseWhenServiceAnswersInTime() throws IOException {
|
||||
startServer(0, 200, "{\"approved\":true}");
|
||||
|
||||
Map<?, ?> response = client(baseUrl(), 2).post(URI, image(), Map.class);
|
||||
|
||||
assertThat(response.get("approved")).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void slowResponseBecomesTimeoutException() throws IOException {
|
||||
startServer(2500, 200, "{\"approved\":true}");
|
||||
|
||||
assertThatThrownBy(() -> client(baseUrl(), 1).post(URI, image(), Map.class))
|
||||
.isInstanceOf(AiTimeoutException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverErrorIsNotWrappedSoItCanBeRetried() throws IOException {
|
||||
startServer(0, 500, "boom");
|
||||
|
||||
assertThatThrownBy(() -> client(baseUrl(), 2).post(URI, image(), Map.class))
|
||||
.isInstanceOf(AiServerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void refusedConnectionBecomesUnavailableException() throws IOException {
|
||||
int freePort;
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
freePort = socket.getLocalPort();
|
||||
}
|
||||
|
||||
assertThatThrownBy(() -> client("http://localhost:" + freePort, 2).post(URI, image(), Map.class))
|
||||
.isInstanceOf(AiUnavailableException.class);
|
||||
}
|
||||
|
||||
private void startServer(long delayMs, int status, String body) throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
|
||||
server.createContext(URI, exchange -> {
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
try {
|
||||
Thread.sleep(delayMs);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().add("Content-Type", "application/json");
|
||||
try {
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(bytes);
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
// клиент уже закрыл соединение по таймауту
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
}
|
||||
|
||||
private String baseUrl() {
|
||||
return "http://localhost:" + server.getAddress().getPort();
|
||||
}
|
||||
|
||||
private static AiMultipartHttpClient client(String url, int imageTimeoutSeconds) {
|
||||
AiProperties properties = new AiProperties();
|
||||
properties.setUrl(url);
|
||||
properties.setTimeout(10);
|
||||
properties.setImageTimeout(imageTimeoutSeconds);
|
||||
return new AiMultipartHttpClient(new WebClientConfig().aiWebClient(properties), properties);
|
||||
}
|
||||
|
||||
private static MockMultipartFile image() {
|
||||
return new MockMultipartFile("file", "photo.jpg", "image/jpeg", new byte[] {1, 2, 3});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user