163 lines
5.6 KiB
Go
163 lines
5.6 KiB
Go
// Headless 测试专用 worker 适配:固定生成输出,但准入、认领、取消、事件和 canonical 落库仍走生产实现。
|
|
package platform
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"net"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"gitea.entroia.com/Zayden-Jung/Bedrock/internal/codex"
|
|
)
|
|
|
|
// Only the generation transport is deterministic. Admission, claims, events,
|
|
// canonical writes and action receipts use the real service and isolated DB.
|
|
// headlessWorkerRoutes 搭建 Unix 假 worker 与私有控制路由,返回给端到端客户端;不是生产服务注册入口。
|
|
func headlessWorkerRoutes(t *testing.T, db *sql.DB, callbacks map[string]string, project, account string) http.Handler {
|
|
t.Helper()
|
|
// gate 同步确认生成已进入并控制释放,sync.Once 防止并发控制重复 close 通道。
|
|
type gate struct {
|
|
entered, release chan struct{}
|
|
started, released sync.Once
|
|
}
|
|
var mu sync.Mutex
|
|
var held *gate
|
|
var executions atomic.Int32
|
|
var picture bytes.Buffer
|
|
pixels := image.NewRGBA(image.Rect(0, 0, 3, 2))
|
|
pixels.Set(0, 0, color.RGBA{B: 255, A: 255})
|
|
if err := png.Encode(&picture, pixels); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
socket := filepath.Join(t.TempDir(), "worker.sock")
|
|
listener, err := net.Listen("unix", socket)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
worker := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/canvas/models" {
|
|
reply(w, 200, response{"models": []any{
|
|
codex.CanvasModel{ID: "fixture-text", Label: "Fixture text", Available: true, InputModalities: []string{"text", "image"}, OutputModalities: []string{"text"}, MaxInputBytes: 1 << 20, MaxExecutionMS: 90000},
|
|
codex.CanvasModel{ID: "fixture-image", Label: "Fixture image", Available: true, InputModalities: []string{"text", "image"}, OutputModalities: []string{"image"}, MaxInputBytes: 1 << 20, MaxExecutionMS: 90000},
|
|
}})
|
|
return
|
|
}
|
|
if r.URL.Path != "/canvas/exec" {
|
|
reject(w, 404, "not_found")
|
|
return
|
|
}
|
|
var task codex.CanvasTask
|
|
if json.NewDecoder(r.Body).Decode(&task) != nil {
|
|
reject(w, 400, "invalid_request")
|
|
return
|
|
}
|
|
executions.Add(1)
|
|
mu.Lock()
|
|
current := held
|
|
mu.Unlock()
|
|
if current != nil {
|
|
current.started.Do(func() { close(current.entered) })
|
|
select {
|
|
case <-current.release:
|
|
case <-r.Context().Done():
|
|
return
|
|
}
|
|
mu.Lock()
|
|
if held == current {
|
|
held = nil
|
|
}
|
|
mu.Unlock()
|
|
}
|
|
if task.OutputModality == "image" {
|
|
reply(w, 200, response{"output": codex.CanvasOutput{Kind: "image", Image: &codex.CanvasImage{MIME: "image/png", Data: picture.Bytes()}}})
|
|
} else {
|
|
reply(w, 200, response{"result": "headless fixture output"})
|
|
}
|
|
})}
|
|
// 清理先释放正在阻塞的生成再关闭 server,避免用例失败后泄漏等待 goroutine。
|
|
go worker.Serve(listener)
|
|
t.Cleanup(func() {
|
|
mu.Lock()
|
|
if held != nil {
|
|
held.released.Do(func() { close(held.release) })
|
|
}
|
|
mu.Unlock()
|
|
worker.Close()
|
|
})
|
|
// 不启动周期队列,测试通过 execute 控制认领时刻,以稳定覆盖运行中目标移动/取消等竞态。
|
|
s := &service{db: db, origin: "https://api.example.com", canvasAI: codex.NewCanvasClient(socket)}
|
|
mux := http.NewServeMux()
|
|
prefix := "/projects/{project}/workspaces/{workspace}/materials/{material}/canvas/ai"
|
|
mux.HandleFunc("GET "+prefix+"/models", s.protected(callbacks, s.canvasAIModels))
|
|
mux.HandleFunc("POST "+prefix+"/runs", s.protected(callbacks, s.createCanvasAIRun))
|
|
mux.HandleFunc("GET "+prefix+"/runs/{run}", s.protected(callbacks, s.getCanvasAIRun))
|
|
mux.HandleFunc("GET "+prefix+"/operations/{request}", s.protected(callbacks, s.getCanvasAIAdmission))
|
|
mux.HandleFunc("POST "+prefix+"/runs/{run}/cancel", s.protected(callbacks, s.cancelCanvasAIRun))
|
|
mux.HandleFunc("POST "+prefix+"/runs/{run}/apply", s.protected(callbacks, s.applyCanvasAIOutput))
|
|
// Private test controls are not registered by production Register().
|
|
mux.HandleFunc("POST "+prefix+"/fixture/{action}", s.protected(callbacks, func(w http.ResponseWriter, r *http.Request, p, a string) error {
|
|
if p != project || a != account {
|
|
reject(w, 403, "forbidden")
|
|
return nil
|
|
}
|
|
// arm/wait/release 管理传输门闩,execute 驱动一次真实队列认领,stats 暴露调用计数。
|
|
switch r.PathValue("action") {
|
|
case "arm":
|
|
mu.Lock()
|
|
held = &gate{entered: make(chan struct{}), release: make(chan struct{})}
|
|
mu.Unlock()
|
|
case "wait":
|
|
mu.Lock()
|
|
current := held
|
|
mu.Unlock()
|
|
if current == nil {
|
|
reject(w, 409, "worker_not_armed")
|
|
return nil
|
|
}
|
|
select {
|
|
case <-current.entered:
|
|
case <-r.Context().Done():
|
|
return nil
|
|
}
|
|
case "release":
|
|
mu.Lock()
|
|
if held != nil {
|
|
held.released.Do(func() { close(held.release) })
|
|
}
|
|
mu.Unlock()
|
|
case "execute":
|
|
run, err := s.claimCanvasRun(r.Context())
|
|
if err != nil || run == nil || run.Project != project || run.Account != account || run.Workspace != r.PathValue("workspace") {
|
|
reject(w, 409, "fixture_run_unavailable")
|
|
return nil
|
|
}
|
|
s.executeCanvasRun(r.Context(), *run)
|
|
case "stats":
|
|
default:
|
|
reject(w, 404, "not_found")
|
|
return nil
|
|
}
|
|
reply(w, 200, response{"executions": executions.Load()})
|
|
return nil
|
|
}))
|
|
// 其他平台请求走常规 Register;只有 AI 路径分流到可受测试精确控制的 service。
|
|
regular := http.NewServeMux()
|
|
Register(regular, db, callbacks, "https://api.example.com", nil)
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.Contains(r.URL.Path, "/canvas/ai/") {
|
|
mux.ServeHTTP(w, r)
|
|
} else {
|
|
regular.ServeHTTP(w, r)
|
|
}
|
|
})
|
|
}
|