Details (original message): Implement the intent core, conformance suite, catalog alignment, examples, and the intentctl diagnostics CLI.
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package intent
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
// TestConcurrentRecognizeAndReload 验证快照原子替换:识别与 Reload 并发时,
|
|
// 每次 Recognize 都拿到完整一致的快照(-race 下运行)。
|
|
func TestConcurrentRecognizeAndReload(t *testing.T) {
|
|
fsys := fstest.MapFS{"specs/a.json": &fstest.MapFile{Data: []byte(
|
|
`{"intents": [
|
|
{"id": "demo.run.status_query", "domain": "demo", "version": 1, "matcher": {"includes_any": ["进度"]}}
|
|
]}`)}}
|
|
b := newTestBuilder()
|
|
if err := b.Register(demoGuardSpec()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg, err := b.BuildFS(context.Background(), fsys, "specs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec := NewRecognizer(Options{}, nil, nil)
|
|
inputs := []string{"查询任务进度", "你好", "帮我改网关", ""}
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func(seed int) {
|
|
defer wg.Done()
|
|
for j := 0; j < 200; j++ {
|
|
in := inputs[(seed+j)%len(inputs)]
|
|
if _, err := rec.Recognize(context.Background(), in, reg.Snapshot()); err != nil {
|
|
t.Errorf("recognize: %v", err)
|
|
return
|
|
}
|
|
}
|
|
}(i)
|
|
}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < 50; j++ {
|
|
if _, err := reg.Reload(context.Background()); err != nil {
|
|
t.Errorf("reload: %v", err)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
wg.Wait()
|
|
|
|
if got := reg.Snapshot().Revision(); got < 51 {
|
|
t.Fatalf("expected revision >= 51, got %d", got)
|
|
}
|
|
}
|