From aee1f7f563b581142bf615545ce1ee07d4c09140 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Mon, 22 Jan 2024 17:54:23 +0100 Subject: [PATCH] invoices: run InvoiceStore and InvoiceRegistry tests on PostgreSQL too --- invoices/invoiceregistry_test.go | 40 +++++++++++++++++--- invoices/invoices_test.go | 65 +++++++++++++++++++++++++++----- invoices/test_utils_test.go | 2 +- 3 files changed, 91 insertions(+), 16 deletions(-) diff --git a/invoices/invoiceregistry_test.go b/invoices/invoiceregistry_test.go index a1204573c..99ad6cddd 100644 --- a/invoices/invoiceregistry_test.go +++ b/invoices/invoiceregistry_test.go @@ -116,12 +116,28 @@ func TestInvoiceRegistry(t *testing.T) { return db, testClock } - makeSQLDB := func(t *testing.T) (invpkg.InvoiceDB, *clock.TestClock) { - db := sqldb.NewTestSqliteDB(t) + // First create a shared Postgres instance so we don't spawn a new + // 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( 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) }) - t.Run(test.name+"_SQL", func(t *testing.T) { - test.test(t, makeSQLDB) + t.Run(test.name+"_SQLite", func(t *testing.T) { + 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) + }) }) } } diff --git a/invoices/invoices_test.go b/invoices/invoices_test.go index 4296f0a54..7e1baadd3 100644 --- a/invoices/invoices_test.go +++ b/invoices/invoices_test.go @@ -6,6 +6,7 @@ import ( "database/sql" "fmt" "math" + "strings" "testing" "time" @@ -36,6 +37,23 @@ var ( 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) { var ( pre lntypes.Preimage @@ -70,7 +88,11 @@ func randInvoice(value lnwire.MilliSatoshi) (*invpkg.Invoice, error) { return nil, err } if r[0]&1 == 0 { - i.PaymentRequest = r[:] + paymentReq, err := randBytesToString(r[:]) + if err != nil { + return nil, err + } + i.PaymentRequest = []byte(paymentReq) } else { i.PaymentRequest = []byte("") } @@ -200,29 +222,52 @@ func TestInvoices(t *testing.T) { return db } - makeSQLDB := func(t *testing.T) invpkg.InvoiceDB { - db := sqldb.NewTestSqliteDB(t) + // First create a shared Postgres instance so we don't spawn a new + // 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( db, func(tx *sql.Tx) sqldb.InvoiceQueries { - return db.BaseDB.WithTx(tx) + return db.WithTx(tx) }, ) - return sqldb.NewInvoiceStore( - executor, clock.NewTestClock(testNow), - ) + testClock := clock.NewTestClock(testNow) + + return sqldb.NewInvoiceStore(executor, testClock) } for _, test := range testList { test := test - t.Run(test.name+"_KV", func(t *testing.T) { test.test(t, makeKeyValueDB) }) - t.Run(test.name+"_SQL", func(t *testing.T) { - test.test(t, makeSQLDB) + t.Run(test.name+"_SQLite", func(t *testing.T) { + 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) + }) }) } } diff --git a/invoices/test_utils_test.go b/invoices/test_utils_test.go index dd2638408..a0adf7dc8 100644 --- a/invoices/test_utils_test.go +++ b/invoices/test_utils_test.go @@ -231,7 +231,7 @@ func timeout() func() { go func() { select { - case <-time.After(5 * time.Second): + case <-time.After(10 * time.Second): err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) if err != nil { panic(fmt.Sprintf("error writing to std out "+