mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Adopt grpc_testing to grpc-1.36.
This commit is contained in:
parent
a60676686b
commit
d0962f10ae
@ -1,6 +1,7 @@
|
|||||||
package grpc_testing
|
package grpc_testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
@ -16,7 +17,7 @@ import (
|
|||||||
// StubServer is a server that is easy to customize within individual test
|
// StubServer is a server that is easy to customize within individual test
|
||||||
// cases.
|
// cases.
|
||||||
type StubServer struct {
|
type StubServer struct {
|
||||||
testService *testpb.TestServiceService
|
testService testpb.TestServiceServer
|
||||||
|
|
||||||
// Network and Address are parameters for Listen. Defaults will be used if these are empty before Start.
|
// Network and Address are parameters for Listen. Defaults will be used if these are empty before Start.
|
||||||
Network string
|
Network string
|
||||||
@ -27,7 +28,7 @@ type StubServer struct {
|
|||||||
cleanups []func() // Lambdas executed in Stop(); populated by Start().
|
cleanups []func() // Lambdas executed in Stop(); populated by Start().
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(testService *testpb.TestServiceService) *StubServer {
|
func New(testService testpb.TestServiceServer) *StubServer {
|
||||||
return &StubServer{testService: testService}
|
return &StubServer{testService: testService}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +49,7 @@ func (ss *StubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption)
|
|||||||
ss.cleanups = append(ss.cleanups, func() { lis.Close() })
|
ss.cleanups = append(ss.cleanups, func() { lis.Close() })
|
||||||
|
|
||||||
s := grpc.NewServer(sopts...)
|
s := grpc.NewServer(sopts...)
|
||||||
testpb.RegisterTestServiceService(s, ss.testService)
|
testpb.RegisterTestServiceServer(s, ss.testService)
|
||||||
go s.Serve(lis)
|
go s.Serve(lis)
|
||||||
ss.cleanups = append(ss.cleanups, s.Stop)
|
ss.cleanups = append(ss.cleanups, s.Stop)
|
||||||
ss.s = s
|
ss.s = s
|
||||||
@ -67,3 +68,23 @@ func (ss *StubServer) Stop() {
|
|||||||
func (ss *StubServer) Addr() string {
|
func (ss *StubServer) Addr() string {
|
||||||
return ss.Address
|
return ss.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dummyStubServer struct {
|
||||||
|
testpb.UnimplementedTestServiceServer
|
||||||
|
body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d dummyStubServer) UnaryCall(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
|
||||||
|
return &testpb.SimpleResponse{
|
||||||
|
Payload: &testpb.Payload{
|
||||||
|
Type: testpb.PayloadType_COMPRESSABLE,
|
||||||
|
Body: d.body,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDummyStubServer creates a simple test server that serves Unary calls with
|
||||||
|
// responses with the given payload.
|
||||||
|
func NewDummyStubServer(body []byte) *StubServer {
|
||||||
|
return New(dummyStubServer{body: body})
|
||||||
|
}
|
||||||
|
@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
"go.etcd.io/etcd/client/v3/naming/endpoints"
|
"go.etcd.io/etcd/client/v3/naming/endpoints"
|
||||||
"go.etcd.io/etcd/client/v3/naming/resolver"
|
"go.etcd.io/etcd/client/v3/naming/resolver"
|
||||||
grpctest "go.etcd.io/etcd/pkg/v3/grpc_testing"
|
"go.etcd.io/etcd/pkg/v3/grpc_testing"
|
||||||
"go.etcd.io/etcd/tests/v3/integration"
|
"go.etcd.io/etcd/tests/v3/integration"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@ -35,14 +35,14 @@ func TestEtcdGrpcResolver(t *testing.T) {
|
|||||||
integration.BeforeTest(t)
|
integration.BeforeTest(t)
|
||||||
|
|
||||||
s1PayloadBody := []byte{'1'}
|
s1PayloadBody := []byte{'1'}
|
||||||
s1 := newDummyStubServer(s1PayloadBody)
|
s1 := grpc_testing.NewDummyStubServer(s1PayloadBody)
|
||||||
if err := s1.Start(nil); err != nil {
|
if err := s1.Start(nil); err != nil {
|
||||||
t.Fatal("failed to start dummy grpc server (s1)", err)
|
t.Fatal("failed to start dummy grpc server (s1)", err)
|
||||||
}
|
}
|
||||||
defer s1.Stop()
|
defer s1.Stop()
|
||||||
|
|
||||||
s2PayloadBody := []byte{'2'}
|
s2PayloadBody := []byte{'2'}
|
||||||
s2 := newDummyStubServer(s2PayloadBody)
|
s2 := grpc_testing.NewDummyStubServer(s2PayloadBody)
|
||||||
if err := s2.Start(nil); err != nil {
|
if err := s2.Start(nil); err != nil {
|
||||||
t.Fatal("failed to start dummy grpc server (s2)", err)
|
t.Fatal("failed to start dummy grpc server (s2)", err)
|
||||||
}
|
}
|
||||||
@ -112,17 +112,3 @@ func TestEtcdGrpcResolver(t *testing.T) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDummyStubServer(body []byte) *grpctest.StubServer {
|
|
||||||
testService := &testpb.TestServiceService{
|
|
||||||
UnaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
|
|
||||||
return &testpb.SimpleResponse{
|
|
||||||
Payload: &testpb.Payload{
|
|
||||||
Type: testpb.PayloadType_COMPRESSABLE,
|
|
||||||
Body: body,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return grpctest.New(testService)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user