39 lines
1 KiB
Go
39 lines
1 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Cancelling the context must abort the in-flight provider HTTP request.
|
|
func TestOpenAICancelAbortsRequest(t *testing.T) {
|
|
aborted := make(chan struct{})
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"content\":\"Hal\"}}]}\n\n")
|
|
w.(http.Flusher).Flush()
|
|
<-r.Context().Done()
|
|
close(aborted)
|
|
}))
|
|
defer srv.Close()
|
|
p := NewOpenAI(srv.URL, "k", "m", 0, 30*time.Second)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
got := ""
|
|
_, err := p.Chat(ctx, Request{Messages: []Message{{Role: RoleUser, Content: "x"}}}, func(s string) {
|
|
got += s
|
|
cancel()
|
|
})
|
|
if err == nil || !errors.Is(ctx.Err(), context.Canceled) || got != "Hal" {
|
|
t.Fatalf("err=%v got=%q", err, got)
|
|
}
|
|
select {
|
|
case <-aborted:
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("provider request was not aborted")
|
|
}
|
|
}
|