From 89f5828bd9b6bb4cda604a7acbc60d26f59d0bf5 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 24 Jun 2025 20:08:12 -0500 Subject: [PATCH] test: implement unit test to excercise pg connection deadlock --- coderd/database/dbtestutil/db.go | 11 +++++++++++ coderd/files/cache_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/coderd/database/dbtestutil/db.go b/coderd/database/dbtestutil/db.go index fa3567c490826..14334ef9f8bc3 100644 --- a/coderd/database/dbtestutil/db.go +++ b/coderd/database/dbtestutil/db.go @@ -36,6 +36,7 @@ type options struct { returnSQLDB func(*sql.DB) logger slog.Logger url string + maxConns int } type Option func(*options) @@ -66,6 +67,12 @@ func WithURL(u string) Option { } } +func WithMaxConns(maxConns int) Option { + return func(o *options) { + o.maxConns = maxConns + } +} + func withReturnSQLDB(f func(*sql.DB)) Option { return func(o *options) { o.returnSQLDB = f @@ -137,12 +144,16 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) { t.Cleanup(func() { _ = sqlDB.Close() }) + if o.returnSQLDB != nil { o.returnSQLDB(sqlDB) } if o.dumpOnFailure { t.Cleanup(func() { DumpOnFailure(t, connectionURL) }) } + if o.maxConns > 0 { + sqlDB.SetMaxOpenConns(o.maxConns) + } // Unit tests should not retry serial transaction failures. db = database.New(sqlDB, database.WithSerialRetryCount(1)) diff --git a/coderd/files/cache_test.go b/coderd/files/cache_test.go index 8a8acfbc0772f..4cdc776ad9e83 100644 --- a/coderd/files/cache_test.go +++ b/coderd/files/cache_test.go @@ -26,6 +26,36 @@ import ( "github.com/coder/coder/v2/testutil" ) +func TestPGConn(t *testing.T) { + t.Parallel() + + // This test is just to ensure that the Postgres connection is working. + // It does not test the cache itself, but rather the underlying database + // connection. + db, _ := dbtestutil.NewDB(t, dbtestutil.WithMaxConns(1)) + cache := files.New(prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{}) + + //nolint:gocritic // Unit testing + ctx := dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort)) + file := dbgen.File(t, db, database.File{}) + + // Consume a pg connection with a transaction. + tx := dbtestutil.StartTx(t, db, nil) + + // Acquire the file from the cache outside the transaction. + fs, err := cache.Acquire(ctx, db, file.ID) + require.NoError(t, err) + defer fs.Close() + + // Acquire the file from the cache inside the transaction. + fs2, err := cache.Acquire(ctx, tx, file.ID) + require.NoError(t, err) + defer fs2.Close() + require.NoError(t, tx.Done()) // Close the transaction + + require.Equal(t, 1, cache.Count()) +} + // nolint:paralleltest,tparallel // Serially testing is easier func TestCacheRBAC(t *testing.T) { t.Parallel() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy