fix: 对齐规格解析跨端超时预算 (#257)
This commit is contained in:
@@ -99,6 +99,23 @@ type stubAIModelClient struct {
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type runtimeDeadlineAIClient struct {
|
||||
deadline time.Time
|
||||
deadlineOK bool
|
||||
contextErr error
|
||||
}
|
||||
|
||||
func (client *runtimeDeadlineAIClient) Match(ctx context.Context, _ model.AIProviderConfig, _ string,
|
||||
_ AIModelMatchRequest) (AIModelMatchResponse, error) {
|
||||
client.deadline, client.deadlineOK = ctx.Deadline()
|
||||
client.contextErr = ctx.Err()
|
||||
if client.contextErr != nil {
|
||||
return AIModelMatchResponse{}, client.contextErr
|
||||
}
|
||||
return AIModelMatchResponse{Conclusion: "uncertain", ConfidenceBPS: 7000,
|
||||
Reason: "测试信息不足"}, nil
|
||||
}
|
||||
|
||||
func (client *stubAIModelClient) Match(ctx context.Context, _ model.AIProviderConfig, _ string,
|
||||
request AIModelMatchRequest) (AIModelMatchResponse, error) {
|
||||
client.calls.Add(1)
|
||||
@@ -115,6 +132,46 @@ func (client *stubAIModelClient) Match(ctx context.Context, _ model.AIProviderCo
|
||||
return client.response, client.err
|
||||
}
|
||||
|
||||
func TestMatchPurchaseSpecWithRuntimeAI_限制30秒并继承更早取消(t *testing.T) {
|
||||
req, _, _ := validRuntimeSpecRequest(t, "cg257-timeout", "L码", "M码", "XL码")
|
||||
client := &runtimeDeadlineAIClient{}
|
||||
loader := func(context.Context, *sql.DB, AISecretStore, AIEndpointPolicy) (AIMatchSnapshot, error) {
|
||||
return AIMatchSnapshot{Provider: model.AIProviderConfig{
|
||||
ProviderID: "provider-timeout", Model: "model-timeout", TimeoutSeconds: 120,
|
||||
ConfidenceThresholdBPS: 9000,
|
||||
}, ConfigFingerprint: "fingerprint", Secret: "not-a-real-secret", Client: client}, nil
|
||||
}
|
||||
startedAt := time.Now()
|
||||
decision := matchPurchaseSpecWithRuntimeAI(context.Background(), nil, nil,
|
||||
AIEndpointPolicy{}, req, loader)
|
||||
if decision.Outcome != model.PurchaseSpecResolutionUncertain {
|
||||
t.Fatalf("outcome=%s reason=%s", decision.Outcome, decision.Reason)
|
||||
}
|
||||
if !client.deadlineOK {
|
||||
t.Fatal("运行时 AI 上下文缺少截止时间")
|
||||
}
|
||||
budget := client.deadline.Sub(startedAt)
|
||||
if budget < 29*time.Second || budget > purchaseSpecResolutionAITimeout {
|
||||
t.Fatalf("运行时 AI 时间预算=%s,期望不超过 %s", budget, purchaseSpecResolutionAITimeout)
|
||||
}
|
||||
|
||||
cancelledContext, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
cancelledClient := &runtimeDeadlineAIClient{}
|
||||
cancelledLoader := func(context.Context, *sql.DB, AISecretStore, AIEndpointPolicy) (AIMatchSnapshot, error) {
|
||||
return AIMatchSnapshot{Provider: model.AIProviderConfig{
|
||||
ProviderID: "provider-timeout", Model: "model-timeout", TimeoutSeconds: 120,
|
||||
ConfidenceThresholdBPS: 9000,
|
||||
}, ConfigFingerprint: "fingerprint", Secret: "not-a-real-secret", Client: cancelledClient}, nil
|
||||
}
|
||||
decision = matchPurchaseSpecWithRuntimeAI(cancelledContext, nil, nil,
|
||||
AIEndpointPolicy{}, req, cancelledLoader)
|
||||
if decision.Outcome != model.PurchaseSpecResolutionFailed ||
|
||||
!errors.Is(cancelledClient.contextErr, context.Canceled) {
|
||||
t.Fatalf("更早取消未继承: outcome=%s context_err=%v", decision.Outcome, cancelledClient.contextErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchPurchaseSpecWithAI_只接受请求候选并执行安全门禁(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user