Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions test/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/resolver"
Expand Down Expand Up @@ -431,10 +432,15 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) {
t.Fatal(err)
}
cred := &authorityCheckCreds{}
s := grpc.NewServer()
go s.Serve(lis)
defer s.Stop()

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(),
}
stubserver.StartTestService(t, stub)
defer stub.S.Stop()
r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
Expand Down Expand Up @@ -463,10 +469,16 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) {
t.Fatal(err)
}
cred := &authorityCheckCreds{}
s := grpc.NewServer()
go s.Serve(lis)
defer s.Stop()

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
S: grpc.NewServer(),
}
stubserver.StartTestService(t, stub)
defer stub.S.Stop()
r := manual.NewBuilderWithScheme("whatever")

cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
Expand Down
159 changes: 93 additions & 66 deletions test/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"
"testing"
Expand All @@ -35,6 +36,7 @@ import (
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
Expand Down Expand Up @@ -150,12 +152,37 @@ func setupServer(t *testing.T, watchFunc healthWatchFunc) (*grpc.Server, net.Lis
} else {
ts = newTestHealthServer()
}
s := grpc.NewServer()
healthgrpc.RegisterHealthServer(s, ts)
testgrpc.RegisterTestServiceServer(s, &testServer{})
go s.Serve(lis)
t.Cleanup(func() { s.Stop() })
return s, lis, ts

stub := &stubserver.StubServer{
Listener: lis,
EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
FullDuplexCallF: func(stream testpb.TestService_FullDuplexCallServer) error {
for {
req, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return fmt.Errorf("error receiving from stream: %v", err)
}
t.Logf("Received message: %v", req)
resp := &testpb.StreamingOutputCallResponse{
Payload: req.Payload,
}
if err := stream.Send(resp); err != nil {
return fmt.Errorf("error sending to stream: %v", err)
}
t.Logf("Sent response: %v", resp)
}
},
S: grpc.NewServer(),
}
healthgrpc.RegisterHealthServer(stub.S, ts)
stubserver.StartTestService(t, stub)
t.Cleanup(func() { stub.Stop() })
return stub.S.(*grpc.Server), lis, ts
}

type clientConfig struct {
Expand Down Expand Up @@ -207,11 +234,11 @@ func (s) TestHealthCheckWatchStateChange(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand Down Expand Up @@ -262,11 +289,11 @@ func (s) TestHealthCheckHealthServerNotRegistered(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand All @@ -289,11 +316,11 @@ func (s) TestHealthCheckWithGoAway(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand Down Expand Up @@ -367,11 +394,11 @@ func (s) TestHealthCheckWithConnClose(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand Down Expand Up @@ -415,11 +442,11 @@ func (s) TestHealthCheckWithAddrConnDrain(t *testing.T) {
cc, r := setupClient(t, &clientConfig{})
tc := testgrpc.NewTestServiceClient(cc)
sc := parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: sc,
Expand Down Expand Up @@ -497,11 +524,11 @@ func (s) TestHealthCheckWithClientConnClose(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand Down Expand Up @@ -564,11 +591,11 @@ func (s) TestHealthCheckWithoutSetConnectivityStateCalledAddrConnShutDown(t *tes
// back to client immediately upon receiving the request (client should receive no response until
// test ends).
sc := parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "delay"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)
"healthCheckConfig": {
"serviceName": "delay"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: sc,
Expand Down Expand Up @@ -629,11 +656,11 @@ func (s) TestHealthCheckWithoutSetConnectivityStateCalled(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "delay"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "delay"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

select {
case <-hcExitChan:
Expand Down Expand Up @@ -667,11 +694,11 @@ func testHealthCheckDisableWithDialOption(t *testing.T, addr string) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: addr}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand Down Expand Up @@ -699,11 +726,11 @@ func testHealthCheckDisableWithBalancer(t *testing.T, addr string) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: addr}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"pick_first":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "foo"
},
"loadBalancingConfig": [{"pick_first":{}}]
}`)})

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
Expand Down Expand Up @@ -773,11 +800,11 @@ func (s) TestHealthCheckChannelzCountingCallSuccess(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "channelzSuccess"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "channelzSuccess"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

if err := verifyResultWithDelay(func() (bool, error) {
cm, _ := channelz.GetTopChannels(0, 0)
Expand Down Expand Up @@ -822,11 +849,11 @@ func (s) TestHealthCheckChannelzCountingCallFailure(t *testing.T) {
r.UpdateState(resolver.State{
Addresses: []resolver.Address{{Addr: lis.Addr().String()}},
ServiceConfig: parseServiceConfig(t, r, `{
"healthCheckConfig": {
"serviceName": "channelzFailure"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})
"healthCheckConfig": {
"serviceName": "channelzFailure"
},
"loadBalancingConfig": [{"round_robin":{}}]
}`)})

if err := verifyResultWithDelay(func() (bool, error) {
cm, _ := channelz.GetTopChannels(0, 0)
Expand Down
Loading