Preserve operation timeout identity during HTTP generation
This commit is contained in:
@@ -3,6 +3,9 @@ package promptkit_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/synctest"
|
"testing/synctest"
|
||||||
"time"
|
"time"
|
||||||
@@ -10,6 +13,70 @@ import (
|
|||||||
"go.maximumdirect.net/promptkit"
|
"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) {
|
func TestRunOperationTimeoutAttribution(t *testing.T) {
|
||||||
synctest.Test(t, func(t *testing.T) {
|
synctest.Test(t, func(t *testing.T) {
|
||||||
engine := observationEngine(t, observationClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
engine := observationEngine(t, observationClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package usecase
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,7 +18,9 @@ type OperationScope struct {
|
|||||||
func NewOperationScope(parent context.Context, duration time.Duration) *OperationScope {
|
func NewOperationScope(parent context.Context, duration time.Duration) *OperationScope {
|
||||||
scope := &OperationScope{Context: parent, cancel: func() {}}
|
scope := &OperationScope{Context: parent, cancel: func() {}}
|
||||||
if duration > 0 {
|
if duration > 0 {
|
||||||
scope.cause = errors.New("operation deadline")
|
// Transports may return context.Cause rather than ctx.Err(). Retain
|
||||||
|
// deadline identity while keeping this cause unique to the invocation.
|
||||||
|
scope.cause = fmt.Errorf("operation deadline: %w", context.DeadlineExceeded)
|
||||||
scope.Context, scope.cancel = context.WithTimeoutCause(parent, duration, scope.cause)
|
scope.Context, scope.cancel = context.WithTimeoutCause(parent, duration, scope.cause)
|
||||||
}
|
}
|
||||||
return scope
|
return scope
|
||||||
|
|||||||
Reference in New Issue
Block a user