test: add test database cleaner in subprocess (#19844)

fixes https://github.com/coder/internal/issues/927

Adds a small subprocess that outlives the testing process to clean up any leaked test databases.
This commit is contained in:
Spike Curtis
2025-09-22 15:27:06 +04:00
committed by GitHub
parent 596fdcba81
commit e2f5401fb2
4 changed files with 249 additions and 1 deletions
+13 -1
View File
@@ -1,6 +1,7 @@
package dbtestutil
import (
"context"
"database/sql"
_ "embed"
"fmt"
@@ -25,6 +26,8 @@ type Broker struct {
uuid uuid.UUID
coderTestingDB *sql.DB
refCount int
// we keep a reference to the stdin of the cleaner so that Go doesn't garbage collect it.
cleanerFD any
}
func (b *Broker) Create(t TBSubset, opts ...OpenOption) (ConnectionParams, error) {
@@ -142,7 +145,16 @@ func (b *Broker) init(t TBSubset) error {
return xerrors.Errorf("ping '%s' database: %w", CoderTestingDBName, err)
}
b.coderTestingDB = coderTestingDB
b.uuid = uuid.New()
if b.uuid == uuid.Nil {
b.uuid = uuid.New()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
b.cleanerFD, err = startCleaner(ctx, b.uuid, coderTestingParams.DSN())
if err != nil {
return xerrors.Errorf("start test db cleaner: %w", err)
}
}
return nil
}