feat: modularize gateway requests and recover settlements
This commit is contained in:
1 parent
7785cda6a6
commit
5533a483d9
40 files changed
+1598
-232
No files matched your search
+25
@@ -112,6 +112,31 @@ class OpenAiCompatibleProviderHttpTest {
|
||||
assertEquals(List.of("Bearer route-key", "Bearer model-key"), authorization);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dynamicDatabaseChannelKeepsRotationAcrossRepeatedResolution() throws Exception {
|
||||
var authorization = new ArrayList<String>();
|
||||
server.createContext("/v1/chat/completions", exchange -> {
|
||||
authorization.add(exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
write(exchange, 200, "{\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1}}");
|
||||
});
|
||||
server.start();
|
||||
var jdbc = org.mockito.Mockito.mock(JdbcTemplate.class);
|
||||
var cipher = new SecretCipher("local-test-only");
|
||||
org.mockito.Mockito.when(jdbc.queryForMap(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.eq("dynamic-provider")))
|
||||
.thenReturn(Map.of("code", "dynamic-code", "base_url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1",
|
||||
"credential_ciphertext", cipher.encrypt("first-key")));
|
||||
environment.withProperty("MOYI_UPSTREAM_API_KEYS_DYNAMIC_CODE", "second-key");
|
||||
var dynamic = new OpenAiCompatibleProvider(json, environment, jdbc, cipher);
|
||||
try {
|
||||
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||
} finally { dynamic.close(); }
|
||||
|
||||
assertEquals(List.of("Bearer first-key", "Bearer second-key", "Bearer first-key"), authorization);
|
||||
}
|
||||
|
||||
private Map<String, Object> textInput(int maxTokens) {
|
||||
var input = new LinkedHashMap<String, Object>();
|
||||
input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK.")));
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.moyi.controlplane.provider.credentials;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UpstreamCredentialResolverTest {
|
||||
@Test
|
||||
void scopedPoolUsesRouteThenProviderModelAndNeverGlobalModelKeys() {
|
||||
var env = new MockEnvironment()
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-first | route-second | route-first")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_CODE_MODEL_1", "model-first|model-second")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_MODEL_1", "unscoped-key");
|
||||
var resolver = new UpstreamCredentialResolver(env);
|
||||
|
||||
assertEquals(List.of("route-first", "route-second"), resolver.scopedKeys("relay-code", "route-a", "model-1"));
|
||||
assertEquals(List.of("model-first", "model-second"), resolver.scopedKeys("relay-code", "route-b", "model-1"));
|
||||
assertTrue(resolver.scopedKeys("other-provider", "route-b", "model-1").isEmpty());
|
||||
assertTrue(resolver.scopedKeys("relay-code", null, null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void channelPoolPreservesFallbackAndExplicitAliasOrderAndDeduplicates() {
|
||||
var resolver = new UpstreamCredentialResolver(new MockEnvironment()
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_PROVIDER_A", "a|shared")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_CHANNEL_CODE", "b|shared"));
|
||||
|
||||
assertEquals(List.of("fallback", "a", "shared", "b"),
|
||||
resolver.providerKeys(" fallback ", "provider-a", "channel-code"));
|
||||
assertEquals(List.of("fallback", "b", "shared", "a"),
|
||||
resolver.providerKeys("fallback", "channel-code", "provider-a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsingHandlesBlankEntriesAndReturnsImmutablePools() {
|
||||
assertEquals(List.of("a", "b"), UpstreamCredentialResolver.parseKeys(" a | |b|a| "));
|
||||
assertTrue(UpstreamCredentialResolver.parseKeys(null).isEmpty());
|
||||
assertTrue(UpstreamCredentialResolver.parseKeys(" || ").isEmpty());
|
||||
assertThrows(UnsupportedOperationException.class, () -> UpstreamCredentialResolver.parseKeys("a").add("b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotationUsesPersistentCallerCursorAndSurvivesIntegerOverflow() {
|
||||
var cursor = new AtomicInteger(Integer.MAX_VALUE);
|
||||
var keys = List.of("a", "b", "c");
|
||||
|
||||
assertEquals(keys.get(Math.floorMod(Integer.MAX_VALUE, keys.size())), UpstreamCredentialResolver.nextKey(keys, cursor));
|
||||
assertEquals(keys.get(Math.floorMod(Integer.MIN_VALUE, keys.size())), UpstreamCredentialResolver.nextKey(keys, cursor));
|
||||
assertEquals(Integer.MIN_VALUE + 1, cursor.get());
|
||||
assertEquals("UPSTREAM_KEY_NOT_AVAILABLE", assertThrows(ApiException.class,
|
||||
() -> UpstreamCredentialResolver.nextKey(List.of(), cursor)).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void officialFallbackKeysAreReadOnlyWhenRequestedExplicitly() {
|
||||
var resolver = new UpstreamCredentialResolver(new MockEnvironment().withProperty("TRIPO_API_KEY", "tripo-a|tripo-b"));
|
||||
|
||||
assertTrue(resolver.providerKeys("", "provider-tripo").isEmpty());
|
||||
assertEquals(List.of("tripo-a", "tripo-b"), resolver.environmentKeys("TRIPO_API_KEY"));
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SseEventReaderTest {
|
||||
private final SseEventReader reader = new SseEventReader(new ObjectMapper());
|
||||
|
||||
@Test
|
||||
void fragmentedUtf8CrLfMultilineDataAndCommentsHaveOneEvent() throws Exception {
|
||||
String data = ": heartbeat\r\nevent: result\r\ndata: {\"content\":\r\ndata: \"你好\"}\r\n\r\ndata: [DONE]\r\n\r\n";
|
||||
var input = new FilterInputStream(bytes(data)) {
|
||||
@Override public int read(byte[] value, int offset, int length) throws IOException {
|
||||
return super.read(value, offset, Math.min(1, length));
|
||||
}
|
||||
};
|
||||
var events = new ArrayList<Map<String,Object>>();
|
||||
|
||||
reader.read(input, () -> false, true, events::add);
|
||||
|
||||
assertEquals(1, events.size());
|
||||
assertEquals("你好", events.getFirst().get("content"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void chatRequiresDoneAndNativeRequiresWholeFramesEvenWithoutDone() throws Exception {
|
||||
String event = "data: {\"type\":\"message_stop\"}\n\n";
|
||||
|
||||
assertEquals("UPSTREAM_STREAM_INCOMPLETE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes(event), () -> false, true, ignored -> {})).getCode());
|
||||
assertDoesNotThrow(() -> reader.read(bytes(event), () -> false, false, ignored -> {}));
|
||||
assertEquals("UPSTREAM_STREAM_INCOMPLETE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes("data: {\"type\":\"message_stop\"}"), () -> false, false, ignored -> {})).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedJsonAndUtf8NeverReachConsumer() {
|
||||
var delivered = new AtomicBoolean();
|
||||
assertThrows(IOException.class, () -> reader.read(bytes("data: {not json}\n\n"), () -> false, true,
|
||||
ignored -> delivered.set(true)));
|
||||
assertThrows(IOException.class, () -> reader.read(new ByteArrayInputStream(new byte[]{(byte) 0xc3, 0x28}),
|
||||
() -> false, true, ignored -> delivered.set(true)));
|
||||
assertFalse(delivered.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void frameAndActualUtf8ByteBudgetsAreBothBounded() {
|
||||
assertEquals("UPSTREAM_RESPONSE_TOO_LARGE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes("data: " + "x".repeat(262_145)), () -> false, true, ignored -> {})).getCode());
|
||||
// Fewer than 2M characters, but more than 4MiB on the wire; comments cannot bypass the budget.
|
||||
String multibyte = (": " + "中".repeat(1024) + "\n\n").repeat(1400);
|
||||
assertEquals("UPSTREAM_RESPONSE_TOO_LARGE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes(multibyte), () -> false, true, ignored -> {})).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void transportDeadlineMapsReadFailureAndTransportRetainsStreamOwnership() throws Exception {
|
||||
var closed = new AtomicBoolean();
|
||||
var input = new ByteArrayInputStream("data: [DONE]\n\n".getBytes(StandardCharsets.UTF_8)) {
|
||||
@Override public void close() { closed.set(true); }
|
||||
};
|
||||
reader.read(input, () -> false, true, ignored -> {});
|
||||
assertFalse(closed.get());
|
||||
assertEquals(504, assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes("data: {}\n\n"), () -> true, true, ignored -> {})).getStatus());
|
||||
}
|
||||
|
||||
private ByteArrayInputStream bytes(String text) {
|
||||
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user