From 89da51d10a40e18fce52f127521aa05257bb99fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Wed, 30 Aug 2023 07:27:22 +0200 Subject: [PATCH] test: dynamically resolve address --- samples/golang/pgx/pgx_sample.go | 9 ++++++++- wrappers/golang/pgadapter.go | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/samples/golang/pgx/pgx_sample.go b/samples/golang/pgx/pgx_sample.go index 810229fb1..079c5afce 100644 --- a/samples/golang/pgx/pgx_sample.go +++ b/samples/golang/pgx/pgx_sample.go @@ -17,6 +17,7 @@ import ( "context" "flag" "fmt" + "net" pgadapter "github.com/GoogleCloudPlatform/pgadapter/wrappers/golang" "github.com/jackc/pgx/v5" @@ -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) diff --git a/wrappers/golang/pgadapter.go b/wrappers/golang/pgadapter.go index 969651fcb..bd7912d50 100644 --- a/wrappers/golang/pgadapter.go +++ b/wrappers/golang/pgadapter.go @@ -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 }