Content-Length: 2506 | pFad | http://github.com/coder/coder/pull/18556.diff

thub.com 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()








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/coder/coder/pull/18556.diff

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy