2020-09-29 11:15:29 +03:00
|
|
|
package grpc_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-29 00:27:32 +03:00
|
|
|
"io"
|
2023-01-07 19:16:48 +03:00
|
|
|
"net"
|
2021-03-29 00:27:32 +03:00
|
|
|
"net/http"
|
2020-09-29 11:15:29 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
gclient "go.unistack.org/micro-client-grpc/v3"
|
|
|
|
protocodec "go.unistack.org/micro-codec-proto/v3"
|
|
|
|
regRouter "go.unistack.org/micro-router-register/v3"
|
|
|
|
gserver "go.unistack.org/micro-server-grpc/v3"
|
|
|
|
httpsrv "go.unistack.org/micro-server-http/v3"
|
|
|
|
gpb "go.unistack.org/micro-tests/server/grpc/gproto"
|
|
|
|
pb "go.unistack.org/micro-tests/server/grpc/proto"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/errors"
|
2023-01-07 19:16:48 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2021-10-27 01:24:35 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
health "go.unistack.org/micro/v3/server/health"
|
2023-01-07 19:16:48 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"google.golang.org/grpc/encoding/gzip"
|
|
|
|
gmetadata "google.golang.org/grpc/metadata"
|
2020-09-29 11:15:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type testServer struct {
|
|
|
|
pb.UnimplementedTestServer
|
|
|
|
}
|
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
type testnServer struct {
|
|
|
|
pb.UnimplementedTestServer
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:08:01 +03:00
|
|
|
func NewServerHandlerWrapper() server.HandlerWrapper {
|
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
2021-10-01 01:37:54 +03:00
|
|
|
// fmt.Printf("wrap ctx: %#+v req: %#+v\n", ctx, req)
|
2021-02-06 19:08:01 +03:00
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
func (g *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
2023-01-07 19:16:48 +03:00
|
|
|
_, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return errors.InternalServerError("xxx", "missing metadata")
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
if req.Name == "Error" {
|
2021-10-01 01:37:54 +03:00
|
|
|
return &errors.Error{ID: "id", Code: 99, Detail: "detail"}
|
2020-09-29 11:15:29 +03:00
|
|
|
}
|
|
|
|
rsp.Msg = "Hello " + req.Name
|
2020-12-29 13:44:22 +03:00
|
|
|
rsp.Broken = &pb.Broken{Field: "12345"}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
func (g *testnServer) Call(ctx context.Context, req *pb.Request) (*pb.Response, error) {
|
|
|
|
_, ok := gmetadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.InternalServerError("xxx", "missing metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Name == "Error" {
|
|
|
|
return nil, &errors.Error{ID: "id", Code: 99, Detail: "detail"}
|
|
|
|
}
|
|
|
|
rsp := &pb.Response{}
|
|
|
|
|
|
|
|
for i := 0; i < 650; i++ {
|
|
|
|
rsp.Msg += "Hello " + req.Name
|
|
|
|
}
|
|
|
|
return rsp, nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
func TestGRPCServer(t *testing.T) {
|
|
|
|
var err error
|
2023-01-07 19:16:48 +03:00
|
|
|
codec.DefaultMaxMsgSize = 8 * 1024 * 1024
|
2021-03-29 00:27:32 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2023-01-07 19:16:48 +03:00
|
|
|
_ = logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel))
|
2021-02-12 20:50:02 +03:00
|
|
|
r := register.NewRegister()
|
|
|
|
b := broker.NewBroker(broker.Register(r))
|
2021-03-29 00:27:32 +03:00
|
|
|
s := gserver.NewServer(
|
|
|
|
server.Codec("application/grpc+proto", protocodec.NewCodec()),
|
2023-01-07 19:16:48 +03:00
|
|
|
server.Codec("application/grpc", protocodec.NewCodec()),
|
2021-07-16 23:30:58 +03:00
|
|
|
server.Address("127.0.0.1:0"),
|
|
|
|
server.Register(r),
|
|
|
|
server.Name("helloworld"),
|
|
|
|
gserver.Reflection(true),
|
2021-02-06 19:08:01 +03:00
|
|
|
server.WrapHandler(NewServerHandlerWrapper()),
|
|
|
|
)
|
2020-09-29 11:15:29 +03:00
|
|
|
// create router
|
2021-01-29 16:47:26 +03:00
|
|
|
rtr := regRouter.NewRouter(router.Register(r))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
h := &testServer{}
|
2021-03-29 00:27:32 +03:00
|
|
|
if err = gpb.RegisterTestServer(s, h); err != nil {
|
2020-09-29 11:15:29 +03:00
|
|
|
t.Fatalf("can't register handler: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-03-29 00:27:32 +03:00
|
|
|
srv := httpsrv.NewServer(
|
2021-07-16 23:30:58 +03:00
|
|
|
server.Address("127.0.0.1:0"),
|
2021-03-29 00:27:32 +03:00
|
|
|
server.Codec("text/plain", codec.NewCodec()),
|
|
|
|
)
|
2023-02-22 00:12:01 +03:00
|
|
|
if err = health.RegisterHealthServiceServer(srv, health.NewHandler(health.Version("0.0.1"))); err != nil {
|
2021-03-29 00:27:32 +03:00
|
|
|
t.Fatalf("cant register health handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
if err = s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2021-03-29 00:27:32 +03:00
|
|
|
if err = srv.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 11:15:29 +03:00
|
|
|
if err = s.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-03-29 00:27:32 +03:00
|
|
|
hr, err := http.NewRequestWithContext(ctx, "GET", "http://"+srv.Options().Address+"/version", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
hr.Header.Set("Content-Type", "text/plain")
|
|
|
|
rsp, err := http.DefaultClient.Do(hr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
buf, err := io.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if string(buf) != "0.0.1" {
|
|
|
|
t.Fatalf("unknown version returned from health handler: %s", buf)
|
|
|
|
}
|
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
gs := grpc.NewServer()
|
|
|
|
pb.RegisterTestServer(gs, &testnServer{})
|
|
|
|
go func() {
|
|
|
|
if err := gs.Serve(lis); err != nil {
|
|
|
|
t.Fatalf("failed to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
// lookup server
|
|
|
|
service, err := r.LookupService(ctx, "helloworld")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2020-09-29 11:15:29 +03:00
|
|
|
}
|
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
if len(service) != 1 {
|
|
|
|
t.Fatalf("Expected 1 service got %d: %+v", len(service), service)
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
if len(service[0].Nodes) != 1 {
|
|
|
|
t.Fatalf("Expected 1 node got %d: %+v", len(service[0].Nodes), service[0].Nodes)
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
// create client
|
|
|
|
gc := gclient.NewClient(
|
|
|
|
client.ContentType("application/grpc"),
|
2023-01-07 23:56:14 +03:00
|
|
|
client.Codec("application/grpc", protocodec.NewCodec()),
|
|
|
|
client.Codec("application/grpc+proto", protocodec.NewCodec()), client.Router(rtr), client.Register(r), client.Broker(b))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
c := gpb.NewTestClient("helloworld", gc)
|
2020-12-29 13:44:22 +03:00
|
|
|
|
2023-01-07 19:16:48 +03:00
|
|
|
var md metadata.Metadata
|
|
|
|
t.Logf("call micro via micro")
|
|
|
|
rq := &pb.Request{Name: "John"}
|
|
|
|
for i := 0; i < 1500; i++ {
|
|
|
|
rq.Name += "name"
|
|
|
|
}
|
|
|
|
_, err = c.Call(ctx, rq,
|
|
|
|
client.WithResponseMetadata(&md),
|
|
|
|
gclient.CallOptions(grpc.UseCompressor(gzip.Name)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("response md %#+v", md)
|
|
|
|
|
|
|
|
ngcli, err := grpc.DialContext(ctx,
|
|
|
|
// lis.Addr().String(),
|
|
|
|
service[0].Nodes[0].Address,
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ngcli.Close()
|
|
|
|
|
|
|
|
var gmd gmetadata.MD
|
|
|
|
ngrpcsvc := pb.NewTestClient(ngcli)
|
|
|
|
t.Logf("call micro via native")
|
|
|
|
if _, err = ngrpcsvc.Call(ctx, rq,
|
|
|
|
grpc.UseCompressor(gzip.Name),
|
|
|
|
grpc.Header(&gmd)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Logf("gmd %#+v\n", gmd)
|
|
|
|
|
|
|
|
nxgcli, err := grpc.DialContext(ctx,
|
|
|
|
lis.Addr().String(),
|
|
|
|
// service[0].Nodes[0].Address,
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nxgcli.Close()
|
|
|
|
|
|
|
|
ngrpcsvc = pb.NewTestClient(nxgcli)
|
|
|
|
t.Logf("call native via native")
|
|
|
|
if _, err := ngrpcsvc.Call(ctx, rq,
|
|
|
|
grpc.UseCompressor(gzip.Name),
|
|
|
|
grpc.Header(&gmd)); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-09-29 11:15:29 +03:00
|
|
|
}
|
2023-01-07 19:16:48 +03:00
|
|
|
t.Logf("gmd %#+v\n", gmd)
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
//rsp := rpb.ServerReflectionResponse{}
|
|
|
|
//req := c.NewRequest("helloworld", "Test.ServerReflectionInfo", &rpb.ServerReflectionRequest{}, client.StreamingRequest())
|
|
|
|
//if err := c.Call(context.TODO(), req, &rsp); err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
//}
|
|
|
|
|
|
|
|
// select {}
|
|
|
|
}
|