Skip to content

Commit

Permalink
test: modify tests to use stubserver instead of Testservice implement…
Browse files Browse the repository at this point in the history
…ation (grpc#8022)
  • Loading branch information
pvsravani authored Feb 3, 2025
1 parent 990f5e0 commit ee3e8d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 54 deletions.
38 changes: 15 additions & 23 deletions credentials/alts/alts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
altspb "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
"google.golang.org/grpc/credentials/alts/internal/testutil"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
testpb "google.golang.org/grpc/interop/grpc_testing"
Expand Down Expand Up @@ -323,7 +324,7 @@ func (s) TestFullHandshake(t *testing.T) {
defer wait.Wait()
stopHandshaker, handshakerAddress := startFakeHandshakerService(t, &wait)
defer stopHandshaker()
stopServer, serverAddress := startServer(t, handshakerAddress, &wait)
stopServer, serverAddress := startServer(t, handshakerAddress)
defer stopServer()

// Ping the server, authenticating with ALTS.
Expand All @@ -349,7 +350,7 @@ func (s) TestConcurrentHandshakes(t *testing.T) {
defer wait.Wait()
stopHandshaker, handshakerAddress := startFakeHandshakerService(t, &wait)
defer stopHandshaker()
stopServer, serverAddress := startServer(t, handshakerAddress, &wait)
stopServer, serverAddress := startServer(t, handshakerAddress)
defer stopServer()

// Ping the server, authenticating with ALTS.
Expand Down Expand Up @@ -444,31 +445,22 @@ func startFakeHandshakerService(t *testing.T, wait *sync.WaitGroup) (stop func()
return func() { s.Stop() }, listener.Addr().String()
}

func startServer(t *testing.T, handshakerServiceAddress string, wait *sync.WaitGroup) (stop func(), address string) {
func startServer(t *testing.T, handshakerServiceAddress string) (stop func(), address string) {
listener, err := testutils.LocalTCPListener()
if err != nil {
t.Fatalf("LocalTCPListener() failed: %v", err)
}
serverOpts := &ServerOptions{HandshakerServiceAddress: handshakerServiceAddress}
creds := NewServerCreds(serverOpts)
s := grpc.NewServer(grpc.Creds(creds))
testgrpc.RegisterTestServiceServer(s, &testServer{})
wait.Add(1)
go func() {
defer wait.Done()
if err := s.Serve(listener); err != nil {
t.Errorf("s.Serve(%v) failed: %v", listener, err)
}
}()
return func() { s.Stop() }, listener.Addr().String()
}

type testServer struct {
testgrpc.UnimplementedTestServiceServer
}

func (s *testServer) UnaryCall(_ context.Context, _ *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{
Payload: &testpb.Payload{},
}, nil
stub := &stubserver.StubServer{
Listener: listener,
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{
Payload: &testpb.Payload{},
}, nil
},
S: grpc.NewServer(grpc.Creds(creds)),
}
stubserver.StartTestService(t, stub)
return func() { stub.S.Stop() }, listener.Addr().String()
}
23 changes: 11 additions & 12 deletions test/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/interop"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
testpb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
)
Expand Down Expand Up @@ -59,18 +61,15 @@ func (s) TestPeerForClientStatsHandler(t *testing.T) {
if err != nil {
t.Fatal(err)
}
s := grpc.NewServer()
testgrpc.RegisterTestServiceServer(s, interop.NewTestServer())
errCh := make(chan error)
go func() {
errCh <- s.Serve(l)
}()
defer func() {
s.Stop()
if err := <-errCh; err != nil {
t.Error(err)
}
}()
ss := &stubserver.StubServer{
Listener: l,
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(),
}
stubserver.StartTestService(t, ss)
defer ss.S.Stop()

// Create client with stats handler and do some calls.
cc, err := grpc.NewClient(
Expand Down
29 changes: 10 additions & 19 deletions xds/internal/server/listener_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"fmt"
"net"
"strconv"
"sync"
"testing"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
Expand Down Expand Up @@ -159,14 +159,6 @@ func (s) TestListenerWrapper(t *testing.T) {
}
}

type testService struct {
testgrpc.TestServiceServer
}

func (*testService) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
}

// TestConnsCleanup tests that the listener wrapper clears it's connection
// references when connections close. It sets up a listener wrapper and gRPC
// Server, and connects to the server 100 times and makes an RPC each time, and
Expand Down Expand Up @@ -220,14 +212,15 @@ func (s) TestConnsCleanup(t *testing.T) {
}
}

server := grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
testgrpc.RegisterTestServiceServer(server, &testService{})
wg := sync.WaitGroup{}
go func() {
if err := server.Serve(lw); err != nil {
t.Errorf("failed to serve: %v", err)
}
}()
ss := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(grpc.Creds(insecure.NewCredentials())),
}
stubserver.StartTestService(t, ss)
defer ss.S.Stop()

// Make 100 connections to the server, and make an RPC on each one.
for i := 0; i < 100; i++ {
Expand Down Expand Up @@ -255,6 +248,4 @@ func (s) TestConnsCleanup(t *testing.T) {
t.Fatalf("timeout waiting for lis wrapper conns to clear, size: %v", lenConns)
}

server.Stop()
wg.Wait()
}

0 comments on commit ee3e8d9

Please # to comment.