mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(bedrock-runtime): return errors when upstream error bodies stall (#5527)
* fix(bedrock-runtime): cancel abandoned calls and bound stalled error bodies Close the upstream request when the caller disconnects, including a stream stalled between tokens, and read streaming error bodies under a one-second deadline so a backend that stalls mid-error still returns ModelErrorException. Warnings keep the upstream status but omit the response body. * fix(bedrock-runtime): bound stalled error bodies on non-streaming converse Non-streaming Converse read the upstream body with ofString, so an error status followed by a stalled body hung until the caller gave up: the request timeout stops at the headers. Both Converse paths now share one send step that reads error bodies under the collection deadline. The deadline is configurable (error-body-timeout-millis, default 1000) so the tests run against a short one, and the raw-socket upstream fixture is shared between the cancellation and logging tests. * fix(bedrock-runtime): declare the error-body timeout in application.yml * fix(bedrock-runtime): schedule the error-body cut instead of polling for it The cancellation poll checked the deadline every 100 ms, so a stalled error body was cut 100 to 200 ms after it started. A timer armed when the error status arrives cuts it exactly, and the poll goes back to watching only for a caller disconnect. The test deadline drops to 10 ms. * fix(bedrock-runtime): validate the error-body timeout and tighten its tests Refuse a non-positive error-body-timeout-millis at startup, list the setting in the configuration table, and restore the non-streaming "Proxy request interrupted." wording. The complete-error-body tests now send headers and body in one write through StallingUpstream, so the 10 ms test deadline cannot race them, and the 8 KiB test asserts that the read ended at the cap rather than at the deadline. A shared ConverseRequests helper replaces the three hand-built Converse requests.
This commit is contained in:
1 parent
1f4a46804c
commit
4204ee2ea5
11 files changed
+340
-239
No files matched your search
+38
@@ -0,0 +1,38 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class BedrockRuntimeStartupValidatorTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, -1, Integer.MIN_VALUE})
|
||||
void nonPositiveErrorBodyTimeoutIsRejectedByStartupObserver(int value) {
|
||||
BedrockRuntimeStartupValidator validator = validator(value);
|
||||
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
||||
() -> validator.onStart(new StartupEvent()));
|
||||
assertEquals("floci.services.bedrock-runtime.proxy.error-body-timeout-millis must be positive",
|
||||
error.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {1, 10, 1000, Integer.MAX_VALUE})
|
||||
void positiveErrorBodyTimeoutIsAcceptedByStartupObserver(int value) {
|
||||
BedrockRuntimeStartupValidator validator = validator(value);
|
||||
assertDoesNotThrow(() -> validator.onStart(new StartupEvent()));
|
||||
}
|
||||
|
||||
private static BedrockRuntimeStartupValidator validator(int value) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().bedrockRuntime().proxy().errorBodyTimeoutMillis()).thenReturn(value);
|
||||
return new BedrockRuntimeStartupValidator(config);
|
||||
}
|
||||
}
|
||||
+26
-120
@@ -7,17 +7,11 @@ import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.PORT;
|
||||
@@ -42,63 +36,18 @@ class BedrockProxyCancellationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void disconnectClosesUpstreamWithoutAnotherToken() throws Exception {
|
||||
CountDownLatch closed = new CountDownLatch(1);
|
||||
try (ServerSocket server = new ServerSocket(PORT)) {
|
||||
server.setSoTimeout(10000);
|
||||
Thread upstream = Thread.ofVirtual().start(() -> {
|
||||
try (Socket socket = server.accept()) {
|
||||
socket.setSoTimeout(10000);
|
||||
InputStream input = socket.getInputStream();
|
||||
StringBuilder header = new StringBuilder();
|
||||
while (!header.toString().endsWith("\r\n\r\n")) {
|
||||
int next = input.read();
|
||||
if (next < 0 || header.length() > 65536) {
|
||||
return;
|
||||
}
|
||||
header.append((char) next);
|
||||
}
|
||||
int bytes = 0;
|
||||
for (String line : header.toString().split("\r\n")) {
|
||||
if (line.toLowerCase().startsWith("content-length:")) {
|
||||
bytes = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim());
|
||||
}
|
||||
}
|
||||
assertEquals(bytes, input.readNBytes(bytes).length);
|
||||
OutputStream output = socket.getOutputStream();
|
||||
output.write("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n".getBytes(StandardCharsets.US_ASCII));
|
||||
byte[] chunk = "data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}]}\n\n".getBytes(StandardCharsets.UTF_8);
|
||||
output.write((Integer.toHexString(chunk.length) + "\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
output.write(chunk);
|
||||
output.write("\r\n".getBytes(StandardCharsets.US_ASCII));
|
||||
output.flush();
|
||||
// No more output: disconnect must close the transport independently of token delivery.
|
||||
try {
|
||||
if (input.read() == -1) {
|
||||
closed.countDown();
|
||||
}
|
||||
} catch (SocketException disconnected) {
|
||||
closed.countDown();
|
||||
}
|
||||
} catch (Exception expected) {
|
||||
// The latch remains unset so a backend setup failure cannot pass the assertion.
|
||||
}
|
||||
});
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
HttpRequest request = HttpRequest.newBuilder(base.resolve("/model/fixture/converse-stream"))
|
||||
.timeout(Duration.ofSeconds(10)).header("Content-Type", "application/json")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request")
|
||||
.POST(HttpRequest.BodyPublishers.ofString("{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}"))
|
||||
.build();
|
||||
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
assertEquals(200, response.statusCode());
|
||||
try (InputStream body = response.body()) {
|
||||
assertTrue(body.read() >= 0);
|
||||
}
|
||||
assertTrue(closed.await(5, TimeUnit.SECONDS), "Client disconnect must close the stalled upstream");
|
||||
} finally {
|
||||
upstream.interrupt();
|
||||
upstream.join(1000);
|
||||
String chunk = "data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}]}\n\n";
|
||||
String firstToken = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n"
|
||||
+ Integer.toHexString(chunk.getBytes(StandardCharsets.UTF_8).length) + "\r\n" + chunk + "\r\n";
|
||||
try (StallingUpstream upstream = new StallingUpstream(PORT, firstToken);
|
||||
HttpClient client = HttpClient.newHttpClient()) {
|
||||
HttpResponse<InputStream> response = client.send(ConverseRequests.to(base, "converse-stream"),
|
||||
HttpResponse.BodyHandlers.ofInputStream());
|
||||
assertEquals(200, response.statusCode());
|
||||
try (InputStream body = response.body()) {
|
||||
assertTrue(body.read() >= 0);
|
||||
}
|
||||
assertTrue(upstream.awaitClosed(5, TimeUnit.SECONDS), "Client disconnect must close the stalled upstream");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,64 +67,21 @@ class BedrockProxyCancellationIntegrationTest {
|
||||
}
|
||||
|
||||
private void disconnectWhileUpstreamStalls(String route, boolean partialResponse) throws Exception {
|
||||
CountDownLatch accepted = new CountDownLatch(1);
|
||||
CountDownLatch closed = new CountDownLatch(1);
|
||||
try (ServerSocket server = new ServerSocket(PORT)) {
|
||||
server.setSoTimeout(10000);
|
||||
Thread upstream = Thread.ofVirtual().start(() -> {
|
||||
try (Socket socket = server.accept()) {
|
||||
socket.setSoTimeout(10000);
|
||||
InputStream input = socket.getInputStream();
|
||||
StringBuilder header = new StringBuilder();
|
||||
while (!header.toString().endsWith("\r\n\r\n")) {
|
||||
int next = input.read();
|
||||
if (next < 0 || header.length() > 65536) {
|
||||
return;
|
||||
}
|
||||
header.append((char) next);
|
||||
}
|
||||
int bytes = 0;
|
||||
for (String line : header.toString().split("\r\n")) {
|
||||
if (line.toLowerCase().startsWith("content-length:")) {
|
||||
bytes = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim());
|
||||
}
|
||||
}
|
||||
if (input.readNBytes(bytes).length != bytes) {
|
||||
return;
|
||||
}
|
||||
if (partialResponse) {
|
||||
socket.getOutputStream().write(("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
|
||||
+ "Content-Length: 1000\r\n\r\n{").getBytes(StandardCharsets.US_ASCII));
|
||||
socket.getOutputStream().flush();
|
||||
}
|
||||
accepted.countDown();
|
||||
// The upstream stalls before completing its response, so cancellation must close it.
|
||||
try {
|
||||
if (input.read() == -1) {
|
||||
closed.countDown();
|
||||
}
|
||||
} catch (SocketException disconnected) {
|
||||
closed.countDown();
|
||||
}
|
||||
} catch (Exception expected) {
|
||||
// An unset latch exposes setup and transport failures to the test thread.
|
||||
}
|
||||
});
|
||||
try (Socket frontend = new Socket(base.getHost(), base.getPort())) {
|
||||
String body = "{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}";
|
||||
String request = "POST /model/fixture/" + route + " HTTP/1.1\r\nHost: localhost\r\n"
|
||||
+ "Content-Type: application/json\r\n"
|
||||
+ "Authorization: AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request\r\n"
|
||||
+ "Content-Length: " + body.getBytes(StandardCharsets.UTF_8).length + "\r\n\r\n" + body;
|
||||
frontend.getOutputStream().write(request.getBytes(StandardCharsets.UTF_8));
|
||||
frontend.getOutputStream().flush();
|
||||
assertTrue(accepted.await(5, TimeUnit.SECONDS), "Upstream must receive the request before disconnect");
|
||||
frontend.close();
|
||||
assertTrue(closed.await(5, TimeUnit.SECONDS), "Disconnect must close the stalled upstream request");
|
||||
} finally {
|
||||
upstream.interrupt();
|
||||
upstream.join(1000);
|
||||
}
|
||||
String prefix = partialResponse
|
||||
? "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 1000\r\n\r\n{"
|
||||
: "";
|
||||
try (StallingUpstream upstream = new StallingUpstream(PORT, prefix);
|
||||
Socket frontend = new Socket(base.getHost(), base.getPort())) {
|
||||
String body = ConverseRequests.BODY;
|
||||
String request = "POST /model/fixture/" + route + " HTTP/1.1\r\nHost: localhost\r\n"
|
||||
+ "Content-Type: application/json\r\n"
|
||||
+ "Authorization: " + ConverseRequests.AUTHORIZATION + "\r\n"
|
||||
+ "Content-Length: " + body.getBytes(StandardCharsets.UTF_8).length + "\r\n\r\n" + body;
|
||||
frontend.getOutputStream().write(request.getBytes(StandardCharsets.UTF_8));
|
||||
frontend.getOutputStream().flush();
|
||||
assertTrue(upstream.awaitResponded(5, TimeUnit.SECONDS), "Upstream must receive the request before disconnect");
|
||||
frontend.close();
|
||||
assertTrue(upstream.awaitClosed(5, TimeUnit.SECONDS), "Disconnect must close the stalled upstream request");
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
-53
@@ -1,26 +1,21 @@
|
||||
package io.github.hectorvent.floci.services.bedrockruntime;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.services.bedrockruntime.backend.ProxyBackend;
|
||||
import io.github.hectorvent.floci.testing.BedrockProxyBackendProfile;
|
||||
import io.github.hectorvent.floci.testutil.LogCapture;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
@@ -30,6 +25,7 @@ import static io.github.hectorvent.floci.testing.BedrockProxyBackendProfile.PORT
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -51,25 +47,17 @@ class BedrockProxyLoggingIntegrationTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"converse", "converse-stream"})
|
||||
void upstreamErrorIsLoggedWithoutResponseBody(String operation) throws IOException {
|
||||
void upstreamErrorIsLoggedWithoutResponseBody(String operation) throws Exception {
|
||||
String marker = "synthetic-private-upstream-content";
|
||||
HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", PORT), 0);
|
||||
backend.createContext("/v1/chat/completions", exchange -> {
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
byte[] body = ("{\"error\":\"" + marker + "\"}").getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().add("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(503, body.length);
|
||||
try (exchange) {
|
||||
exchange.getResponseBody().write(body);
|
||||
}
|
||||
});
|
||||
backend.start();
|
||||
try {
|
||||
String body = "{\"error\":\"" + marker + "\"}";
|
||||
String completeError = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\n"
|
||||
+ "Content-Length: " + body.getBytes(StandardCharsets.UTF_8).length + "\r\n\r\n" + body;
|
||||
try (StallingUpstream upstream = new StallingUpstream(PORT, completeError)) {
|
||||
List<LogRecord> records = LogCapture.capture(ProxyBackend.class, () ->
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request")
|
||||
.body("{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}")
|
||||
.header("Authorization", ConverseRequests.AUTHORIZATION)
|
||||
.body(ConverseRequests.BODY)
|
||||
.when()
|
||||
.post("/model/fixture/" + operation)
|
||||
.then()
|
||||
@@ -82,45 +70,50 @@ class BedrockProxyLoggingIntegrationTest {
|
||||
&& formatter.format(record).contains("HTTP 503")), "The upstream status must remain diagnosable");
|
||||
assertFalse(records.stream().anyMatch(record -> formatter.format(record).contains(marker)),
|
||||
"Operational logs must not include upstream response bodies");
|
||||
} finally {
|
||||
backend.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamingErrorBodyReadIsBoundedWithoutWaitingForEof() throws Exception {
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", PORT), 0);
|
||||
backend.createContext("/v1/chat/completions", exchange -> {
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
exchange.sendResponseHeaders(503, 32768);
|
||||
try (exchange) {
|
||||
exchange.getResponseBody().write("x".repeat(8192).getBytes(StandardCharsets.UTF_8));
|
||||
exchange.getResponseBody().flush();
|
||||
try {
|
||||
release.await(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
backend.start();
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
HttpRequest request = HttpRequest.newBuilder(base.resolve("/model/fixture/converse-stream"))
|
||||
.timeout(Duration.ofSeconds(5))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(
|
||||
"{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}"))
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"converse", "converse-stream"})
|
||||
void errorBodyReadStopsAtTheCapWithoutWaitingForEof(String operation) throws Exception {
|
||||
String cappedError = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\nContent-Length: 32768\r\n\r\n"
|
||||
+ "x".repeat(8192);
|
||||
try (StallingUpstream upstream = new StallingUpstream(PORT, cappedError);
|
||||
HttpClient client = HttpClient.newHttpClient()) {
|
||||
HttpResponse<String> response = client.send(ConverseRequests.to(base, operation),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(424, response.statusCode());
|
||||
assertTrue(response.body().contains("ModelErrorException"));
|
||||
assertTrue(response.body().contains("HTTP 503"));
|
||||
// Only a read that ends at the cap carries the body; the deadline cut would return none of it.
|
||||
assertTrue(response.body().contains("x".repeat(512)), "The error read must end at the 8 KiB cap");
|
||||
assertTrue(response.body().length() < 1024, "Caller error descriptions must remain bounded");
|
||||
} finally {
|
||||
release.countDown();
|
||||
backend.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"converse, 0", "converse, 1", "converse, 50",
|
||||
"converse-stream, 0", "converse-stream, 1", "converse-stream, 50"})
|
||||
void stalledErrorBodyHasDeadlineAndClosesUpstream(String operation, int lines) throws Exception {
|
||||
String marker = "synthetic-private-upstream-content";
|
||||
String errorPrefix = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\nContent-Length: 32768\r\n\r\n"
|
||||
+ (marker + "\n").repeat(lines);
|
||||
try (StallingUpstream upstream = new StallingUpstream(PORT, errorPrefix);
|
||||
HttpClient client = HttpClient.newHttpClient()) {
|
||||
HttpRequest request = ConverseRequests.to(base, operation);
|
||||
List<LogRecord> records = LogCapture.capture(ProxyBackend.class, () -> {
|
||||
HttpResponse<String> response = assertDoesNotThrow(
|
||||
() -> client.send(request, HttpResponse.BodyHandlers.ofString()));
|
||||
assertEquals(424, response.statusCode());
|
||||
assertTrue(response.body().contains("ModelErrorException"));
|
||||
assertTrue(response.body().contains("HTTP 503"));
|
||||
assertTrue(response.body().length() < 1024);
|
||||
assertTrue(assertDoesNotThrow(() -> upstream.awaitClosed(1, TimeUnit.SECONDS)),
|
||||
"The proxy must close the stalled upstream body");
|
||||
});
|
||||
SimpleFormatter formatter = new SimpleFormatter();
|
||||
assertFalse(records.stream().anyMatch(record -> formatter.format(record).contains(marker)));
|
||||
assertTrue(records.stream().anyMatch(record -> formatter.format(record).contains("HTTP 503")));
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.services.bedrockruntime;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* The Converse request the proxy tests send: a SigV4-shaped Authorization header and one user
|
||||
* message, to the {@code fixture} model id, which reaches the stand-in through passthrough.
|
||||
*/
|
||||
final class ConverseRequests {
|
||||
|
||||
static final String AUTHORIZATION = "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request";
|
||||
static final String BODY = "{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}";
|
||||
|
||||
private ConverseRequests() {
|
||||
}
|
||||
|
||||
static HttpRequest to(URI base, String operation) {
|
||||
return HttpRequest.newBuilder(base.resolve("/model/fixture/" + operation))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(BODY))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package io.github.hectorvent.floci.services.bedrockruntime;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
/**
|
||||
* A raw-socket stand-in for an OpenAI-compatible backend. It accepts one request, writes a
|
||||
* fixed response prefix, then stalls, so a test can observe whether Floci closes the upstream.
|
||||
*/
|
||||
final class StallingUpstream implements AutoCloseable {
|
||||
|
||||
private final ServerSocket server;
|
||||
private final Thread thread;
|
||||
private final CountDownLatch responded = new CountDownLatch(1);
|
||||
private final CompletableFuture<Boolean> closed = new CompletableFuture<>();
|
||||
|
||||
StallingUpstream(int port, String responsePrefix) throws IOException {
|
||||
server = new ServerSocket(port);
|
||||
server.setSoTimeout(10000);
|
||||
thread = Thread.ofVirtual().start(() -> serve(responsePrefix));
|
||||
}
|
||||
|
||||
private void serve(String responsePrefix) {
|
||||
try (Socket socket = server.accept()) {
|
||||
socket.setSoTimeout(10000);
|
||||
InputStream input = socket.getInputStream();
|
||||
StringBuilder headers = new StringBuilder();
|
||||
while (!headers.toString().endsWith("\r\n\r\n")) {
|
||||
int next = input.read();
|
||||
if (next < 0 || headers.length() > 65536) {
|
||||
throw new IOException("Incomplete fixture request headers");
|
||||
}
|
||||
headers.append((char) next);
|
||||
}
|
||||
int length = 0;
|
||||
for (String header : headers.toString().split("\r\n")) {
|
||||
if (header.toLowerCase().startsWith("content-length:")) {
|
||||
length = Integer.parseInt(header.substring(header.indexOf(':') + 1).trim());
|
||||
}
|
||||
}
|
||||
if (input.readNBytes(length).length != length) {
|
||||
throw new IOException("Incomplete fixture request body");
|
||||
}
|
||||
OutputStream output = socket.getOutputStream();
|
||||
output.write(responsePrefix.getBytes(StandardCharsets.UTF_8));
|
||||
output.flush();
|
||||
responded.countDown();
|
||||
// Nothing more is sent, so only Floci closing its side ends this read.
|
||||
try {
|
||||
closed.complete(input.read() == -1);
|
||||
} catch (SocketException disconnected) {
|
||||
closed.complete(true);
|
||||
}
|
||||
} catch (Exception failure) {
|
||||
closed.completeExceptionally(failure);
|
||||
}
|
||||
}
|
||||
|
||||
boolean awaitResponded(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
return responded.await(timeout, unit);
|
||||
}
|
||||
|
||||
boolean awaitClosed(long timeout, TimeUnit unit) throws Exception {
|
||||
try {
|
||||
return closed.get(timeout, unit);
|
||||
} catch (TimeoutException stillOpen) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
thread.interrupt();
|
||||
thread.join(1000);
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
@@ -21,12 +21,16 @@ import java.util.Map;
|
||||
* <li>{@code proxy.passthrough=true} forwards a model id with no mapping as it is.
|
||||
* {@code BedrockProxyPassthroughIntegrationTest}, {@code BedrockProxyCancellationIntegrationTest}
|
||||
* and {@code BedrockProxyLoggingIntegrationTest} rely on it.</li>
|
||||
* <li>{@code proxy.error-body-timeout-millis=10} shortens the deadline for collecting an
|
||||
* upstream error body, so the stalled-error-body tests do not each wait the one-second default.
|
||||
* {@code BedrockProxyLoggingIntegrationTest} relies on it.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Each override is inert for the classes that do not rely on it. Only the stand-in in
|
||||
* {@code BedrockProxyIntegrationTest} reads the {@code Authorization} header. The other classes
|
||||
* call model ids the mapping does not name, and every call in {@code BedrockProxyIntegrationTest}
|
||||
* names the mapped id, which the mapping resolves before passthrough is consulted.
|
||||
* names the mapped id, which the mapping resolves before passthrough is consulted. Their stand-ins
|
||||
* write each error body whole, so the shorter error-body deadline never fires for them.
|
||||
*
|
||||
* <p>{@code BedrockProxyTimeoutIntegrationTest} stays apart. Its one-second request timeout would
|
||||
* also close an upstream that stalls before its headers, so the cancellation tests would pass
|
||||
@@ -44,6 +48,7 @@ public class BedrockProxyBackendProfile implements QuarkusTestProfile {
|
||||
"floci.services.bedrock-runtime.proxy.url", "http://127.0.0.1:" + PORT + "/v1",
|
||||
"floci.services.bedrock-runtime.proxy.api-key", "test-key",
|
||||
"floci.services.bedrock-runtime.proxy.model-mapping", MAPPED_MODEL_ID + "=claude-3-haiku",
|
||||
"floci.services.bedrock-runtime.proxy.passthrough", "true");
|
||||
"floci.services.bedrock-runtime.proxy.passthrough", "true",
|
||||
"floci.services.bedrock-runtime.proxy.error-body-timeout-millis", "10");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user