Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Emyrk wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromstevenmasley/file_cache_tx_deadlock
Draft
Show file tree
Hide file tree
Changes fromall 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 deletionscoderd/database/dbtestutil/db.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 {
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 deletionscoderd/files/cache_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp