51 lines
1.9 KiB
Go
51 lines
1.9 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestTransportDoesNotRelabelIndependentFailureAfterCancellation(t *testing.T) {
|
|
ctx, cancel := context.WithCancelCause(context.Background())
|
|
defer cancel(nil)
|
|
independent := errors.New("independent transport failure")
|
|
client := NewOpenAICompatibleClient(OpenAICompatibleConfig{HTTPClient: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
|
cancel(errors.New("unrelated caller cause"))
|
|
return nil, independent
|
|
})}})
|
|
_, err := client.Generate(ctx, contextIdentityRequest())
|
|
if !errors.Is(err, independent) || errors.Is(err, context.Canceled) {
|
|
t.Fatalf("unrelated failure relabeled: %v", err)
|
|
}
|
|
}
|
|
|
|
type contextIdentityBody struct {
|
|
cancel context.CancelFunc
|
|
cause error
|
|
}
|
|
|
|
func (b contextIdentityBody) Read([]byte) (int, error) { b.cancel(); return 0, b.cause }
|
|
func (contextIdentityBody) Close() error { return nil }
|
|
|
|
func TestResponseReadCancellationRetainsOriginalCause(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
cause := errors.New("private body failure")
|
|
client := NewOpenAICompatibleClient(OpenAICompatibleConfig{HTTPClient: &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
|
return &http.Response{StatusCode: http.StatusOK, Body: contextIdentityBody{cancel: cancel, cause: cause}}, nil
|
|
})}})
|
|
_, err := client.Generate(ctx, contextIdentityRequest())
|
|
if !errors.Is(err, context.Canceled) || !errors.Is(err, cause) || !errors.Is(err, ErrTransport) || strings.Contains(err.Error(), cause.Error()) {
|
|
t.Fatalf("lost body cause or redaction: %v", err)
|
|
}
|
|
}
|
|
|
|
func contextIdentityRequest() domain.GenerateRequest {
|
|
return domain.GenerateRequest{Target: domain.ExecutionTarget{Endpoint: "http://example.test/v1", Model: "test"}}
|
|
}
|