-
Notifications
You must be signed in to change notification settings - Fork 97
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
Test DB pool fixes #769
Test DB pool fixes #769
Changes from all commits
8b5afc3
4153a4e
8372cbc
b7a7169
c1f5b53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,8 @@ func DrainContinuously[T any](drainChan <-chan T) func() []T { | |
|
||
// TestDB acquires a dedicated test database for the duration of the test. If an | ||
// error occurs, the test fails. The test database will be automatically | ||
// returned to the pool at the end of the test and the pgxpool will be closed. | ||
// returned to the pool at the end of the test. If the pool was closed, it will | ||
// be recreated. | ||
func TestDB(ctx context.Context, tb testing.TB) *pgxpool.Pool { | ||
tb.Helper() | ||
|
||
|
@@ -173,9 +174,6 @@ func TestDB(ctx context.Context, tb testing.TB) *pgxpool.Pool { | |
} | ||
tb.Cleanup(testPool.Release) | ||
|
||
// Also close the pool just to ensure nothing is still active on it: | ||
tb.Cleanup(testPool.Pool().Close) | ||
|
||
return testPool.Pool() | ||
} | ||
|
||
|
@@ -282,8 +280,16 @@ func TruncateRiverTables(ctx context.Context, pool *pgxpool.Pool) error { | |
// amongst all packages. e.g. Configures a manager for test databases on setup, | ||
// and checks for no goroutine leaks on teardown. | ||
func WrapTestMain(m *testing.M) { | ||
poolConfig := DatabaseConfig("river_test") | ||
// Use a smaller number of conns per pool, because otherwise we could have | ||
// NUM_CPU pools, each with NUM_CPU connections, and that's a lot of | ||
// connections if there are many CPUs. | ||
Comment on lines
+284
to
+286
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually realized this issue because in some of my tests I was hitting the 100 conn limit on my local Postgres, at least once I stopped closing every pool after every test. 14 cores * 14 conns per pool = 196 potential conns. I don't think any given test ever needs to use more than 4 in the pool. |
||
poolConfig.MaxConns = 4 | ||
// Pre-initialize 1 connection per pool. | ||
poolConfig.MinConns = 1 | ||
|
||
var err error | ||
dbManager, err = testdb.NewManager(DatabaseConfig("river_test"), dbPoolMaxConns, nil, TruncateRiverTables) | ||
dbManager, err = testdb.NewManager(poolConfig, int32(runtime.GOMAXPROCS(0)), nil, TruncateRiverTables) //nolint:gosec | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we actually want to rely on this setting since it could be removed someday. However it's at least somewhat dynamic and can be overridden, whereas
runtime.NumCPU()
is fixed. My thought was this option is just slightly more reliable if somebody tweaks theGOMAXPROCS
and still wants things to work here.Not set on it though.