Skip to content

Commit

Permalink
test: dynamically resolve address
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Aug 30, 2023
1 parent 9164ce4 commit 89da51d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion samples/golang/pgx/pgx_sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"flag"
"fmt"
"net"

pgadapter "github.com/GoogleCloudPlatform/pgadapter/wrappers/golang"
"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -59,7 +60,13 @@ func main() {
return
}
// Connect to Cloud Spanner through PGAdapter.
connString := fmt.Sprintf("postgres://uid:pwd@localhost:%d/%s?sslmode=disable", port, *database)
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
fmt.Printf("failed to resolve localhost address %v\n", err)
return
}
fmt.Printf("Connecting to PGAdapter on address %s\n", addr)
connString := fmt.Sprintf("postgres://uid:pwd@%s/%s?sslmode=disable", addr, *database)
conn, err := pgx.Connect(ctx, connString)
if err != nil {
fmt.Printf("failed to connect to PGAdapter: %v\n", err)
Expand Down
5 changes: 4 additions & 1 deletion wrappers/golang/pgadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,10 @@ func startJava(ctx context.Context, config Config) (*PGAdapter, error) {
return nil, err
}
// Wait for PGAdapter to start.
_ = waitForPort(port, 50*time.Millisecond, 20)
if err := waitForPort(port, 50*time.Millisecond, 20); err != nil {
cancelFunc()
return nil, err
}
return &PGAdapter{stopFunc: cancelFunc, port: port}, nil
}

Expand Down

0 comments on commit 89da51d

Please sign in to comment.