Skip to content

Commit

Permalink
backport etcd-io#12709 and etcd-io#12801 to resolve gogo unmarshal er…
Browse files Browse the repository at this point in the history
…rors

Signed-off-by: Chao Chen <[email protected]>
  • Loading branch information
chaochn47 committed Nov 22, 2023
1 parent 1665b8e commit f549da3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
19 changes: 3 additions & 16 deletions clientv3/integration/naming/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"go.etcd.io/etcd/clientv3/naming/endpoints"
"go.etcd.io/etcd/clientv3/naming/resolver"
"go.etcd.io/etcd/integration"
grpctest "go.etcd.io/etcd/pkg/grpc_testing"
"go.etcd.io/etcd/pkg/grpc_testing"
"go.etcd.io/etcd/pkg/testutil"
)

Expand All @@ -35,14 +35,14 @@ import (
func TestEtcdGrpcResolver(t *testing.T) {
defer testutil.AfterTest(t)
s1PayloadBody := []byte{'1'}
s1 := newDummyStubServer(s1PayloadBody)
s1 := grpc_testing.NewDummyStubServer(s1PayloadBody)
if err := s1.Start(nil); err != nil {
t.Fatal("failed to start dummy grpc server (s1)", err)
}
defer s1.Stop()

s2PayloadBody := []byte{'2'}
s2 := newDummyStubServer(s2PayloadBody)
s2 := grpc_testing.NewDummyStubServer(s2PayloadBody)
if err := s2.Start(nil); err != nil {
t.Fatal("failed to start dummy grpc server (s2)", err)
}
Expand Down Expand Up @@ -111,16 +111,3 @@ func TestEtcdGrpcResolver(t *testing.T) {
break
}
}

func newDummyStubServer(body []byte) *grpctest.StubServer {
return &grpctest.StubServer{
UnaryCallF: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return &testpb.SimpleResponse{
Payload: &testpb.Payload{
Type: testpb.PayloadType_COMPRESSABLE,
Body: body,
},
}, nil
},
}
}
2 changes: 1 addition & 1 deletion etcdserver/api/v3rpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package v3rpc

import "github.com/gogo/protobuf/proto"
import "github.com/golang/protobuf/proto"

type codec struct{}

Expand Down
44 changes: 24 additions & 20 deletions pkg/grpc_testing/stub_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ import (
// StubServer is a server that is easy to customize within individual test
// cases.
type StubServer struct {
// Guarantees we satisfy this interface; panics if unimplemented methods are called.
testpb.TestServiceServer

EmptyCallF func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error)
UnaryCallF func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error)
FullDuplexCallF func(stream testpb.TestService_FullDuplexCallServer) error
testService testpb.TestServiceServer

s *grpc.Server

Expand All @@ -47,19 +42,8 @@ type StubServer struct {
cleanups []func() // Lambdas executed in Stop(); populated by Start().
}

// EmptyCall is the handler for testpb.EmptyCall.
func (ss *StubServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return ss.EmptyCallF(ctx, in)
}

// UnaryCall is the handler for testpb.UnaryCall.
func (ss *StubServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
return ss.UnaryCallF(ctx, in)
}

// FullDuplexCall is the handler for testpb.FullDuplexCall.
func (ss *StubServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServer) error {
return ss.FullDuplexCallF(stream)
func New(testService testpb.TestServiceServer) *StubServer {
return &StubServer{testService: testService}
}

// Start starts the server and creates a client connected to it.
Expand All @@ -79,7 +63,7 @@ func (ss *StubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption)
ss.cleanups = append(ss.cleanups, func() { lis.Close() })

s := grpc.NewServer(sopts...)
testpb.RegisterTestServiceServer(s, ss)
testpb.RegisterTestServiceServer(s, ss.testService)
go s.Serve(lis)
ss.cleanups = append(ss.cleanups, s.Stop)
ss.s = s
Expand All @@ -98,3 +82,23 @@ func (ss *StubServer) Stop() {
func (ss *StubServer) Addr() string {
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})
}

0 comments on commit f549da3

Please sign in to comment.