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
+67
-3
@@ -308,7 +308,7 @@ class PlatformAdminPostgresTest {
|
||||
} finally { org.springframework.web.context.request.RequestContextHolder.resetRequestAttributes(); }
|
||||
var saved=jdbc.queryForMap("SELECT * FROM usage_events WHERE user_id=?",memberId);
|
||||
assertEquals("/v1/chat/completions",saved.get("endpoint"));
|
||||
assertEquals("playground",saved.get("request_source"));
|
||||
assertEquals("api",saved.get("request_source"),"a caller-supplied source header must not reclassify a public API request");
|
||||
assertEquals("Local test browser",saved.get("user_agent"));
|
||||
assertEquals(80L,saved.get("cached_input_units"));
|
||||
assertEquals(5L,saved.get("cache_write_units"));
|
||||
@@ -482,7 +482,13 @@ class PlatformAdminPostgresTest {
|
||||
.withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+upstream.getAddress().getPort()+"/v1");
|
||||
var provider=new com.moyi.controlplane.provider.OpenAiCompatibleProvider(json,environment,jdbc,new SecretCipher("local-test"));
|
||||
try {
|
||||
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class),f.billing(),json);
|
||||
var guard=org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class);
|
||||
org.mockito.Mockito.doReturn(org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.Admission.class))
|
||||
.when(guard).acquire(org.mockito.ArgumentMatchers.anyMap(),org.mockito.ArgumentMatchers.any(java.util.UUID.class),org.mockito.ArgumentMatchers.anyLong());
|
||||
var requestExecution=new com.moyi.controlplane.gateway.request.GatewayRequestExecutionService(
|
||||
f.billing(),new com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy(
|
||||
com.moyi.controlplane.gateway.request.ApiRequestRiskEngine.allowAll()));
|
||||
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,guard,requestExecution,json);
|
||||
var http=MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
var input=new LinkedHashMap<>(f.request().body());
|
||||
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("Idempotency-Key","retry-test").contentType("application/json").content(json.writeValueAsBytes(input)))
|
||||
@@ -494,7 +500,7 @@ class PlatformAdminPostgresTest {
|
||||
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/event-stream")).andReturn().getResponse().getContentAsString();
|
||||
assertTrue(streamed.endsWith("data: [DONE]\n\n")); assertFalse(streamed.contains("private-alias"));
|
||||
assertEquals(new BigDecimal("0.99932000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='playground' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='api' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
||||
mode.set("no-usage");
|
||||
var failed=http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).contentType("application/json").content(json.writeValueAsBytes(input))).andReturn().getResponse().getContentAsString();
|
||||
assertTrue(failed.contains("UPSTREAM_USAGE_MISSING"));
|
||||
@@ -530,4 +536,62 @@ class PlatformAdminPostgresTest {
|
||||
f.billing().fail(ticket,"SETTLEMENT_FAILED","Test rollback",5);
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
}
|
||||
|
||||
@Test void explicitRequestMetadataPersistsWithoutThreadLocalHttpContext() {
|
||||
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","metadata test",meta);
|
||||
var metadata=com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground("198.51.100.7");
|
||||
var ticket=f.billing().reserve(f.key(),f.model(),f.request(),"metadata",metadata);
|
||||
var saved=jdbc.queryForMap("SELECT request_source,host(source_ip) AS client_ip FROM usage_events WHERE id=?",ticket.id());
|
||||
assertEquals("playground",saved.get("request_source"));
|
||||
assertEquals("198.51.100.7",saved.get("client_ip"));
|
||||
f.billing().fail(ticket,"TEST_COMPLETE","metadata fixture",1);
|
||||
|
||||
var audit=context.getBean(UsageAuditService.class);
|
||||
audit.recordTask(memberId,UUID.fromString(String.valueOf(f.key().get("id"))),String.valueOf(f.model().get("id")),
|
||||
String.valueOf(f.model().get("providerId")),"metadata-task","queued",Map.of(),null,null,null,
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.fromHttp("203.0.113.8"));
|
||||
saved=jdbc.queryForMap("SELECT request_source,host(source_ip) AS client_ip FROM usage_events WHERE provider_task_id='metadata-task'");
|
||||
assertEquals("api",saved.get("request_source"));
|
||||
assertEquals("203.0.113.8",saved.get("client_ip"));
|
||||
}
|
||||
|
||||
@Test void gatewaySettlementFailureKeepsDurableMeterAndRecoversExactlyOnce() {
|
||||
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","recoverable settlement test",meta);
|
||||
var billing=f.billing();
|
||||
var execution=new com.moyi.controlplane.gateway.request.GatewayRequestExecutionService(billing,
|
||||
new com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy(
|
||||
com.moyi.controlplane.gateway.request.ApiRequestRiskEngine.allowAll()));
|
||||
var requestContext=new com.moyi.controlplane.gateway.request.ApiRequestContext(memberId,
|
||||
UUID.fromString(String.valueOf(f.key().get("id"))),
|
||||
com.moyi.controlplane.gateway.request.ApiRequestProtocol.CHAT_COMPLETIONS,
|
||||
String.valueOf(f.model().get("id")),String.valueOf(f.model().get("id")),false,
|
||||
f.request().reservedInputTokens(),f.request().maxOutputTokens(),
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.internal(),System.nanoTime());
|
||||
jdbc.execute("ALTER TABLE usage_events ADD CONSTRAINT test_reject_recoverable_cost CHECK (cost_amount IS NULL OR cost_amount=0)");
|
||||
try {
|
||||
String result=execution.execute(requestContext,f.key(),f.model(),f.request(),null,
|
||||
() -> (com.moyi.controlplane.gateway.request.ApiRequestAdmission)()->{},
|
||||
ticket -> { execution.settle(ticket,meteredResult(),5,null); return "unexpected"; },
|
||||
failure -> failure instanceof ApiException api ? api : new ApiException("CALL_INTERRUPTED","interrupted",502),
|
||||
ApiException::getCode);
|
||||
assertEquals("SETTLEMENT_PENDING",result);
|
||||
UUID ticketId=jdbc.queryForObject("SELECT id FROM chat_billing_requests WHERE user_id=? ORDER BY created_at DESC LIMIT 1",UUID.class,memberId);
|
||||
assertEquals("pending",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticketId));
|
||||
assertNotNull(jdbc.queryForObject("SELECT settlement_usage FROM chat_billing_requests WHERE id=?",Object.class,ticketId));
|
||||
assertTrue(jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum()>0);
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticketId));
|
||||
|
||||
jdbc.execute("ALTER TABLE usage_events DROP CONSTRAINT test_reject_recoverable_cost");
|
||||
jdbc.update("UPDATE chat_billing_requests SET expires_at=now()-interval '1 second' WHERE id=?",ticketId);
|
||||
billing.recoverExpired();
|
||||
billing.recoverExpired();
|
||||
|
||||
assertEquals("succeeded",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticketId));
|
||||
assertEquals(new BigDecimal("0.99966000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticketId));
|
||||
} finally {
|
||||
jdbc.execute("ALTER TABLE usage_events DROP CONSTRAINT IF EXISTS test_reject_recoverable_cost");
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
-9
@@ -7,6 +7,9 @@ import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskEngine;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
|
||||
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -20,6 +23,13 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
private static GatewayController controller(CatalogService catalog, OpenAiCompatibleProvider provider,
|
||||
GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||
doReturn(mock(GatewayGuard.Admission.class)).when(guard).acquire(anyMap(), any(UUID.class), anyLong());
|
||||
var requests = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
return new GatewayController(catalog, provider, guard, requests, json);
|
||||
}
|
||||
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
||||
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
||||
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
||||
@@ -124,7 +134,7 @@ class GatewayDeploymentTest {
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||
when(catalog.keyCanInvoke(key, "route-1")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
|
||||
Map<String, Object> response = controller.models("Bearer test-key");
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -163,7 +173,7 @@ class GatewayDeploymentTest {
|
||||
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
|
||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
|
||||
verify(catalog).keyCanInvoke(key, "route-readable");
|
||||
}
|
||||
@@ -236,7 +246,7 @@ class GatewayDeploymentTest {
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
||||
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
@@ -251,9 +261,9 @@ class GatewayDeploymentTest {
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||
var billing=mock(ChatBillingService.class);
|
||||
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||
when(billing.reserve(anyMap(),anyMap(),any(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
|
||||
}
|
||||
|
||||
@@ -276,10 +286,10 @@ class GatewayDeploymentTest {
|
||||
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
|
||||
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
|
||||
@@ -307,8 +317,8 @@ class GatewayDeploymentTest {
|
||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
|
||||
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
|
||||
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayController;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.gateway.NativeGatewayController;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ApiRequestEntryPointsTest {
|
||||
private final UUID userId=UUID.randomUUID(), keyId=UUID.randomUUID();
|
||||
private final Map<String,Object> key=Map.of("id",keyId.toString(),"userId",userId.toString());
|
||||
private final CatalogService catalog=mock(CatalogService.class);
|
||||
private final GatewayGuard guard=mock(GatewayGuard.class);
|
||||
private final ChatBillingService billing=mock(ChatBillingService.class);
|
||||
private final OpenAiCompatibleProvider provider=mock(OpenAiCompatibleProvider.class);
|
||||
private final AtomicReference<ApiRequestContext> observed=new AtomicReference<>();
|
||||
private final GatewayRequestExecutionService requests=new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(context -> {
|
||||
observed.set(context);
|
||||
return ApiRequestRiskDecision.deny(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||
"test.entry-block","RISK_BLOCKED","Blocked",0));
|
||||
}));
|
||||
|
||||
@Test void publicChatIgnoresSpoofedPlaygroundHeaderAndDenialNeverAcquiresOrReserves() {
|
||||
Map<String,Object> model=Map.of("id","chat-route","providerId","provider-mock","type","text","mode","sync");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("chat-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key,"chat-route")).thenReturn(true);
|
||||
var controller=new GatewayController(catalog,provider,guard,requests,new ObjectMapper());
|
||||
var request=http("/v1/chat/completions");
|
||||
|
||||
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,() -> controller.chatCompletions("Bearer test-key",
|
||||
Map.of("model","chat-route","messages",List.of(Map.of("role","user","content","hello"))),null,
|
||||
request,new MockHttpServletResponse())).getCode());
|
||||
|
||||
assertEquals(ApiRequestMetadata.Source.API,observed.get().metadata().source());
|
||||
assertEquals("198.51.100.7",observed.get().metadata().clientIp());
|
||||
verify(guard).checkRate(keyId,false);
|
||||
verify(guard,never()).acquire(anyMap(),any(UUID.class),anyLong());
|
||||
verifyNoInteractions(billing,provider);
|
||||
}
|
||||
|
||||
@Test void nativePublicAndPlaygroundBothEnterRiskBoundaryWithEndpointOwnedSource() {
|
||||
Map<String,Object> model=Map.of("id","embedding-route","providerId","provider-a","upstreamModel","embedding-model","type","embedding","mode","sync");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("embedding-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key,"embedding-route")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-a","embedding-route","embedding-model")).thenReturn(true);
|
||||
var controller=new NativeGatewayController(catalog,guard,requests,provider,new ObjectMapper());
|
||||
var request=http("/v1/embeddings");
|
||||
request.addHeader("Authorization","Bearer test-key");
|
||||
Map<String,Object> input=Map.of("model","embedding-route","input","hello");
|
||||
|
||||
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,
|
||||
() -> controller.invoke(input,request,new MockHttpServletResponse())).getCode());
|
||||
assertEquals(ApiRequestProtocol.EMBEDDINGS,observed.get().protocol());
|
||||
assertEquals(ApiRequestMetadata.Source.API,observed.get().metadata().source());
|
||||
|
||||
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,
|
||||
() -> controller.executeForKey("embeddings",input,false,key,request,new MockHttpServletResponse(),
|
||||
ApiRequestMetadata.playground(request.getRemoteAddr()))).getCode());
|
||||
assertEquals(ApiRequestMetadata.Source.PLAYGROUND,observed.get().metadata().source());
|
||||
verify(guard,never()).acquire(anyMap(),any(UUID.class),anyLong());
|
||||
verify(provider,never()).invoke(anyString(),anyString(),anyString(),anyMap(),anyString());
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
@Test void malformedRequestsStillConsumeTheirEarlyRateBudget() {
|
||||
var controller=new NativeGatewayController(catalog,guard,requests,provider,new ObjectMapper());
|
||||
assertThrows(ApiException.class,() -> controller.executeForKey("embeddings",Map.of(),false,key,
|
||||
http("/v1/embeddings"),new MockHttpServletResponse()));
|
||||
verify(guard).checkRate(keyId,false);
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest http(String path) {
|
||||
var request=new MockHttpServletRequest("POST",path);
|
||||
request.setRemoteAddr("198.51.100.7");
|
||||
request.addHeader("X-Moyi-Source","playground");
|
||||
request.addHeader("X-Forwarded-For","203.0.113.99"); // Adapter never parses this untrusted header itself.
|
||||
return request;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ApiRequestModuleConfigurationTest {
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(ApiRequestModuleConfiguration.class);
|
||||
private final ApiRequestContext request = new ApiRequestContext(UUID.randomUUID(), UUID.randomUUID(),
|
||||
ApiRequestProtocol.EMBEDDINGS, "public", "route", false, 12, 0,
|
||||
ApiRequestMetadata.fromHttp("198.51.100.7"), System.nanoTime());
|
||||
|
||||
@Test void noRulesStartsSafelyAndDefaultsToShadow() {
|
||||
runner.run(context -> {
|
||||
assertNull(context.getStartupFailure());
|
||||
assertEquals(ApiRequestRiskDecision.Action.ALLOW, context.getBean(ApiRequestRiskEngine.class).evaluate(request).action());
|
||||
});
|
||||
}
|
||||
|
||||
@Test void installedRuleIsShadowedByDefaultAndEnforcedOnlyWhenConfigured() {
|
||||
var configured = runner.withBean("blockedRule", ApiRequestRiskRule.class, () -> rule("test.block", 0));
|
||||
configured.run(context -> assertEquals(ApiRequestRiskDecision.Action.SHADOW,
|
||||
context.getBean(ApiRequestRiskEngine.class).evaluate(request).action()));
|
||||
configured.withPropertyValues("moyi.gateway.risk-mode=enforce").run(context -> assertEquals(
|
||||
ApiRequestRiskDecision.Action.DENY, context.getBean(ApiRequestRiskEngine.class).evaluate(request).action()));
|
||||
}
|
||||
|
||||
@Test void throttleAggregationHonorsTheLongestRetryAndRuleFailureClosesOnlyInEnforce() {
|
||||
var engine = new CompositeApiRequestRiskEngine(java.util.List.of(rule("test.short", 2), rule("test.long", 30)),
|
||||
CompositeApiRequestRiskEngine.Mode.ENFORCE);
|
||||
assertEquals(30, engine.evaluate(request).retryAfterSeconds());
|
||||
ApiRequestRiskRule broken = new ApiRequestRiskRule() {
|
||||
public String id() { return "test.broken"; }
|
||||
public Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context) { throw new IllegalStateException(); }
|
||||
};
|
||||
assertEquals(ApiRequestRiskDecision.Action.SHADOW, new CompositeApiRequestRiskEngine(java.util.List.of(broken),
|
||||
CompositeApiRequestRiskEngine.Mode.SHADOW).evaluate(request).action());
|
||||
assertEquals(ApiRequestRiskDecision.Action.UNAVAILABLE, new CompositeApiRequestRiskEngine(java.util.List.of(broken),
|
||||
CompositeApiRequestRiskEngine.Mode.ENFORCE).evaluate(request).action());
|
||||
assertFalse(request.metadata().toString().contains("198.51.100.7"));
|
||||
}
|
||||
|
||||
@Test void invalidModeDoesNotSilentlyDisableRisk() {
|
||||
runner.withPropertyValues("moyi.gateway.risk-mode=typo").run(context -> assertNotNull(context.getStartupFailure()));
|
||||
}
|
||||
|
||||
private ApiRequestRiskRule rule(String id, int retry) {
|
||||
return new ApiRequestRiskRule() {
|
||||
public String id() { return id; }
|
||||
public Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context) {
|
||||
return Optional.of(new ApiRequestRiskSignal(retry == 0 ? ApiRequestRiskSignal.Action.DENY : ApiRequestRiskSignal.Action.THROTTLE,
|
||||
id, "RISK_BLOCKED", "Blocked", retry));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.ChatMeter;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class GatewayRequestExecutionServiceTest {
|
||||
private final UUID userId = UUID.randomUUID();
|
||||
private final UUID keyId = UUID.randomUUID();
|
||||
private final ApiRequestContext context = new ApiRequestContext(userId, keyId,
|
||||
ApiRequestProtocol.CHAT_COMPLETIONS, "public-model", "route-1", false,
|
||||
256, 128, ApiRequestMetadata.fromHttp("127.0.0.1"), System.nanoTime());
|
||||
private final Map<String, Object> key = Map.of("id", keyId.toString(), "userId", userId.toString());
|
||||
private final Map<String, Object> model = Map.of("id", "route-1");
|
||||
private final ChatMeter.Request normalized = new ChatMeter.Request(Map.of("model", "public-model"), 256, 128);
|
||||
|
||||
@Test
|
||||
void denyHappensBeforeAdmissionAndWalletReservation() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var service = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.deny(
|
||||
new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY, "test.block", "RISK_BLOCKED", "Blocked", 0))));
|
||||
AtomicBoolean admitted = new AtomicBoolean();
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> service.execute(context, key, model, normalized,
|
||||
null, () -> { admitted.set(true); return mock(GatewayGuard.Admission.class); },
|
||||
ticket -> "unreachable", failure -> new ApiException("CALL_FAILED", "failed", 502),
|
||||
ApiException::getCode));
|
||||
|
||||
assertEquals(403, error.getStatus());
|
||||
assertFalse(admitted.get());
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shadowSignalDoesNotChangeRequestExecution() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
var service = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.shadow(List.of("test.observe"))));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
String result = service.execute(context, key, model, normalized, null, () -> admission,
|
||||
ignored -> "executed", failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode);
|
||||
|
||||
assertEquals("executed", result);
|
||||
verify(billing).reserve(key, model, normalized, null, context.metadata());
|
||||
verify(admission).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void enforceModeFailsClosedWhenRiskEngineThrowsBeforeAdmission() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var service = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ignored -> { throw new IllegalStateException("private detail"); }));
|
||||
AtomicBoolean admitted = new AtomicBoolean();
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> service.execute(context, key, model, normalized,
|
||||
null, () -> { admitted.set(true); return mock(GatewayGuard.Admission.class); },
|
||||
ticket -> "unreachable", failure -> new ApiException("CALL_FAILED", "failed", 502),
|
||||
ApiException::getCode));
|
||||
|
||||
assertEquals("RISK_ENGINE_UNAVAILABLE", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
assertFalse(admitted.get());
|
||||
assertFalse(error.getMessage().contains("private detail"));
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
@Test
|
||||
void admissionLeaseClosesWhenReservationFails() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenThrow(new ApiException("INSUFFICIENT_BALANCE", "low", 402));
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
assertEquals("INSUFFICIENT_BALANCE", assertThrows(ApiException.class, () -> service.execute(context, key, model,
|
||||
normalized, null, () -> admission, ticket -> "unreachable",
|
||||
failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode)).getCode());
|
||||
|
||||
verify(admission).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void workFailureIsRecordedAndAdmissionLeaseCloses() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
assertEquals("CALL_INTERRUPTED", service.execute(context, key, model, normalized, null, () -> admission,
|
||||
ignored -> { throw new IllegalStateException("failure"); },
|
||||
failure -> new ApiException("CALL_INTERRUPTED", "failed", 502), ApiException::getCode));
|
||||
|
||||
verify(billing).fail(eq(ticket), eq("CALL_INTERRUPTED"), eq("failed"), anyInt());
|
||||
verify(admission).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void asyncTaskRiskDenialPrecedesQueueAndWalletDatabaseWork() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
UsageAuditService usage = mock(UsageAuditService.class);
|
||||
var route = new java.util.LinkedHashMap<String,Object>();
|
||||
route.put("id", "async-route");
|
||||
route.put("providerId", "provider-mock");
|
||||
route.put("type", "3d");
|
||||
route.put("mode", "async");
|
||||
route.put("executionReady", true);
|
||||
var apiKey = Map.<String,Object>of("id", keyId.toString(), "userId", userId.toString());
|
||||
when(catalog.listModels(false)).thenReturn(List.<Map<String,Object>>of(route));
|
||||
when(catalog.activeModel("async-route")).thenReturn(route);
|
||||
when(catalog.keyCanInvoke(apiKey, "async-route")).thenReturn(true);
|
||||
when(catalog.keyOwnerId(keyId)).thenReturn(userId);
|
||||
var risk = new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.deny(
|
||||
new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY, "test.async-block", "RISK_BLOCKED", "Blocked", 0)));
|
||||
var tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, 4, 4, 3_600_000,
|
||||
false, guard, usage, null, null, risk);
|
||||
|
||||
ApiException error = assertThrows(ApiException.class,
|
||||
() -> tasks.createForKey(apiKey, "3d", Map.of("model", "async-route", "prompt", "generate"), ApiRequestMetadata.playground("127.0.0.1")));
|
||||
|
||||
assertEquals("RISK_BLOCKED", error.getCode());
|
||||
verify(guard).checkRate(keyId,false);
|
||||
verifyNoMoreInteractions(guard);
|
||||
verifyNoInteractions(jdbc, usage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicMetadataCannotBePromotedToPlaygroundByRequestHeaderAndContextStringOmitsIp() {
|
||||
var publicMetadata = ApiRequestMetadata.fromHttp("198.51.100.7");
|
||||
var playgroundMetadata = ApiRequestMetadata.playground("198.51.100.7");
|
||||
|
||||
assertEquals(ApiRequestMetadata.Source.API, publicMetadata.source());
|
||||
assertEquals(ApiRequestMetadata.Source.PLAYGROUND, playgroundMetadata.source());
|
||||
assertFalse(context.toString().contains("127.0.0.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void compositeRiskModeReportsSignalsInShadowAndAppliesThemOnlyInEnforce() {
|
||||
ApiRequestRiskRule rule = new ApiRequestRiskRule() {
|
||||
@Override public String id() { return "test.rule"; }
|
||||
@Override public java.util.Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext ignored) {
|
||||
return java.util.Optional.of(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||
id(), "RISK_BLOCKED", "Blocked", 0));
|
||||
}
|
||||
};
|
||||
|
||||
var shadow = new CompositeApiRequestRiskEngine(List.of(rule), CompositeApiRequestRiskEngine.Mode.SHADOW)
|
||||
.evaluate(context);
|
||||
var enforce = new CompositeApiRequestRiskEngine(List.of(rule), CompositeApiRequestRiskEngine.Mode.ENFORCE)
|
||||
.evaluate(context);
|
||||
|
||||
assertEquals(ApiRequestRiskDecision.Action.SHADOW, shadow.action());
|
||||
assertEquals(List.of("test.rule"), shadow.ruleIds());
|
||||
assertEquals(ApiRequestRiskDecision.Action.DENY, enforce.action());
|
||||
assertEquals("RISK_BLOCKED", enforce.code());
|
||||
}
|
||||
|
||||
@Test
|
||||
void settlementPersistenceFailureDoesNotMarkCompletedProviderWorkAsFailed() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
doThrow(new org.springframework.dao.DataIntegrityViolationException("ledger unavailable"))
|
||||
.when(billing).settle(eq(ticket), anyMap(), anyInt(), any());
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
String result = service.execute(context, key, model, normalized, null, () -> admission,
|
||||
current -> {
|
||||
service.settle(current, Map.of("usage", Map.of("prompt_tokens", 1, "completion_tokens", 1)), 2, null);
|
||||
return "unexpected";
|
||||
}, failure -> new ApiException("CALL_INTERRUPTED", "failed", 502), ApiException::getCode);
|
||||
|
||||
assertEquals("SETTLEMENT_PENDING", result);
|
||||
verify(billing, never()).fail(any(), anyString(), anyString(), anyInt());
|
||||
verify(admission).close();
|
||||
}
|
||||
}
|
||||
+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