Skip to content

Commit

Permalink
chore: update test code to have comments (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored May 7, 2024
1 parent 5554a9f commit 12de0dc
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions e2e_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,36 @@ func TestPostgresPgxPoolConnect(t *testing.T) {

ctx := context.Background()

d, err := cloudsqlconn.NewDialer(ctx)
if err != nil {
t.Fatalf("failed to init Dialer: %v", err)
}
defer d.Close()

// Configure the driver to connect to the database
dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPass, postgresDB)
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
t.Fatalf("failed to parse pgx config: %v", err)
}

// Create a new dialer with any options
d, err := cloudsqlconn.NewDialer(ctx)
if err != nil {
t.Fatalf("failed to init Dialer: %v", err)
}

// call cleanup when you're done with the database connection to close dialer
cleanup := func() error { return d.Close() }

// Tell the driver to use the Cloud SQL Go Connector to create connections
// postgresConnName takes the form of 'project:region:instance'.
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return d.Dial(ctx, postgresConnName)
}

// Interact with the driver directly as you normally would
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
t.Fatalf("failed to create pool: %s", err)
}
// ... etc

defer cleanup()
defer pool.Close()

var now time.Time
Expand Down

0 comments on commit 12de0dc

Please sign in to comment.