-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: modify tests to use stubserver #7951
base: master
Are you sure you want to change the base?
Changes from all commits
2899311
a2ae0e4
71cfc96
2a93d5b
a57bfbd
5a8228b
bab2a27
307ae58
634a971
d8c709a
c493f94
5903d14
ecc7561
87a8136
a82cae3
565c524
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,21 +57,20 @@ func (s) TestGracefulClientOnGoAway(t *testing.T) { | |
const maxConnAge = 100 * time.Millisecond | ||
const testTime = maxConnAge * 10 | ||
|
||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("Failed to create listener: %v", err) | ||
} | ||
|
||
ss := &stubserver.StubServer{ | ||
Listener: lis, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's do the same with listeners to be consistent. lis1, lis2, lis3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { | ||
return &testpb.Empty{}, nil | ||
}, | ||
S: grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: maxConnAge})), | ||
} | ||
|
||
s := grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: maxConnAge})) | ||
defer s.Stop() | ||
testgrpc.RegisterTestServiceServer(s, ss) | ||
|
||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("Failed to create listener: %v", err) | ||
} | ||
go s.Serve(lis) | ||
stubserver.StartTestService(t, ss) | ||
defer ss.S.Stop() | ||
|
||
cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
|
@@ -82,7 +81,6 @@ func (s) TestGracefulClientOnGoAway(t *testing.T) { | |
|
||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
|
||
endTime := time.Now().Add(testTime) | ||
for time.Now().Before(endTime) { | ||
if _, err := c.EmptyCall(ctx, &testpb.Empty{}); err != nil { | ||
|
@@ -551,36 +549,55 @@ func (s) TestGoAwayThenClose(t *testing.T) { | |
if err != nil { | ||
t.Fatalf("Error while listening. Err: %v", err) | ||
} | ||
s1 := grpc.NewServer() | ||
defer s1.Stop() | ||
ts := &funcServer{ | ||
unaryCall: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { | ||
|
||
ss1 := &stubserver.StubServer{ | ||
Listener: lis1, | ||
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { | ||
return &testpb.SimpleResponse{}, nil | ||
}, | ||
fullDuplexCall: func(stream testgrpc.TestService_FullDuplexCallServer) error { | ||
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { | ||
if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil { | ||
t.Errorf("unexpected error from send: %v", err) | ||
return err | ||
} | ||
// Wait forever. | ||
// Wait until a message is received from client | ||
_, err := stream.Recv() | ||
if err == nil { | ||
t.Error("expected to never receive any message") | ||
} | ||
return err | ||
}, | ||
S: grpc.NewServer(), | ||
} | ||
testgrpc.RegisterTestServiceServer(s1, ts) | ||
go s1.Serve(lis1) | ||
stubserver.StartTestService(t, ss1) | ||
defer ss1.S.Stop() | ||
|
||
conn2Established := grpcsync.NewEvent() | ||
lis2, err := listenWithNotifyingListener("tcp", "localhost:0", conn2Established) | ||
if err != nil { | ||
t.Fatalf("Error while listening. Err: %v", err) | ||
} | ||
s2 := grpc.NewServer() | ||
defer s2.Stop() | ||
testgrpc.RegisterTestServiceServer(s2, ts) | ||
ss2 := &stubserver.StubServer{ | ||
Listener: lis2, | ||
UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { | ||
return &testpb.SimpleResponse{}, nil | ||
}, | ||
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { | ||
if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil { | ||
t.Errorf("unexpected error from send: %v", err) | ||
return err | ||
} | ||
// Wait until a message is received from client | ||
_, err := stream.Recv() | ||
if err == nil { | ||
t.Error("expected to never receive any message") | ||
} | ||
return err | ||
}, | ||
S: grpc.NewServer(), | ||
} | ||
stubserver.StartTestService(t, ss2) | ||
defer ss2.S.Stop() | ||
|
||
r := manual.NewBuilderWithScheme("whatever") | ||
r.InitialState(resolver.State{Addresses: []resolver.Address{ | ||
|
@@ -613,10 +630,8 @@ func (s) TestGoAwayThenClose(t *testing.T) { | |
t.Fatalf("unexpected error from first recv: %v", err) | ||
} | ||
|
||
go s2.Serve(lis2) | ||
|
||
t.Log("Gracefully stopping server 1.") | ||
go s1.GracefulStop() | ||
go ss1.S.GracefulStop() | ||
|
||
t.Log("Waiting for the ClientConn to enter IDLE state.") | ||
testutils.AwaitState(ctx, t, cc, connectivity.Idle) | ||
|
@@ -637,7 +652,7 @@ func (s) TestGoAwayThenClose(t *testing.T) { | |
lis2.Close() | ||
|
||
t.Log("Hard closing connection 1.") | ||
s1.Stop() | ||
ss1.S.Stop() | ||
|
||
t.Log("Waiting for the first stream to error.") | ||
if _, err = stream.Recv(); err == nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,13 @@ func (s) TestInsecureCreds(t *testing.T) { | |
|
||
for _, test := range tests { | ||
t.Run(test.desc, func(t *testing.T) { | ||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err) | ||
} | ||
|
||
ss := &stubserver.StubServer{ | ||
Listener: lis, | ||
EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
if !test.serverInsecureCreds { | ||
return &testpb.Empty{}, nil | ||
|
@@ -104,22 +110,14 @@ func (s) TestInsecureCreds(t *testing.T) { | |
return &testpb.Empty{}, nil | ||
}, | ||
} | ||
|
||
sOpts := []grpc.ServerOption{} | ||
if test.serverInsecureCreds { | ||
sOpts = append(sOpts, grpc.Creds(insecure.NewCredentials())) | ||
ss.S = grpc.NewServer(grpc.Creds(insecure.NewCredentials())) | ||
} else { | ||
ss.S = grpc.NewServer() | ||
} | ||
s := grpc.NewServer(sOpts...) | ||
defer s.Stop() | ||
defer ss.S.Stop() | ||
|
||
testgrpc.RegisterTestServiceServer(s, ss) | ||
|
||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err) | ||
} | ||
|
||
go s.Serve(lis) | ||
stubserver.StartTestService(t, ss) | ||
|
||
addr := lis.Addr().String() | ||
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} | ||
|
@@ -143,21 +141,20 @@ func (s) TestInsecureCreds(t *testing.T) { | |
} | ||
|
||
func (s) TestInsecureCreds_WithPerRPCCredentials_AsCallOption(t *testing.T) { | ||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen(tcp, localhost:0) failed: %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())), | ||
} | ||
|
||
s := grpc.NewServer(grpc.Creds(insecure.NewCredentials())) | ||
defer s.Stop() | ||
testgrpc.RegisterTestServiceServer(s, ss) | ||
|
||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err) | ||
} | ||
go s.Serve(lis) | ||
stubserver.StartTestService(t, ss) | ||
defer ss.S.Stop() | ||
|
||
addr := lis.Addr().String() | ||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
|
@@ -179,21 +176,19 @@ func (s) TestInsecureCreds_WithPerRPCCredentials_AsCallOption(t *testing.T) { | |
} | ||
|
||
func (s) TestInsecureCreds_WithPerRPCCredentials_AsDialOption(t *testing.T) { | ||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err) | ||
} | ||
ss := &stubserver.StubServer{ | ||
Listener: lis, | ||
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. listener also needs to be assigned here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return &testpb.Empty{}, nil | ||
}, | ||
S: grpc.NewServer(grpc.Creds(insecure.NewCredentials())), | ||
} | ||
|
||
s := grpc.NewServer(grpc.Creds(insecure.NewCredentials())) | ||
defer s.Stop() | ||
testgrpc.RegisterTestServiceServer(s, ss) | ||
|
||
lis, err := net.Listen("tcp", "localhost:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen(tcp, localhost:0) failed: %v", err) | ||
} | ||
go s.Serve(lis) | ||
stubserver.StartTestService(t, ss) | ||
defer ss.S.Stop() | ||
|
||
addr := lis.Addr().String() | ||
dopts := []grpc.DialOption{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this can be ss1 and then others can ss2, ss3 and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done