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.
39 lines
993 B
Go
39 lines
993 B
Go
package evidence
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// Initialize an explicitly configured fresh database before taking the same
|
|
// suite lock as db, agent and server. Hold it on one pinned connection.
|
|
func TestMain(m *testing.M) {
|
|
// Own database per package (#80): stop contending on the shared tables of
|
|
// a single test database during parallel ./... runs.
|
|
db.SuffixTestDatabase("evidence")
|
|
if os.Getenv("ARTEX_PG_DSN") == "" {
|
|
os.Exit(m.Run())
|
|
}
|
|
os.Exit(runEvidenceSuite(m))
|
|
}
|
|
func runEvidenceSuite(m *testing.M) int {
|
|
pg, err := db.Open(os.Getenv("ARTEX_PG_DSN"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer pg.Close()
|
|
conn, err := pg.Conn(context.Background())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer conn.Close()
|
|
if _, err = conn.ExecContext(context.Background(), `SELECT pg_advisory_lock(7337741002)`); err != nil {
|
|
panic(err)
|
|
}
|
|
defer conn.ExecContext(context.Background(), `SELECT pg_advisory_unlock(7337741002)`)
|
|
return m.Run()
|
|
}
|