返回 DeepSeek-Reasonix
protocol_compatibility_scope_test.go
根目录 / internal / provider / protocol_compatibility_scope_test.go
1 package provider
2
3 import (
4 "context"
5 "errors"
6 "io"
7 "net/http"
8 "net/http/httptest"
9 "net/http/httptrace"
10 "net/url"
11 "sync"
12 "sync/atomic"
13 "testing"
14 "time"
15 )
16
17 func TestProtocolCompatibilityPreservesSiblingAndProviderScope(t *testing.T) {
18 release := make(chan struct{})
19 server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20 if r.Header.Get("X-Hold") != "" {
21 w.(http.Flusher).Flush()
22 <-release
23 _, _ = io.WriteString(w, "sibling complete")
24 return
25 }
26 if r.ProtoMajor == 2 {
27 panic(http.ErrAbortHandler)
28 }
29 _, _ = io.WriteString(w, "ok")
30 }))
31 server.EnableHTTP2 = true
32 server.StartTLS()
33 defer server.Close()
34 var once sync.Once
35 defer once.Do(func() { close(release) })
36 c := compatibilityTestClient(t, server)
37 ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
38 defer cancel()
39 sibling, err := SendOnce(ctx, c, SendOptions{}, func(ctx context.Context) (*http.Request, error) {
40 r, _ := http.NewRequestWithContext(ctx, http.MethodPost, server.URL, nil)
41 r.Header.Set("X-Hold", "true")
42 return r, nil
43 })
44 if err != nil || sibling.ProtoMajor != 2 {
45 t.Fatalf("sibling=%v err=%v", sibling, err)
46 }
47 defer sibling.Body.Close()
48 var reused bool
49 traceCtx := httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{GotConn: func(i httptrace.GotConnInfo) { reused = i.Reused }})
50 _, err = compatibilityPost(traceCtx, c, server.URL)
51 if HTTP2TransportCode(err) != "INTERNAL_ERROR" || !reused {
52 t.Fatalf("failure did not share sibling connection: reused=%t err=%v", reused, err)
53 }
54 _, err = compatibilityPost(ctx, c, server.URL)
55 if HTTP2TransportCode(err) != "INTERNAL_ERROR" {
56 t.Fatalf("one INTERNAL_ERROR switched protocol: %v", err)
57 }
58 resp, err := compatibilityPost(ctx, c, server.URL)
59 if err != nil || resp.ProtoMajor != 1 {
60 t.Fatalf("compatibility failed: %v", err)
61 }
62 resp.Body.Close()
63 // Reconstructed clients share hints, but another configured provider does not.
64 other := WithProtocolCompatibility(server.Client(), RequestIdentity{Provider: "other", Protocol: "openai"})
65 other.Transport.(*protocolTransport).hints = c.Transport.(*protocolTransport).hints
66 defer other.CloseIdleConnections()
67 _, err = compatibilityPost(ctx, other, server.URL)
68 if HTTP2TransportCode(err) != "INTERNAL_ERROR" {
69 t.Fatalf("compatibility leaked across providers: %v", err)
70 }
71 rebuilt := WithProtocolCompatibility(server.Client(), RequestIdentity{Provider: t.Name(), Protocol: "openai"})
72 rebuilt.Transport.(*protocolTransport).hints = c.Transport.(*protocolTransport).hints
73 defer rebuilt.CloseIdleConnections()
74 resp, err = compatibilityPost(ctx, rebuilt, server.URL)
75 if err != nil || resp.ProtoMajor != 1 {
76 t.Fatalf("provider reconstruction lost hint: %v", err)
77 }
78 resp.Body.Close()
79 once.Do(func() { close(release) })
80 b, err := io.ReadAll(sibling.Body)
81 if err != nil || string(b) != "sibling complete" {
82 t.Fatalf("fallback interrupted multiplexed sibling: %s %v", b, err)
83 }
84 }
85
86 func TestProtocolCompatibilityProxyChangesDoNotInheritHints(t *testing.T) {
87 // Even a failed route must be keyed by the actual selected proxy, not a
88 // stale config label. This resolver alternates direct and a dead proxy.
89 server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
90 panic(http.ErrAbortHandler)
91 }))
92 server.EnableHTTP2 = true
93 server.StartTLS()
94 defer server.Close()
95 c := compatibilityTestClient(t, server)
96 primeCompatibility(t, c, server.URL)
97 tr := c.Transport.(*protocolTransport)
98 dead, _ := url.Parse("http://127.0.0.1:1")
99 tr.proxy = func(*http.Request) (*url.URL, error) { return dead, nil }
100 var mu sync.Mutex
101 var reason string
102 ctx, cancel := context.WithTimeout(t.Context(), time.Second)
103 defer cancel()
104 ctx = WithRequestObserver(ctx, func(o RequestObservation) { mu.Lock(); reason = o.ProtocolFallbackReason; mu.Unlock() })
105 _, err := compatibilityPost(ctx, c, server.URL)
106 if err == nil {
107 t.Fatal("expected dead proxy failure")
108 }
109 mu.Lock()
110 defer mu.Unlock()
111 if reason != "" {
112 t.Fatal("direct connection's hint leaked into proxy path")
113 }
114 }
115
116 func TestProtocolCompatibilityCancellationDoesNotPoisonHint(t *testing.T) {
117 hints := freshProtocolHints()
118 key := [32]byte{}
119 now := time.Now()
120 hints.failedHTTP2(key, "PROTOCOL_ERROR", hints.begin(), now)
121 h := hints.selectHTTP1(key, now)
122 ctx, cancel := context.WithCancel(t.Context())
123 cancel()
124 tr := &protocolTransport{hints: hints}
125 tr.failed(ctx, key, h, hints.begin(), ctx.Err())
126 if hints.selectHTTP1(key, now) == nil {
127 t.Fatal("caller cancellation disabled compatibility")
128 }
129 // Cancellation without HTTP/2 evidence must not install a new hint.
130 tr.failed(ctx, [32]byte{1}, nil, hints.begin(), ctx.Err())
131 if len(hints.entries) != 1 {
132 t.Fatal("caller cancellation created a hint")
133 }
134 }
135
136 func TestProtocolCompatibilityUserCancelIsNotRetried(t *testing.T) {
137 var calls atomic.Int32
138 arrived, release := make(chan struct{}), make(chan struct{})
139 s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
140 n := calls.Add(1)
141 if r.ProtoMajor == 2 {
142 panic(http.ErrAbortHandler)
143 }
144 if n == 3 {
145 close(arrived)
146 <-release
147 }
148 _, _ = io.WriteString(w, "ok")
149 }))
150 s.EnableHTTP2 = true
151 s.StartTLS()
152 defer s.Close()
153 defer close(release)
154 c := compatibilityTestClient(t, s)
155 primeCompatibility(t, c, s.URL)
156 ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
157 defer cancel()
158 ctx = WithRequestAttemptCounter(ctx)
159 go func() {
160 select {
161 case <-arrived:
162 cancel()
163 case <-ctx.Done():
164 }
165 }()
166 _, err := compatibilityPost(ctx, c, s.URL)
167 if !errors.Is(err, context.Canceled) || RequestAttemptCount(ctx) != 1 || calls.Load() != 3 {
168 t.Fatalf("cancel was retried or misclassified: calls=%d err=%v", calls.Load(), err)
169 }
170 resp, err := compatibilityPost(t.Context(), c, s.URL)
171 if err != nil || resp.ProtoMajor != 1 || calls.Load() != 4 {
172 t.Fatalf("cancellation poisoned hint: calls=%d err=%v", calls.Load(), err)
173 }
174 resp.Body.Close()
175 }
176
176 lines GO