mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-13 11:40:53 +08:00
agent, db, server and evidence serialized on one shared test database via an advisory lock, while evidence, llmrec and traffic-style packages could still run against it in parallel; under full-suite load that window produced a real SQLSTATE 40P01 deadlock in the blackboard direct-source test. TestMain in every PG-using package now rewrites ARTEX_PG_DSN to a per-package database (created automatically by ensureDatabase), so packages neither contend nor need to serialize; the advisory lock stays as an in-package safety net. llmrec gains the TestMain it lacked, which also gives its requires-isolated-DSN tests a truly isolated database inside ./... runs.
35 lines
958 B
Go
35 lines
958 B
Go
package agent
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
)
|
|
|
|
// TestMain acquires a PostgreSQL advisory lock (7337741002) for the entire
|
|
// agent test suite so cross-package DELETE cleanup races with db/server
|
|
// packages are avoided when running `go test ./...`.
|
|
func TestMain(m *testing.M) {
|
|
// Own database per package (#80): with separate databases, packages stop
|
|
// contending on shared tables and the advisory lock below stops
|
|
// serializing go test ./... across packages.
|
|
db.SuffixTestDatabase("agent")
|
|
dsn, _, err := db.DSN()
|
|
if err != nil {
|
|
os.Exit(m.Run())
|
|
}
|
|
conn, err := sql.Open("pgx", dsn)
|
|
if err != nil || conn.Ping() != nil {
|
|
os.Exit(m.Run())
|
|
}
|
|
defer conn.Close()
|
|
if _, err := conn.Exec(`SELECT pg_advisory_lock(7337741002)`); err != nil {
|
|
os.Exit(m.Run())
|
|
}
|
|
defer conn.Exec(`SELECT pg_advisory_unlock(7337741002)`) //nolint:errcheck
|
|
os.Exit(m.Run())
|
|
}
|