test(mcp): stabilize concurrent policy updates on SQLite

main
wizardchen 2026-09-17 17:20:47 +08:00 committed by lyingbug
parent 1c43934e53
commit ac4dd59712
1 changed files with 12 additions and 2 deletions

View File

@ -2,12 +2,12 @@ package repository
import (
"context"
"fmt"
"sync"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
@ -15,9 +15,15 @@ import (
func newMCPToolApprovalTestRepo(t *testing.T) interfaces.MCPToolApprovalRepository {
t.Helper()
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared&_busy_timeout=5000", t.Name())
dsn := "file:" + uuid.NewString() + "?mode=memory&cache=shared&_busy_timeout=5000"
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
require.NoError(t, err)
sqlDB, err := db.DB()
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, sqlDB.Close()) })
// Match the production SQLite pool: concurrent callers share one connection.
// A busy timeout cannot resolve shared-cache SQLITE_LOCKED table conflicts.
sqlDB.SetMaxOpenConns(1)
require.NoError(t, db.AutoMigrate(&types.MCPToolApproval{}))
return NewMCPToolApprovalRepository(db)
}
@ -98,15 +104,19 @@ func TestMCPToolApprovalConcurrentColumnPatches(t *testing.T) {
var wg sync.WaitGroup
errs := make(chan error, 2)
start := make(chan struct{})
wg.Add(2)
go func() {
defer wg.Done()
<-start
errs <- repo.UpsertPolicy(ctx, 1, "svc", "tool", types.MCPToolPolicyPatch{Enabled: boolPtr(false)})
}()
go func() {
defer wg.Done()
<-start
errs <- repo.UpsertPolicy(ctx, 1, "svc", "tool", types.MCPToolPolicyPatch{RequireApproval: boolPtr(true)})
}()
close(start)
wg.Wait()
close(errs)
for err := range errs {