Skip to content

test: implement unit test to excercise pg connection deadlock #18556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions coderd/database/dbtestutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type options struct {
returnSQLDB func(*sql.DB)
logger slog.Logger
url string
maxConns int
}

type Option func(*options)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it do something to unset the limit if you call it with 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unit test code does not currently set a default MaxConns that I can tell. If we unset it with 0, idk what to default it to 🤔.

I'm sure the package has some default we could revert to.

sqlDB.SetMaxOpenConns(o.maxConns)
}
// Unit tests should not retry serial transaction failures.
db = database.New(sqlDB, database.WithSerialRetryCount(1))

Expand Down
30 changes: 30 additions & 0 deletions coderd/files/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading