2026-11-14 17:37:25 +00:00
|
|
|
package promptkit_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
2026-11-19 13:44:42 +00:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"net/url"
|
2026-11-14 17:37:25 +00:00
|
|
|
"testing"
|
|
|
|
|
"testing/synctest"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"go.maximumdirect.net/promptkit"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-19 13:44:42 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 17:37:25 +00:00
|
|
|
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) {
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
return nil, ctx.Err()
|
|
|
|
|
}))
|
|
|
|
|
var terminal promptkit.ExecutionEvent
|
|
|
|
|
_, err := engine.Run(context.Background(), promptkit.RunRequest{PromptID: "prompt", OperationTimeout: time.Second,
|
|
|
|
|
Observer: func(event promptkit.ExecutionEvent) {
|
|
|
|
|
if event.Kind == promptkit.ExecutionFinished {
|
|
|
|
|
terminal = event
|
|
|
|
|
}
|
|
|
|
|
}})
|
|
|
|
|
if !errors.Is(err, promptkit.ErrOperationTimeout) || !errors.Is(err, context.DeadlineExceeded) || !errors.Is(err, promptkit.ErrLLMGenerate) || terminal.Failure != promptkit.ExecutionFailureDeadline {
|
|
|
|
|
t.Fatalf("error=%v terminal=%+v", err, terminal)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRunProviderDeadlineIsNotOperationTimeout(t *testing.T) {
|
|
|
|
|
engine := observationEngine(t, observationClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
|
|
|
return nil, context.DeadlineExceeded
|
|
|
|
|
}))
|
|
|
|
|
_, err := engine.Run(context.Background(), promptkit.RunRequest{PromptID: "prompt", OperationTimeout: time.Minute})
|
|
|
|
|
if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, promptkit.ErrOperationTimeout) {
|
|
|
|
|
t.Fatalf("provider deadline attribution: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-14 17:38:09 +00:00
|
|
|
|
|
|
|
|
func TestPreparedExecutionRetainsTimeoutWithoutIdleTimer(t *testing.T) {
|
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
|
|
|
engine := observationEngine(t, observationClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
return nil, ctx.Err()
|
|
|
|
|
}))
|
|
|
|
|
req := promptkit.RunRequest{PromptID: "prompt", OperationTimeout: time.Second}
|
|
|
|
|
handle, err := engine.PrepareExecution(context.Background(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
req.OperationTimeout = 0
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
start := time.Now()
|
|
|
|
|
_, err = engine.RunPrepared(context.Background(), handle)
|
|
|
|
|
if time.Since(start) != time.Second || !errors.Is(err, promptkit.ErrOperationTimeout) || !errors.Is(err, context.DeadlineExceeded) || !errors.Is(err, promptkit.ErrLLMGenerate) {
|
|
|
|
|
t.Fatalf("elapsed=%v error=%v", time.Since(start), err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPreparedExecutionRespectsCallerDeadline(t *testing.T) {
|
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
|
|
|
engine := observationEngine(t, observationClientFunc(func(ctx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
return nil, ctx.Err()
|
|
|
|
|
}))
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
handle, err := engine.PrepareExecution(ctx, promptkit.RunRequest{PromptID: "prompt", OperationTimeout: 5 * time.Second})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
_, err = engine.RunPrepared(ctx, handle)
|
|
|
|
|
if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, promptkit.ErrOperationTimeout) {
|
|
|
|
|
t.Fatalf("caller deadline attribution: %v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-11-14 17:47:55 +00:00
|
|
|
|
|
|
|
|
func TestPreparedExecutionDoesNotTimePreparation(t *testing.T) {
|
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
|
|
|
release := make(chan struct{})
|
|
|
|
|
reader := &capacityArtifactReader{entered: make(chan struct{}, 1), release: release}
|
|
|
|
|
engine := newBackendCapacityEngine(t, newCapacityGateClient(closedCapacityChannel(), 1), 1, capacityInt(0), reader)
|
|
|
|
|
req := capacityInputRequest("http://example.invalid/v1")
|
|
|
|
|
req.OperationTimeout = time.Second
|
|
|
|
|
go func() { time.Sleep(2 * time.Second); close(release) }()
|
|
|
|
|
start := time.Now()
|
|
|
|
|
handle, err := engine.PrepareExecution(context.Background(), req)
|
|
|
|
|
if err != nil || time.Since(start) != 2*time.Second {
|
|
|
|
|
t.Fatalf("prepare elapsed=%v error=%v", time.Since(start), err)
|
|
|
|
|
}
|
|
|
|
|
if _, err = engine.RunPrepared(context.Background(), handle); err != nil {
|
|
|
|
|
t.Fatalf("execution after preparation: %v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|