Files
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

99 lines
2.8 KiB
Go

package requestbudget
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
func TestGraphExecutionRefusesFourthSendAndScopeChanges(t *testing.T) {
for _, mismatch := range []string{"limit", "model", "phase"} {
t.Run(mismatch, func(t *testing.T) {
ctx, guard, cancel, err := BindGraphExecution(context.Background(), "spent-receipt", "model")
if err != nil {
t.Fatal(err)
}
defer cancel()
ctx = WithModel(ctx, "model")
if mismatch == "model" {
ctx = WithModel(ctx, "changed")
}
if mismatch == "phase" {
ctx = WithPhase(ctx, "changed")
}
sends := 0
transport := Transport{Base: roundTripFunc(func(*http.Request) (*http.Response, error) {
sends++
return nil, errors.New("provider failure")
})}
for i := 0; i < 4; i++ {
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "http://local.test", nil)
_, _ = transport.RoundTrip(req)
}
want := 0
if mismatch == "limit" {
want = 3
}
if sends != want || guard.Reservations() != want || !errors.Is(guard.Check(ctx), ErrStopped) {
t.Fatalf("sends=%d reserved=%d stop=%v", sends, guard.Reservations(), guard.Check(ctx))
}
})
}
}
func TestGraphExecutionCancelsBlockedHTTPAtInheritedDeadline(t *testing.T) {
var sends atomic.Int32
arrived := make(chan struct{})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sends.Add(1)
close(arrived)
<-r.Context().Done()
}))
defer server.Close()
parent, parentCancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer parentCancel()
ctx, guard, cancel, err := BindGraphExecution(parent, "spent-receipt", "model")
if err != nil {
t.Fatal(err)
}
defer cancel()
wantDeadline, _ := parent.Deadline()
if deadline, _ := ctx.Deadline(); !deadline.Equal(wantDeadline) {
t.Fatal("deadline was replenished")
}
req, _ := http.NewRequestWithContext(WithModel(ctx, "model"), http.MethodGet, server.URL, nil)
started := time.Now()
_, err = (&http.Client{Transport: Transport{}}).Do(req)
if !errors.Is(err, context.DeadlineExceeded) || time.Since(started) > 2*time.Second {
t.Fatalf("blocked send did not cancel at deadline: %v", err)
}
select {
case <-arrived:
default:
t.Fatal("request never reached local server")
}
if sends.Load() != 1 || guard.Reservations() != 1 {
t.Fatal("blocked send was not counted")
}
}
func TestGraphExecutionLastAllowedResponseRemainsUsable(t *testing.T) {
ctx, guard, cancel, err := BindGraphExecution(context.Background(), "spent-receipt", "model")
if err != nil {
t.Fatal(err)
}
defer cancel()
for i := 0; i < GraphMaxSends; i++ {
if err := guard.Reserve(ctx, "model", GraphPhase); err != nil {
t.Fatal(err)
}
}
if err := guard.Check(ctx); err != nil {
t.Fatalf("third valid response rejected: %v", err)
}
}