refactor(*): 平台架构 - 拆分后端领域模块并同步插件协议与模型能力

This commit is contained in:
ddCat
2026-09-11 14:02:49 +08:00
parent 916043fb5a
commit 6e5cbc8b05
644 changed files with 62731 additions and 2320 deletions

View File

@@ -0,0 +1,54 @@
package platform
import (
"context"
"errors"
"testing"
"time"
)
func TestWorkerRuntimeStopsLoopsAndWaitsForTasks(t *testing.T) {
runtime := &Worker{}
ctx, started := runtime.Start()
if !started {
t.Fatal("worker runtime should start")
}
loopStopped := make(chan struct{})
if !runtime.GoLoop(func(context.Context) {
<-ctx.Done()
close(loopStopped)
}) {
t.Fatal("worker loop should start")
}
taskRelease := make(chan struct{})
if !runtime.GoTask(func() { <-taskRelease }) {
t.Fatal("worker task should start")
}
stopCtx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
if err := runtime.Stop(stopCtx); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("stop should wait for the active task, got %v", err)
}
select {
case <-loopStopped:
case <-time.After(time.Second):
t.Fatal("worker loop did not observe drain cancellation")
}
if runtime.ActiveTaskCount() != 1 {
t.Fatalf("active task count = %d, want 1", runtime.ActiveTaskCount())
}
close(taskRelease)
finalCtx, finalCancel := context.WithTimeout(context.Background(), time.Second)
defer finalCancel()
if err := runtime.Stop(finalCtx); err != nil {
t.Fatal(err)
}
if runtime.ActiveTaskCount() != 0 {
t.Fatalf("active task count = %d, want 0", runtime.ActiveTaskCount())
}
if runtime.GoTask(func() {}) {
t.Fatal("drained runtime should reject new tasks")
}
}