2020-02-26 21:34:40 +03:00
|
|
|
package grpc_test
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-26 21:34:40 +03:00
|
|
|
"fmt"
|
2019-06-03 20:44:43 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
bmemory "github.com/micro/go-micro/v3/broker/memory"
|
|
|
|
"github.com/micro/go-micro/v3/client"
|
|
|
|
gcli "github.com/micro/go-micro/v3/client/grpc"
|
|
|
|
"github.com/micro/go-micro/v3/errors"
|
|
|
|
pberr "github.com/micro/go-micro/v3/errors/proto"
|
2020-08-23 20:37:22 +03:00
|
|
|
tgrpc "github.com/micro/go-micro/v3/network/transport/grpc"
|
2020-07-27 15:22:00 +03:00
|
|
|
rmemory "github.com/micro/go-micro/v3/registry/memory"
|
|
|
|
"github.com/micro/go-micro/v3/router"
|
|
|
|
rtreg "github.com/micro/go-micro/v3/router/registry"
|
|
|
|
"github.com/micro/go-micro/v3/server"
|
|
|
|
gsrv "github.com/micro/go-micro/v3/server/grpc"
|
|
|
|
pb "github.com/micro/go-micro/v3/server/grpc/proto"
|
2019-06-03 20:44:43 +03:00
|
|
|
"google.golang.org/grpc"
|
2020-02-06 13:18:33 +03:00
|
|
|
"google.golang.org/grpc/status"
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// server is used to implement helloworld.GreeterServer.
|
2020-02-26 21:34:40 +03:00
|
|
|
type testServer struct {
|
|
|
|
msgCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testServer) Handle(ctx context.Context, msg *pb.Request) error {
|
|
|
|
s.msgCount++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *testServer) HandleError(ctx context.Context, msg *pb.Request) error {
|
|
|
|
return fmt.Errorf("fake")
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-04-29 15:23:10 +03:00
|
|
|
// TestHello implements helloworld.GreeterServer
|
|
|
|
func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
|
|
|
if req.Name == "Error" {
|
|
|
|
return &errors.Error{Id: "1", Code: 99, Detail: "detail"}
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Msg = "Hello " + req.Name
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHello implements helloworld.GreeterServer
|
|
|
|
func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
|
|
|
if req.Name == "Error" {
|
|
|
|
return &errors.Error{Id: "1", Code: 99, Detail: "detail"}
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Msg = "Hello " + req.Name
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:22:28 +03:00
|
|
|
// TestHello implements helloworld.GreeterServer
|
|
|
|
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
2020-02-06 13:18:33 +03:00
|
|
|
if req.Name == "Error" {
|
|
|
|
return &errors.Error{Id: "1", Code: 99, Detail: "detail"}
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:13:51 +03:00
|
|
|
if req.Name == "Panic" {
|
|
|
|
// make it panic
|
|
|
|
panic("handler panic")
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
rsp.Msg = "Hello " + req.Name
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:34:40 +03:00
|
|
|
/*
|
|
|
|
func BenchmarkServer(b *testing.B) {
|
|
|
|
r := rmemory.NewRegistry()
|
|
|
|
br := bmemory.NewBroker()
|
|
|
|
tr := tgrpc.NewTransport()
|
|
|
|
s := gsrv.NewServer(
|
|
|
|
server.Broker(br),
|
|
|
|
server.Name("foo"),
|
|
|
|
server.Registry(r),
|
|
|
|
server.Transport(tr),
|
|
|
|
)
|
|
|
|
c := gcli.NewClient(
|
|
|
|
client.Registry(r),
|
|
|
|
client.Broker(br),
|
|
|
|
client.Transport(tr),
|
|
|
|
)
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
h := &testServer{}
|
|
|
|
pb.RegisterTestHandler(s, h)
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
b.Fatalf("failed to start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check registration
|
|
|
|
services, err := r.GetService("foo")
|
|
|
|
if err != nil || len(services) == 0 {
|
|
|
|
b.Fatalf("failed to get service: %v # %d", err, len(services))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
b.Fatalf("failed to stop: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c.Call()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
2019-06-03 20:44:43 +03:00
|
|
|
func TestGRPCServer(t *testing.T) {
|
2020-02-26 21:34:40 +03:00
|
|
|
r := rmemory.NewRegistry()
|
|
|
|
b := bmemory.NewBroker()
|
|
|
|
tr := tgrpc.NewTransport()
|
2020-07-27 15:22:00 +03:00
|
|
|
rtr := rtreg.NewRouter(router.Registry(r))
|
2020-07-01 19:06:59 +03:00
|
|
|
|
2020-02-26 21:34:40 +03:00
|
|
|
s := gsrv.NewServer(
|
|
|
|
server.Broker(b),
|
2019-06-03 20:44:43 +03:00
|
|
|
server.Name("foo"),
|
|
|
|
server.Registry(r),
|
2020-02-26 21:34:40 +03:00
|
|
|
server.Transport(tr),
|
|
|
|
)
|
2020-07-01 19:06:59 +03:00
|
|
|
|
2020-02-26 21:34:40 +03:00
|
|
|
c := gcli.NewClient(
|
2020-07-01 19:06:59 +03:00
|
|
|
client.Router(rtr),
|
2020-02-26 21:34:40 +03:00
|
|
|
client.Broker(b),
|
|
|
|
client.Transport(tr),
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
2020-02-26 21:34:40 +03:00
|
|
|
ctx := context.TODO()
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-02-26 21:34:40 +03:00
|
|
|
h := &testServer{}
|
|
|
|
pb.RegisterTestHandler(s, h)
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
if err := s.Subscribe(s.NewSubscriber("test_topic", h.Handle)); err != nil {
|
2020-02-26 21:34:40 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check registration
|
|
|
|
services, err := r.GetService("foo")
|
|
|
|
if err != nil || len(services) == 0 {
|
|
|
|
t.Fatalf("failed to get service: %v # %d", err, len(services))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
t.Fatalf("failed to stop: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-26 21:34:40 +03:00
|
|
|
cnt := 4
|
|
|
|
for i := 0; i < cnt; i++ {
|
2020-07-27 15:22:00 +03:00
|
|
|
msg := c.NewMessage("test_topic", &pb.Request{Name: fmt.Sprintf("msg %d", i)})
|
|
|
|
if err = c.Publish(ctx, msg); err != nil {
|
2020-02-26 21:34:40 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.msgCount != cnt {
|
|
|
|
t.Fatalf("pub/sub not work, or invalid message count %d", h.msgCount)
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to dial server: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:22:28 +03:00
|
|
|
testMethods := []string{"/test.Test/Call", "/go.micro.test.Test/Call"}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
for _, method := range testMethods {
|
|
|
|
rsp := pb.Response{}
|
|
|
|
|
|
|
|
if err := cc.Invoke(context.Background(), method, &pb.Request{Name: "John"}, &rsp); err != nil {
|
|
|
|
t.Fatalf("error calling server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Msg != "Hello John" {
|
|
|
|
t.Fatalf("Got unexpected response %v", rsp.Msg)
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:18:33 +03:00
|
|
|
|
|
|
|
// Test grpc error
|
|
|
|
rsp := pb.Response{}
|
|
|
|
|
|
|
|
if err := cc.Invoke(context.Background(), "/test.Test/Call", &pb.Request{Name: "Error"}, &rsp); err != nil {
|
|
|
|
st, ok := status.FromError(err)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("invalid error received %#+v\n", err)
|
|
|
|
}
|
2020-07-19 17:54:33 +03:00
|
|
|
verr, ok := st.Details()[0].(*pberr.Error)
|
2020-02-06 13:18:33 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("invalid error received %#+v\n", st.Details()[0])
|
|
|
|
}
|
|
|
|
if verr.Code != 99 && verr.Id != "1" && verr.Detail != "detail" {
|
|
|
|
t.Fatalf("invalid error received %#+v\n", verr)
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2020-07-06 22:13:51 +03:00
|
|
|
|
|
|
|
// TestGRPCServerWithPanicWrapper test grpc server with panic wrapper
|
|
|
|
// gRPC server should not crash when wrapper crashed
|
|
|
|
func TestGRPCServerWithPanicWrapper(t *testing.T) {
|
|
|
|
r := rmemory.NewRegistry()
|
|
|
|
b := bmemory.NewBroker()
|
|
|
|
tr := tgrpc.NewTransport()
|
|
|
|
s := gsrv.NewServer(
|
|
|
|
server.Broker(b),
|
|
|
|
server.Name("foo"),
|
|
|
|
server.Registry(r),
|
|
|
|
server.Transport(tr),
|
|
|
|
server.WrapHandler(func(hf server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
// make it panic
|
|
|
|
panic("wrapper panic")
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
h := &testServer{}
|
|
|
|
pb.RegisterTestHandler(s, h)
|
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check registration
|
|
|
|
services, err := r.GetService("foo")
|
|
|
|
if err != nil || len(services) == 0 {
|
|
|
|
t.Fatalf("failed to get service: %v # %d", err, len(services))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
t.Fatalf("failed to stop: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to dial server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp := pb.Response{}
|
|
|
|
if err := cc.Invoke(context.Background(), "/test.Test/Call", &pb.Request{Name: "John"}, &rsp); err == nil {
|
|
|
|
t.Fatal("this must return error, as wrapper should be panic")
|
|
|
|
}
|
|
|
|
|
|
|
|
// both wrapper and handler should panic
|
|
|
|
rsp = pb.Response{}
|
|
|
|
if err := cc.Invoke(context.Background(), "/test.Test/Call", &pb.Request{Name: "Panic"}, &rsp); err == nil {
|
|
|
|
t.Fatal("this must return error, as wrapper and handler should be panic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGRPCServerWithPanicWrapper test grpc server with panic handler
|
|
|
|
// gRPC server should not crash when handler crashed
|
|
|
|
func TestGRPCServerWithPanicHandler(t *testing.T) {
|
|
|
|
r := rmemory.NewRegistry()
|
|
|
|
b := bmemory.NewBroker()
|
|
|
|
tr := tgrpc.NewTransport()
|
|
|
|
s := gsrv.NewServer(
|
|
|
|
server.Broker(b),
|
|
|
|
server.Name("foo"),
|
|
|
|
server.Registry(r),
|
|
|
|
server.Transport(tr),
|
|
|
|
)
|
|
|
|
|
|
|
|
h := &testServer{}
|
|
|
|
pb.RegisterTestHandler(s, h)
|
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check registration
|
|
|
|
services, err := r.GetService("foo")
|
|
|
|
if err != nil || len(services) == 0 {
|
|
|
|
t.Fatalf("failed to get service: %v # %d", err, len(services))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := s.Stop(); err != nil {
|
|
|
|
t.Fatalf("failed to stop: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to dial server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp := pb.Response{}
|
|
|
|
if err := cc.Invoke(context.Background(), "/test.Test/Call", &pb.Request{Name: "Panic"}, &rsp); err == nil {
|
|
|
|
t.Fatal("this must return error, as handler should be panic")
|
|
|
|
}
|
|
|
|
}
|