Skip to content

Commit

Permalink
chore: close postgres db on cleanup (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum authored Feb 27, 2024
1 parent a9420ee commit 85f29b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion testhelper/docker/resource/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*R
if err != nil {
return nil, err
}
var db *sql.DB

d.Cleanup(func() {
if d.Failed() && c.PrintLogsOnError {
Expand All @@ -86,13 +87,16 @@ func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*R
if err := pool.Purge(postgresContainer); err != nil {
d.Log("Could not purge resource:", err)
}
if db != nil {
_ = db.Close()
}
})

dbDSN := fmt.Sprintf(
"postgres://%s:%s@localhost:%s/%s?sslmode=disable",
postgresDefaultUser, postgresDefaultPassword, postgresContainer.GetPort("5432/tcp"), postgresDefaultDB,
)
var db *sql.DB

// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
err = pool.Retry(func() (err error) {
// 1. use pg_isready
Expand Down
1 change: 1 addition & 0 deletions testhelper/docker/resource/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Setup(ctx context.Context, pool *dockertest.Pool, d resource.Cleaner, opts
redisClient := redis.NewClient(&redis.Options{
Addr: addr,
})
defer func() { _ = redisClient.Close() }()
_, err := redisClient.Ping(ctx).Result()
return err
})
Expand Down

0 comments on commit 85f29b5

Please sign in to comment.