mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-08 22:41:00 +02:00
sqldb: fixup PostgreSQL fixture to allow creating separate DBs per test
This change will enable us to use a single PostgreSQL container instead of spawning new a one for each (parallel) unit test reducing overall test runtime.
This commit is contained in:
@ -1,7 +1,10 @@
|
|||||||
package sqldb
|
package sqldb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -124,19 +127,33 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewTestPostgresDB is a helper function that creates a Postgres database for
|
// NewTestPostgresDB is a helper function that creates a Postgres database for
|
||||||
// testing.
|
// testing using the given fixture.
|
||||||
func NewTestPostgresDB(t *testing.T) *PostgresStore {
|
func NewTestPostgresDB(t *testing.T, fixture *TestPgFixture) *PostgresStore {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
t.Logf("Creating new Postgres DB for testing")
|
// Create random database name.
|
||||||
|
randBytes := make([]byte, 8)
|
||||||
|
_, err := rand.Read(randBytes)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
sqlFixture := NewTestPgFixture(t, DefaultPostgresFixtureLifetime)
|
dbName := "test_" + hex.EncodeToString(randBytes)
|
||||||
store, err := NewPostgresStore(sqlFixture.GetConfig())
|
|
||||||
|
t.Logf("Creating new Postgres DB '%s' for testing", dbName)
|
||||||
|
|
||||||
|
_, err = fixture.db.ExecContext(
|
||||||
|
context.Background(), "CREATE DATABASE "+dbName,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := fixture.GetConfig()
|
||||||
|
cfg.DBName = dbName
|
||||||
|
|
||||||
|
store, err := NewPostgresStore(cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
sqlFixture.TearDown(t)
|
|
||||||
})
|
|
||||||
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,7 @@ func NewTestPgFixture(t *testing.T, expiry time.Duration) *TestPgFixture {
|
|||||||
"postgres",
|
"postgres",
|
||||||
"-c", "log_statement=all",
|
"-c", "log_statement=all",
|
||||||
"-c", "log_destination=stderr",
|
"-c", "log_destination=stderr",
|
||||||
|
"-c", "max_connections=1000",
|
||||||
},
|
},
|
||||||
}, func(config *docker.HostConfig) {
|
}, func(config *docker.HostConfig) {
|
||||||
// Set AutoRemove to true so that stopped container goes away
|
// Set AutoRemove to true so that stopped container goes away
|
||||||
|
Reference in New Issue
Block a user