From 9142e418a71ad8f5594520eeb345f5c036a85f3b Mon Sep 17 00:00:00 2001 From: Arjan Bal Date: Fri, 27 Sep 2024 14:53:16 +0530 Subject: [PATCH 1/2] Wait for server to be ready --- test/xds/xds_server_integration_test.go | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/xds/xds_server_integration_test.go b/test/xds/xds_server_integration_test.go index 1525cd1a65ee..8a63bb32ab2a 100644 --- a/test/xds/xds_server_integration_test.go +++ b/test/xds/xds_server_integration_test.go @@ -25,6 +25,7 @@ import ( "net" "strconv" "testing" + "time" "github.com/google/uuid" "google.golang.org/grpc" @@ -32,6 +33,7 @@ import ( "google.golang.org/grpc/credentials/insecure" xdscreds "google.golang.org/grpc/credentials/xds" "google.golang.org/grpc/internal" + "google.golang.org/grpc/internal/grpcsync" "google.golang.org/grpc/internal/testutils" "google.golang.org/grpc/internal/testutils/xds/e2e" "google.golang.org/grpc/internal/testutils/xds/e2e/setup" @@ -78,6 +80,19 @@ func testModeChangeServerOption(t *testing.T) grpc.ServerOption { }) } +// acceptNotifyingListener wraps a listener and notifies users when a server +// calls the Listener.Accept() method. This can be used to ensure that the +// server is ready before requests are sent to it. +type acceptNotifyingListener struct { + net.Listener + serverReady grpcsync.Event +} + +func (l *acceptNotifyingListener) Accept() (net.Conn, error) { + l.serverReady.Fire() + return l.Listener.Accept() +} + // setupGRPCServer performs the following: // - spin up an xDS-enabled gRPC server, configure it with xdsCredentials and // register the test service on it @@ -110,12 +125,24 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func t.Fatalf("testutils.LocalTCPListener() failed: %v", err) } + readyLis := &acceptNotifyingListener{ + Listener: lis, + serverReady: *grpcsync.NewEvent(), + } + go func() { - if err := server.Serve(lis); err != nil { + if err := server.Serve(readyLis); err != nil { t.Errorf("Serve() failed: %v", err) } }() + // Wait for the server to start running. + select { + case <-readyLis.serverReady.Done(): + case <-time.After(defaultTestTimeout): + t.Fatalf("Timed out while waiting for the back end server to start") + } + return lis, func() { server.Stop() } From 3b1b79c35c5d841f604c2a2a683904e7597c6df9 Mon Sep 17 00:00:00 2001 From: Arjan Bal Date: Thu, 3 Oct 2024 10:47:53 +0530 Subject: [PATCH 2/2] Improve error message --- test/xds/xds_server_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/xds/xds_server_integration_test.go b/test/xds/xds_server_integration_test.go index 8a63bb32ab2a..347f069c53bd 100644 --- a/test/xds/xds_server_integration_test.go +++ b/test/xds/xds_server_integration_test.go @@ -140,7 +140,7 @@ func setupGRPCServer(t *testing.T, bootstrapContents []byte) (net.Listener, func select { case <-readyLis.serverReady.Done(): case <-time.After(defaultTestTimeout): - t.Fatalf("Timed out while waiting for the back end server to start") + t.Fatalf("Timed out while waiting for the backend server to start serving") } return lis, func() {