| 1 | package anthropic |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "reasonix/internal/provider" |
| 13 | ) |
| 14 | |
| 15 | // TestStreamStallTimesOut covers issue #3374 for the Anthropic provider: a |
| 16 | // half-open connection sends the SSE head then goes silent without an RST, which |
| 17 | // would hang scanner.Scan() forever. The idle watchdog must surface a stall error. |
| 18 | func TestStreamStallTimesOut(t *testing.T) { |
| 19 | release := make(chan struct{}) |
| 20 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 21 | w.Header().Set("Content-Type", "text/event-stream") |
| 22 | w.WriteHeader(http.StatusOK) |
| 23 | if f, ok := w.(http.Flusher); ok { |
| 24 | f.Flush() |
| 25 | } |
| 26 | _, _ = io.WriteString(w, ": ping\n\n") |
| 27 | if f, ok := w.(http.Flusher); ok { |
| 28 | f.Flush() |
| 29 | } |
| 30 | <-release // stall: never send data, never close |
| 31 | })) |
| 32 | defer srv.Close() |
| 33 | defer close(release) |
| 34 | |
| 35 | p, err := New(provider.Config{Name: "claude", BaseURL: srv.URL, Model: "claude-opus-4-8", APIKey: "k"}) |
| 36 | if err != nil { |
| 37 | t.Fatalf("New: %v", err) |
| 38 | } |
| 39 | p.(*client).idleTimeout = 150 * time.Millisecond |
| 40 | ch, err := p.Stream(context.Background(), provider.Request{ |
| 41 | Messages: []provider.Message{{Role: provider.RoleUser, Content: "hi"}}, |
| 42 | MaxTokens: 16, |
| 43 | }) |
| 44 | if err != nil { |
| 45 | t.Fatalf("Stream: %v", err) |
| 46 | } |
| 47 | |
| 48 | deadline := time.After(5 * time.Second) |
| 49 | for { |
| 50 | select { |
| 51 | case chunk, ok := <-ch: |
| 52 | if !ok { |
| 53 | t.Fatal("stream closed without surfacing a stall error") |
| 54 | } |
| 55 | if chunk.Type == provider.ChunkError { |
| 56 | if !strings.Contains(chunk.Err.Error(), "stalled") { |
| 57 | t.Fatalf("error = %v, want a 'stalled' error", chunk.Err) |
| 58 | } |
| 59 | return |
| 60 | } |
| 61 | case <-deadline: |
| 62 | t.Fatal("stream did not time out on a stalled connection — it hung") |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestReadStreamSendUnblocksOnContextCancel(t *testing.T) { |
| 68 | ctx, cancel := context.WithCancel(context.Background()) |
| 69 | resp := &http.Response{Body: io.NopCloser(strings.NewReader("data: {\"type\":\"content_block_delta\",\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n"))} |
| 70 | out := make(chan provider.Chunk) |
| 71 | done := make(chan struct{}) |
| 72 | |
| 73 | go func() { |
| 74 | (&client{name: "anthropic"}).readStream(ctx, resp, out) |
| 75 | close(done) |
| 76 | }() |
| 77 | |
| 78 | time.Sleep(50 * time.Millisecond) |
| 79 | cancel() |
| 80 | |
| 81 | select { |
| 82 | case <-done: |
| 83 | case <-time.After(500 * time.Millisecond): |
| 84 | t.Fatal("readStream remained blocked sending to an abandoned reader after context cancellation") |
| 85 | } |
| 86 | } |
| 87 |