invoices: run InvoiceStore and InvoiceRegistry tests on PostgreSQL too

This commit is contained in:
Andras Banki-Horvath 2024-01-22 17:54:23 +01:00
parent 06730824a2
commit aee1f7f563
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8
3 changed files with 91 additions and 16 deletions

View File

@ -116,12 +116,28 @@ func TestInvoiceRegistry(t *testing.T) {
return db, testClock return db, testClock
} }
makeSQLDB := func(t *testing.T) (invpkg.InvoiceDB, *clock.TestClock) { // First create a shared Postgres instance so we don't spawn a new
db := sqldb.NewTestSqliteDB(t) // docker container for each test.
pgFixture := sqldb.NewTestPgFixture(
t, sqldb.DefaultPostgresFixtureLifetime,
)
t.Cleanup(func() {
pgFixture.TearDown(t)
})
makeSQLDB := func(t *testing.T, sqlite bool) (invpkg.InvoiceDB,
*clock.TestClock) {
var db *sqldb.BaseDB
if sqlite {
db = sqldb.NewTestSqliteDB(t).BaseDB
} else {
db = sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
}
executor := sqldb.NewTransactionExecutor( executor := sqldb.NewTransactionExecutor(
db, func(tx *sql.Tx) sqldb.InvoiceQueries { db, func(tx *sql.Tx) sqldb.InvoiceQueries {
return db.BaseDB.WithTx(tx) return db.WithTx(tx)
}, },
) )
@ -137,8 +153,22 @@ func TestInvoiceRegistry(t *testing.T) {
test.test(t, makeKeyValueDB) test.test(t, makeKeyValueDB)
}) })
t.Run(test.name+"_SQL", func(t *testing.T) { t.Run(test.name+"_SQLite", func(t *testing.T) {
test.test(t, makeSQLDB) test.test(t,
func(t *testing.T) (
invpkg.InvoiceDB, *clock.TestClock) {
return makeSQLDB(t, true)
})
})
t.Run(test.name+"_Postgres", func(t *testing.T) {
test.test(t,
func(t *testing.T) (
invpkg.InvoiceDB, *clock.TestClock) {
return makeSQLDB(t, false)
})
}) })
} }
} }

View File

@ -6,6 +6,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"math" "math"
"strings"
"testing" "testing"
"time" "time"
@ -36,6 +37,23 @@ var (
testNow = time.Unix(1, 0) testNow = time.Unix(1, 0)
) )
// randBytesToString will return a "safe" string from a byte slice. This is
// used to generate random strings for the invoice payment request.
func randBytesToString(buf []byte) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
var stringBuilder strings.Builder
stringBuilder.Grow(len(buf))
for i := 0; i < len(buf); i++ {
ch := charset[int(buf[i])%len(charset)]
if err := stringBuilder.WriteByte(ch); err != nil {
return "", err
}
}
return stringBuilder.String(), nil
}
func randInvoice(value lnwire.MilliSatoshi) (*invpkg.Invoice, error) { func randInvoice(value lnwire.MilliSatoshi) (*invpkg.Invoice, error) {
var ( var (
pre lntypes.Preimage pre lntypes.Preimage
@ -70,7 +88,11 @@ func randInvoice(value lnwire.MilliSatoshi) (*invpkg.Invoice, error) {
return nil, err return nil, err
} }
if r[0]&1 == 0 { if r[0]&1 == 0 {
i.PaymentRequest = r[:] paymentReq, err := randBytesToString(r[:])
if err != nil {
return nil, err
}
i.PaymentRequest = []byte(paymentReq)
} else { } else {
i.PaymentRequest = []byte("") i.PaymentRequest = []byte("")
} }
@ -200,29 +222,52 @@ func TestInvoices(t *testing.T) {
return db return db
} }
makeSQLDB := func(t *testing.T) invpkg.InvoiceDB { // First create a shared Postgres instance so we don't spawn a new
db := sqldb.NewTestSqliteDB(t) // docker container for each test.
pgFixture := sqldb.NewTestPgFixture(
t, sqldb.DefaultPostgresFixtureLifetime,
)
t.Cleanup(func() {
pgFixture.TearDown(t)
})
makeSQLDB := func(t *testing.T, sqlite bool) invpkg.InvoiceDB {
var db *sqldb.BaseDB
if sqlite {
db = sqldb.NewTestSqliteDB(t).BaseDB
} else {
db = sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
}
executor := sqldb.NewTransactionExecutor( executor := sqldb.NewTransactionExecutor(
db, func(tx *sql.Tx) sqldb.InvoiceQueries { db, func(tx *sql.Tx) sqldb.InvoiceQueries {
return db.BaseDB.WithTx(tx) return db.WithTx(tx)
}, },
) )
return sqldb.NewInvoiceStore( testClock := clock.NewTestClock(testNow)
executor, clock.NewTestClock(testNow),
) return sqldb.NewInvoiceStore(executor, testClock)
} }
for _, test := range testList { for _, test := range testList {
test := test test := test
t.Run(test.name+"_KV", func(t *testing.T) { t.Run(test.name+"_KV", func(t *testing.T) {
test.test(t, makeKeyValueDB) test.test(t, makeKeyValueDB)
}) })
t.Run(test.name+"_SQL", func(t *testing.T) { t.Run(test.name+"_SQLite", func(t *testing.T) {
test.test(t, makeSQLDB) test.test(t,
func(t *testing.T) invpkg.InvoiceDB {
return makeSQLDB(t, true)
})
})
t.Run(test.name+"_Postgres", func(t *testing.T) {
test.test(t,
func(t *testing.T) invpkg.InvoiceDB {
return makeSQLDB(t, false)
})
}) })
} }
} }

View File

@ -231,7 +231,7 @@ func timeout() func() {
go func() { go func() {
select { select {
case <-time.After(5 * time.Second): case <-time.After(10 * time.Second):
err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
if err != nil { if err != nil {
panic(fmt.Sprintf("error writing to std out "+ panic(fmt.Sprintf("error writing to std out "+