返回 DeepSeek-Reasonix
auxiliary_recovery_test.go
根目录 / internal / provider / auxiliary_recovery_test.go
1 package provider
2
3 import (
4 "context"
5 "strings"
6 "testing"
7 "time"
8 )
9
10 type auxiliaryScript struct {
11 calls int
12 partial bool
13 }
14
15 func (*auxiliaryScript) Name() string { return "auxiliary-test" }
16 func (p *auxiliaryScript) Stream(context.Context, Request) (<-chan Chunk, error) {
17 p.calls++
18 if !p.partial && p.calls < 4 {
19 return nil, &APIError{Status: 503}
20 }
21 ch := make(chan Chunk, 3)
22 ch <- Chunk{Type: ChunkText, Text: "candidate"}
23 ch <- Chunk{Type: ChunkUsage, Usage: &Usage{PromptTokens: 10, CompletionTokens: 1, TotalTokens: 11}}
24 if !p.partial {
25 ch <- Chunk{Type: ChunkDone}
26 }
27 close(ch)
28 return ch, nil
29 }
30 func TestAuxiliaryRetriesShareFiniteBudgetAndDoNotLeakPartialText(t *testing.T) {
31 for _, partial := range []bool{false, true} {
32 p := &auxiliaryScript{partial: partial}
33 var waits []time.Duration
34 ctx := WithRecoverySleeper(context.Background(), func(_ context.Context, d time.Duration) bool { waits = append(waits, d); return true })
35 ch, err := StreamAuxiliary(ctx, p, Request{})
36 if err != nil {
37 t.Fatal(err)
38 }
39 var text strings.Builder
40 var usage *Usage
41 var streamErr error
42 for c := range ch {
43 switch c.Type {
44 case ChunkText:
45 text.WriteString(c.Text)
46 case ChunkUsage:
47 usage = c.Usage
48 case ChunkError:
49 streamErr = c.Err
50 }
51 }
52 if p.calls != 4 || len(waits) != 3 {
53 t.Fatalf("calls=%d waits=%v", p.calls, waits)
54 }
55 for i, want := range []time.Duration{2 * time.Second, 4 * time.Second, 8 * time.Second} {
56 if waits[i] != want {
57 t.Fatalf("waits=%v", waits)
58 }
59 }
60 if usage == nil || usage.RequestCount != 4 {
61 t.Fatalf("usage=%+v", usage)
62 }
63 if partial {
64 if text.Len() != 0 || streamErr == nil || usage.PromptTokens != 40 {
65 t.Fatalf("partial text=%q err=%v usage=%+v", text.String(), streamErr, usage)
66 }
67 } else if text.String() != "candidate" || streamErr != nil || !usage.Unknown || usage.PromptTokens != 10 {
68 t.Fatalf("text=%q err=%v usage=%+v", text.String(), streamErr, usage)
69 }
70 }
71 }
72
72 lines GO