AI Gateway Go 0.10.0 源码快照 + 旗舰版需求规划报告
M0-M7 已完成:核心网关(身份/RBAC/TOTP/OIDC/SAML/Provider/配额/路由/内容策略/审计/定价)+ 资源市场(MCP/Skills/数字员工)。 含 22 个 PostgreSQL 迁移、管理端/门户端前端源码、OpenAPI 契约、部署 compose。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripFunc) RoundTrip(request *http.Request) (*http.Response, error) { return f(request) }
|
||||
|
||||
func TestResilientTransportRetriesReplayableRequest(t *testing.T) {
|
||||
attempts := 0
|
||||
transport := &resilientTransport{base: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
||||
attempts++
|
||||
if attempts < 3 {
|
||||
return &http.Response{StatusCode: http.StatusServiceUnavailable, Body: io.NopCloser(strings.NewReader("busy")), Header: make(http.Header)}, nil
|
||||
}
|
||||
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("ok")), Header: make(http.Header)}, nil
|
||||
}), circuit: newCircuitBreaker(), maxRetries: 2}
|
||||
request, _ := http.NewRequest(http.MethodGet, "https://provider.example/v1/models", nil)
|
||||
response, err := transport.RoundTrip(request)
|
||||
if err != nil || response.StatusCode != http.StatusOK || attempts != 3 {
|
||||
t.Fatalf("response=%v err=%v attempts=%d", response, err, attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResilientTransportDoesNotRetryUnsafePost(t *testing.T) {
|
||||
attempts := 0
|
||||
transport := &resilientTransport{base: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
||||
attempts++
|
||||
return nil, errors.New("network failure")
|
||||
}), circuit: newCircuitBreaker(), maxRetries: 2}
|
||||
request, _ := http.NewRequest(http.MethodPost, "https://provider.example/v1/chat/completions", strings.NewReader("{}"))
|
||||
_, _ = transport.RoundTrip(request)
|
||||
if attempts != 1 {
|
||||
t.Fatalf("unsafe POST attempted %d times", attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircuitOpensAndAllowsSingleProbe(t *testing.T) {
|
||||
circuit := newCircuitBreaker()
|
||||
circuit.threshold = 2
|
||||
circuit.openFor = time.Millisecond
|
||||
now := time.Now()
|
||||
circuit.failure(now)
|
||||
circuit.failure(now)
|
||||
if circuit.allow(now) {
|
||||
t.Fatal("open circuit allowed request")
|
||||
}
|
||||
if !circuit.allow(now.Add(2 * time.Millisecond)) {
|
||||
t.Fatal("circuit did not allow half-open probe")
|
||||
}
|
||||
if circuit.allow(now.Add(2 * time.Millisecond)) {
|
||||
t.Fatal("circuit allowed concurrent half-open probe")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user