Skip to content

Commit

Permalink
xds: Fix flaky test TestUnmarshalListener_WithUpdateValidatorFunc (#7675
Browse files Browse the repository at this point in the history
)
  • Loading branch information
arjan-bal authored Oct 3, 2024
1 parent ca4865d commit 67e47fc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion test/xds/xds_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
"net"
"strconv"
"testing"
"time"

"github.com/google/uuid"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 backend server to start serving")
}

return lis, func() {
server.Stop()
}
Expand Down

0 comments on commit 67e47fc

Please sign in to comment.