Skip to content

Commit 89f5828

Browse files
committed
test: implement unit test to excercise pg connection deadlock
1 parent 288ec77 commit 89f5828

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

coderd/database/dbtestutil/db.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type options struct {
3636
returnSQLDB func(*sql.DB)
3737
logger slog.Logger
3838
url string
39+
maxConns int
3940
}
4041

4142
type Option func(*options)
@@ -66,6 +67,12 @@ func WithURL(u string) Option {
6667
}
6768
}
6869

70+
func WithMaxConns(maxConns int) Option {
71+
return func(o *options) {
72+
o.maxConns = maxConns
73+
}
74+
}
75+
6976
func withReturnSQLDB(f func(*sql.DB)) Option {
7077
return func(o *options) {
7178
o.returnSQLDB = f
@@ -137,12 +144,16 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
137144
t.Cleanup(func() {
138145
_ = sqlDB.Close()
139146
})
147+
140148
if o.returnSQLDB != nil {
141149
o.returnSQLDB(sqlDB)
142150
}
143151
if o.dumpOnFailure {
144152
t.Cleanup(func() { DumpOnFailure(t, connectionURL) })
145153
}
154+
if o.maxConns > 0 {
155+
sqlDB.SetMaxOpenConns(o.maxConns)
156+
}
146157
// Unit tests should not retry serial transaction failures.
147158
db = database.New(sqlDB, database.WithSerialRetryCount(1))
148159

coderd/files/cache_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@ import (
2626
"github.com/coder/coder/v2/testutil"
2727
)
2828

29+
func TestPGConn(t *testing.T) {
30+
t.Parallel()
31+
32+
// This test is just to ensure that the Postgres connection is working.
33+
// It does not test the cache itself, but rather the underlying database
34+
// connection.
35+
db, _ := dbtestutil.NewDB(t, dbtestutil.WithMaxConns(1))
36+
cache := files.New(prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{})
37+
38+
//nolint:gocritic // Unit testing
39+
ctx := dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort))
40+
file := dbgen.File(t, db, database.File{})
41+
42+
// Consume a pg connection with a transaction.
43+
tx := dbtestutil.StartTx(t, db, nil)
44+
45+
// Acquire the file from the cache outside the transaction.
46+
fs, err := cache.Acquire(ctx, db, file.ID)
47+
require.NoError(t, err)
48+
defer fs.Close()
49+
50+
// Acquire the file from the cache inside the transaction.
51+
fs2, err := cache.Acquire(ctx, tx, file.ID)
52+
require.NoError(t, err)
53+
defer fs2.Close()
54+
require.NoError(t, tx.Done()) // Close the transaction
55+
56+
require.Equal(t, 1, cache.Count())
57+
}
58+
2959
// nolint:paralleltest,tparallel // Serially testing is easier
3060
func TestCacheRBAC(t *testing.T) {
3161
t.Parallel()

0 commit comments

Comments
 (0)
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