Preserve operation timeout identity during HTTP generation
This commit is contained in:
@@ -3,6 +3,9 @@ package promptkit_test
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
@@ -10,6 +13,70 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestBuiltInGenerationOperationTimeout(t *testing.T) {
|
||||
for _, split := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "run", true: "prepared"}[split], func(t *testing.T) {
|
||||
started := make(chan struct{}, 1)
|
||||
release := make(chan struct{})
|
||||
provider := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||
select {
|
||||
case started <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-release
|
||||
}))
|
||||
defer provider.Close()
|
||||
defer close(release)
|
||||
|
||||
config := simpleConfig("message")
|
||||
zero := 0
|
||||
config.TransientRetries = &zero
|
||||
config.HTTPClient = provider.Client()
|
||||
engine, err := promptkit.NewEngine(config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var terminal promptkit.ExecutionEvent
|
||||
req := promptkit.RunRequest{PromptID: "prompt", OperationTimeout: time.Second,
|
||||
Execution: &promptkit.ExecutionTargetOverride{Endpoint: provider.URL + "/v1", TimeoutSeconds: &zero},
|
||||
Observer: func(event promptkit.ExecutionEvent) {
|
||||
if event.Kind == promptkit.ExecutionFinished {
|
||||
terminal = event
|
||||
}
|
||||
},
|
||||
}
|
||||
parent := context.Background()
|
||||
var result *promptkit.RunResult
|
||||
if split {
|
||||
handle, prepareErr := engine.PrepareExecution(parent, req)
|
||||
if prepareErr != nil {
|
||||
t.Fatal(prepareErr)
|
||||
}
|
||||
result, err = engine.RunPrepared(parent, handle)
|
||||
} else {
|
||||
result, err = engine.Run(parent, req)
|
||||
}
|
||||
select {
|
||||
case <-started:
|
||||
default:
|
||||
t.Fatal("operation expired before provider received the request")
|
||||
}
|
||||
for _, target := range []error{promptkit.ErrOperationTimeout, context.DeadlineExceeded, promptkit.ErrLLMGenerate, promptkit.ErrTransport} {
|
||||
if !errors.Is(err, target) {
|
||||
t.Errorf("error=%v, want identity %v", err, target)
|
||||
}
|
||||
}
|
||||
var transportErr *url.Error
|
||||
if !errors.As(err, &transportErr) {
|
||||
t.Errorf("error=%v, want retained HTTP transport cause", err)
|
||||
}
|
||||
if result != nil || parent.Err() != nil || terminal.Failure != promptkit.ExecutionFailureDeadline {
|
||||
t.Fatalf("result=%v parent=%v terminal=%+v", result, parent.Err(), terminal)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunOperationTimeoutAttribution(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
engine := observationEngine(t, observationClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user