Preserve authentication cancellation at the response limit
This commit is contained in:
@@ -3,6 +3,7 @@ package promptkit_test
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
@@ -13,6 +14,45 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type cancelingAuthenticationBody struct {
|
||||
remaining int
|
||||
cancel context.CancelFunc
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (b *cancelingAuthenticationBody) Read(p []byte) (int, error) {
|
||||
n := min(len(p), b.remaining)
|
||||
for i := range p[:n] {
|
||||
p[i] = 'x'
|
||||
}
|
||||
b.remaining -= n
|
||||
if b.remaining == 0 {
|
||||
b.cancel()
|
||||
return n, context.Canceled
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
func (b *cancelingAuthenticationBody) Close() error { b.closed = true; return nil }
|
||||
|
||||
func TestAuthenticationReadCancellationWinsOverOverflow(t *testing.T) {
|
||||
for _, size := range []int{1024, (1 << 20) + 1} {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
body := &cancelingAuthenticationBody{remaining: size, cancel: cancel}
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{HTTPClient: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: body}, nil
|
||||
})}})
|
||||
if err != nil {
|
||||
cancel()
|
||||
t.Fatal(err)
|
||||
}
|
||||
report, err := engine.SmokeTest(ctx, promptkit.SmokeTestRequest{Kind: promptkit.SmokeTestAuthentication, BackendID: "openrouter"})
|
||||
cancel()
|
||||
if !errors.Is(err, context.Canceled) || report == nil || report.Completed || report.Failure != promptkit.ExecutionFailureCanceled || report.Facts.CredentialsAccepted != nil || report.Facts.ResponseReceived == nil || !*report.Facts.ResponseReceived || !body.closed {
|
||||
t.Fatalf("size %d: report=%+v error=%v closed=%v", size, report, err, body.closed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBundledSmokeHTTPDefaults(t *testing.T) {
|
||||
t.Setenv("OPENROUTER_API_KEY", "synthetic-key")
|
||||
var urls []string
|
||||
|
||||
Reference in New Issue
Block a user