2020-09-29 11:15:29 +03:00
|
|
|
package grpc_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-06 19:08:01 +03:00
|
|
|
"fmt"
|
2020-09-29 11:15:29 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-01-16 02:49:58 +03:00
|
|
|
gclient "github.com/unistack-org/micro-client-grpc/v3"
|
|
|
|
protocodec "github.com/unistack-org/micro-codec-proto/v3"
|
2021-01-29 16:47:26 +03:00
|
|
|
regRouter "github.com/unistack-org/micro-router-register/v3"
|
2021-01-16 02:49:58 +03:00
|
|
|
gserver "github.com/unistack-org/micro-server-grpc/v3"
|
2020-09-29 11:15:29 +03:00
|
|
|
pb "github.com/unistack-org/micro-tests/server/grpc/proto"
|
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-09-29 11:15:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2020-12-29 13:44:22 +03:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
2020-09-29 11:15:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type testServer 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 {
|
|
|
|
fmt.Printf("wrap ctx: %#+v req: %#+v\n", ctx, req)
|
|
|
|
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 {
|
|
|
|
if req.Name == "Error" {
|
|
|
|
return &errors.Error{Id: "id", Code: 99, Detail: "detail"}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGRPCServer(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
r := register.NewRegister()
|
|
|
|
b := broker.NewBroker(broker.Register(r))
|
2021-02-06 19:08:01 +03:00
|
|
|
s := gserver.NewServer(server.Codec("application/grpc+proto", protocodec.NewCodec()), server.Address(":12345"), server.Register(r), server.Name("helloworld"), gserver.Reflection(true),
|
|
|
|
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{}
|
|
|
|
err = pb.RegisterTestHandler(s, h)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't register handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = s.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// create client
|
2021-01-29 16:47:26 +03:00
|
|
|
c := gclient.NewClient(client.Codec("application/grpc+proto", protocodec.NewCodec()), client.Router(rtr), client.Register(r), client.Broker(b))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
testMethods := []string{
|
|
|
|
"Test.Call",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, method := range testMethods {
|
|
|
|
req := c.NewRequest("helloworld", method, &pb.Request{
|
|
|
|
Name: "John",
|
|
|
|
})
|
|
|
|
|
2020-12-29 13:44:22 +03:00
|
|
|
rsp := &pb.Response{}
|
2020-09-29 11:15:29 +03:00
|
|
|
|
2020-12-29 13:44:22 +03:00
|
|
|
err = c.Call(context.TODO(), req, rsp)
|
2020-09-29 11:15:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("method: %s err: %v", method, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Msg != "Hello John" {
|
|
|
|
t.Fatalf("Got unexpected response %v", rsp.Msg)
|
|
|
|
}
|
2020-12-29 13:44:22 +03:00
|
|
|
|
|
|
|
enc := &jsonpb.MarshalOptions{EmitUnpopulated: true}
|
2021-01-17 15:35:28 +03:00
|
|
|
_, err := enc.Marshal(rsp)
|
2020-12-29 13:44:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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 {}
|
|
|
|
}
|