Files
Agent-System-Backend/internal/app/mcp_cache_test.go
T
weichenandCursor cd57276f1c
ci / vet-and-test (pull_request) Successful in 39s
Run tool scripts through the shared host and keep orchestration off
Why: workflow scripts and run_tool_script should share one Python host, while a slow MCP server must not block the first prompt.
Evidence: GOWORK=off go test ./internal/runtime/ ./internal/app/ ./internal/settings/ ./internal/tools/ ./internal/runmgr/
Impact: runtime.codeOrchestration defaults to false, so the tool stays hidden. Workflow script confirmation and its longer timeout cap stay as they were.
Refs: feature/code-orchestration
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 18:04:25 +00:00

74 lines
1.6 KiB
Go

package app
import (
"sync"
"testing"
)
func resetMcpCache() {
mcpMu.Lock()
defer mcpMu.Unlock()
if mcpManager != nil {
mcpManager.Close()
}
mcpKey, mcpManager = "", nil
}
func TestMcpManagerForCacheAndRebuild(t *testing.T) {
resetMcpCache()
defer resetMcpCache()
cfgA := `[{"name":"a","command":"/bin/true"}]`
cfgB := `[{"name":"b","command":"/bin/true"}]`
m1 := mcpManagerFor(cfgA, "")
if m1 == nil {
t.Fatal("valid config must build a manager")
}
if m2 := mcpManagerFor(cfgA, ""); m2 != m1 {
t.Fatal("same config must reuse the cached manager")
}
m3 := mcpManagerFor(cfgB, "")
if m3 == nil || m3 == m1 {
t.Fatal("changed config must rebuild a fresh manager")
}
if mcpManagerFor("", "") != nil {
t.Fatal("empty config disables MCP and clears the cache")
}
if mcpManagerFor(`{bad json`, "") != nil {
t.Fatal("invalid JSON must disable MCP (nil), not panic")
}
if mcpManagerFor(`[{"name":"bad name","command":"x"}]`, "") != nil {
t.Fatal("invalid server config must disable MCP (nil)")
}
// 并发调用不死锁、返回同一实例或各自重建(缓存一致性由锁保证)。
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = mcpManagerFor(cfgB, "")
}()
}
wg.Wait()
}
func TestMcpCacheConcurrencyWithSettingsResolution(t *testing.T) {
resetMcpCache()
defer resetMcpCache()
var wg sync.WaitGroup
cfgs := []string{
`[{"name":"a","command":"/bin/true"}]`,
`[{"name":"b","command":"/bin/true"}]`,
"",
}
for i := 0; i < 12; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
_ = mcpManagerFor(cfgs[i%len(cfgs)], "")
}(i)
}
wg.Wait()
}